Skip to content

Commit

Permalink
[TM-1519] POC project updates ready for review.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Dec 4, 2024
1 parent 794c045 commit e1968ff
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import { Model } from "sequelize-typescript";
import { isArray } from "lodash";
import { Attributes } from "sequelize";

export type AirtableEntity<T extends Model<T>> = {
TABLE_NAME: string;
UUID_COLUMN: string;
mapDbEntity: (entity: T) => Promise<object>;
findOne: (uuid: string) => Promise<T>;
};

/**
* A ColumnMapping is either a tuple of [dbColumn, airtableColumn], or a more descriptive object
*/
export type ColumnMapping<T extends Model<T>> =
| [keyof Attributes<T>, string]
| {
airtableColumn: string;
dbColumn?: keyof Attributes<T>;
valueMap: (entity: T) => Promise<null | string | number | boolean | Date>;
};

export const selectAttributes = <T extends Model<T>>(columns: ColumnMapping<T>[]) =>
columns.map(mapping => (isArray(mapping) ? mapping[0] : mapping.dbColumn)).filter(dbColumn => dbColumn != null);

export const mapEntityColumns = async <T extends Model<T>>(entity: T, columns: ColumnMapping<T>[]) => {
const airtableObject = {};
for (const mapping of columns) {
const airtableColumn = isArray(mapping) ? mapping[1] : mapping.airtableColumn;
airtableObject[airtableColumn] = isArray(mapping) ? entity[mapping[0]] : await mapping.valueMap(entity);
}

return airtableObject;
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
import { Project } from "@terramatch-microservices/database/entities";
import { AirtableEntity } from "./airtable-entity";
import { Organisation, Project } from "@terramatch-microservices/database/entities";
import { AirtableEntity, ColumnMapping, mapEntityColumns, selectAttributes } from "./airtable-entity";

const COHORTS = {
terrafund: "TerraFund Top 100",
"terrafund-landscapes": "TerraFund Landscapes",
ppc: "Priceless Planet Coalition (PPC)"
};

const UUID_COLUMN = "TM Project ID";

const COLUMNS: ColumnMapping<Project>[] = [
["uuid", UUID_COLUMN],
["name", "Project Name"],
{
airtableColumn: "TM Organization ID",
valueMap: async project => (await project.loadOrganisation())?.uuid
},
{
airtableColumn: "Organization Name Clean (lookup)",
valueMap: async project => (await project.loadOrganisation())?.name
},
["country", "Project Location Country Code"],
{
dbColumn: "frameworkKey",
airtableColumn: "Cohort",
valueMap: async ({ frameworkKey }) => COHORTS[frameworkKey]
},
["objectives", "Project Objectives"],
["budget", "Project Budget"],
["totalHectaresRestoredGoal", "Number of Hectares to be Restored"],
["goalTreesRestoredPlanting", "Number of Trees to be Planted"],
["goalTreesRestoredAnr", "Total Trees Naturally Regenerated"],
["jobsCreatedGoal", "Jobs to be Created"],
["projBeneficiaries", "Project Beneficiaries Expected"],
["plantingStartDate", "Planting Dates - Start"],
["plantingEndDate", "Planting Dates - End"]
];

export const ProjectEntity: AirtableEntity<Project> = {
TABLE_NAME: "Projects",
UUID_COLUMN: "TM Project ID",
UUID_COLUMN,

findOne: async (uuid: string) => await Project.findOne({ where: { uuid } }),
findOne: async (uuid: string) =>
await Project.findOne({
where: { uuid },
attributes: selectAttributes(COLUMNS),
include: { model: Organisation, attributes: ["uuid", "name"] }
}),

mapDbEntity: async (project: Project) => ({
"Project Name": project.name
})
mapDbEntity: async (project: Project) => mapEntityColumns(project, COLUMNS)
};
2 changes: 1 addition & 1 deletion libs/database/src/lib/entities/organisation.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AllowNull, AutoIncrement, Column, Default, Index, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, BOOLEAN, DATE, DECIMAL, ENUM, INTEGER, STRING, TEXT, TINYINT, UUID } from "sequelize";

@Table({ tableName: "organisations", underscored: true })
@Table({ tableName: "organisations", underscored: true, paranoid: true })
export class Organisation extends Model<Organisation> {
@PrimaryKey
@AutoIncrement
Expand Down
1 change: 1 addition & 0 deletions libs/database/src/lib/entities/project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Project extends Model<Project> {

@AllowNull
@ForeignKey(() => Organisation)
@Column(BIGINT.UNSIGNED)
organisationId: number | null;

// TODO once the Application record has been added
Expand Down

0 comments on commit e1968ff

Please sign in to comment.