-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TM-1519] POC project updates ready for review.
- Loading branch information
Showing
4 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
apps/unified-database-service/src/airtable/entities/airtable-entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
53 changes: 46 additions & 7 deletions
53
apps/unified-database-service/src/airtable/entities/project.airtable-entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters