Skip to content

Commit

Permalink
[TM-1675] Update some columns on project and project-report.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Jan 29, 2025
1 parent e48d0e1 commit 950645f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { faker } from "@faker-js/faker";
import {
Application,
Demographic,
Framework,
Nursery,
NurseryReport,
Organisation,
Expand Down Expand Up @@ -49,7 +50,7 @@ import {
} from "./";
import { orderBy, sortBy } from "lodash";
import { Model } from "sequelize-typescript";
import { FRAMEWORK_NAMES, FrameworkKey } from "@terramatch-microservices/database/constants/framework";
import { FrameworkKey } from "@terramatch-microservices/database/constants/framework";
import { FindOptions, Op } from "sequelize";
import { DateTime } from "luxon";

Expand Down Expand Up @@ -417,9 +418,21 @@ describe("AirtableEntity", () => {
let projects: Project[];
let calculatedValues: Record<string, Record<string, string | number>>;

const FRAMEWORK_NAMES = {
ppc: "PPC",
terrafund: "TerraFund Top 100"
};

beforeAll(async () => {
await Project.truncate();

for (const framework of await Framework.findAll()) {
await framework.destroy();
}
for (const [slug, name] of Object.entries(FRAMEWORK_NAMES)) {
await Framework.create({ uuid: faker.string.uuid(), slug, name });
}

const orgs = await OrganisationFactory.createMany(3);
organisationUuids = orgs.reduce((uuids, { id, uuid }) => ({ ...uuids, [id]: uuid }), {});
const orgIds = orgs.reduce((ids, { id }) => [...ids, id], [] as number[]);
Expand Down Expand Up @@ -487,7 +500,7 @@ describe("AirtableEntity", () => {
fields: {
uuid,
name,
cohort: FRAMEWORK_NAMES[frameworkKey] ?? frameworkKey,
framework: FRAMEWORK_NAMES[frameworkKey] ?? frameworkKey,
organisationUuid: organisationUuids[organisationId],
applicationUuid: applicationUuids[applicationId],
hectaresRestoredToDate: calculatedValues[uuid]?.hectaresRestoredToDate ?? 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export abstract class AirtableEntity<ModelType extends Model<ModelType>, Associa
return {
attributes: selectAttributes(this.COLUMNS),
include: selectIncludes(this.COLUMNS),
order: ["id"],
limit: AIRTABLE_PAGE_SIZE,
offset: page * AIRTABLE_PAGE_SIZE,
where
Expand All @@ -102,9 +103,10 @@ export abstract class AirtableEntity<ModelType extends Model<ModelType>, Associa
return {
attributes: [this.IDENTITY_COLUMN],
paranoid: false,
where,
order: ["id"],
limit: AIRTABLE_PAGE_SIZE,
offset: page * AIRTABLE_PAGE_SIZE
offset: page * AIRTABLE_PAGE_SIZE,
where
} as FindOptions<ModelType>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ const COLUMNS: ColumnMapping<ProjectReport, ProjectReportAssociations>[] = [
},
"technicalNarrative",
"publicNarrative",
"totalUniqueRestorationPartners"
"totalUniqueRestorationPartners",
"businessMilestones"
];

export class ProjectReportEntity extends AirtableEntity<ProjectReport, ProjectReportAssociations> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Application, Organisation, Project, Site, SitePolygon } from "@terramatch-microservices/database/entities";
import {
Application,
Framework,
Organisation,
Project,
Site,
SitePolygon
} from "@terramatch-microservices/database/entities";
import { AirtableEntity, ColumnMapping, commonEntityColumns } from "./airtable-entity";
import { flatten, groupBy } from "lodash";
import { FRAMEWORK_NAMES } from "@terramatch-microservices/database/constants/framework";

const loadApprovedSites = async (projectIds: number[]) =>
groupBy(
Expand Down Expand Up @@ -29,9 +35,10 @@ const COLUMNS: ColumnMapping<Project, ProjectAssociations>[] = [
...commonEntityColumns<Project, ProjectAssociations>("project"),
"name",
{
airtableColumn: "framework",
dbColumn: "frameworkKey",
airtableColumn: "cohort",
valueMap: async ({ frameworkKey }) => FRAMEWORK_NAMES[frameworkKey] ?? frameworkKey
include: [{ model: Framework, attributes: ["name"] }],
valueMap: async ({ framework, frameworkKey }) => framework?.name ?? frameworkKey
},
{
airtableColumn: "applicationUuid",
Expand Down
20 changes: 9 additions & 11 deletions libs/database/src/lib/constants/framework.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
export const FRAMEWORK_NAMES = {
terrafund: "TerraFund Top 100",
"terrafund-landscapes": "TerraFund Landscapes",
ppc: "Priceless Planet Coalition (PPC)",
enterprises: "TerraFund Enterprises",
hbf: "Harit Bharat Fund",
"epa-ghana-pilot": "EPA-Ghana Pilot"
} as const;

export const FRAMEWORK_KEYS = Object.keys(FRAMEWORK_NAMES);
export type FrameworkKey = keyof typeof FRAMEWORK_NAMES;
export const FRAMEWORK_KEYS = [
"terrafund",
"terrafund-landscapes",
"ppc",
"enterprises",
"hbf",
"epa-ghana-pilot"
] as const;
export type FrameworkKey = (typeof FRAMEWORK_KEYS)[number];
11 changes: 8 additions & 3 deletions libs/database/src/lib/entities/framework.entity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { AutoIncrement, Column, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, STRING } from "sequelize";
import { AutoIncrement, Column, Index, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BIGINT, STRING, UUID } from "sequelize";

// A quick stub to get the information needed for users/me
// Incomplete stub
@Table({ tableName: "frameworks", underscored: true })
export class Framework extends Model<Framework> {
@PrimaryKey
@AutoIncrement
@Column(BIGINT.UNSIGNED)
override id: number;

@Index
@Column(UUID)
uuid: string;

@Index
@Column(STRING(20))
slug: string;

Expand Down
4 changes: 4 additions & 0 deletions libs/database/src/lib/entities/project-report.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ export class ProjectReport extends Model<ProjectReport> {
@Column(INTEGER.UNSIGNED)
totalUniqueRestorationPartners: number | null;

@AllowNull
@Column(TEXT)
businessMilestones: string | null;

@HasMany(() => TreeSpecies, {
foreignKey: "speciesableId",
constraints: false,
Expand Down
4 changes: 4 additions & 0 deletions libs/database/src/lib/entities/project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Site } from "./site.entity";
import { Nursery } from "./nursery.entity";
import { JsonColumn } from "../decorators/json-column.decorator";
import { FrameworkKey } from "../constants/framework";
import { Framework } from "./framework.entity";

@Table({ tableName: "v2_projects", underscored: true, paranoid: true })
export class Project extends Model<Project> {
Expand All @@ -39,6 +40,9 @@ export class Project extends Model<Project> {
@Column(STRING)
frameworkKey: FrameworkKey | null;

@BelongsTo(() => Framework, { foreignKey: "frameworkKey", targetKey: "slug", constraints: false })
framework: Framework | null;

@Default(false)
@Column(BOOLEAN)
isTest: boolean;
Expand Down

0 comments on commit 950645f

Please sign in to comment.