From 5604a314347495eae9fb7613e5f32f011335789a Mon Sep 17 00:00:00 2001 From: alexeh Date: Fri, 4 Oct 2024 11:06:06 +0200 Subject: [PATCH] update project_size based on master data --- .../modules/base-data/base-data.repository.ts | 45 ++++++++++++++++++- .../modules/base-data/project-size.entity.ts | 14 +++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/api/src/modules/base-data/base-data.repository.ts b/api/src/modules/base-data/base-data.repository.ts index 6a32e1c0..a988702d 100644 --- a/api/src/modules/base-data/base-data.repository.ts +++ b/api/src/modules/base-data/base-data.repository.ts @@ -1,8 +1,9 @@ -import { DataSource, Repository } from 'typeorm'; +import { DataSource, IsNull, Not, Repository } from 'typeorm'; import { Injectable } from '@nestjs/common'; import { Country } from '@shared/entities/countries/country.entity'; import { BaseData } from '@api/modules/base-data/base-data.entity'; import { ParsedDBEntities } from '@api/modules/import/services/entity.preprocessor'; +import { ProjectSize } from '@api/modules/base-data/project-size.entity'; @Injectable() export class BaseDataRepository extends Repository { @@ -33,8 +34,50 @@ export class BaseDataRepository extends Repository { return await this.datasource.transaction(async (manager) => { const countryRepo = manager.getRepository(Country); const baseDataRepo = manager.getRepository(BaseData); + const projectSizeRepo = manager.getRepository(ProjectSize); await countryRepo.insert(data.countries); await baseDataRepo.insert(data.baseData); + + const result = await baseDataRepo.find({ + where: { projectSizeHa: Not(IsNull()) }, + }); + // Prepare a Map to accumulate updates per country_code + const updatesMap = new Map(); + + result.forEach((record) => { + const columnToUpdate = getProjectSizeColumn( + record.ecosystem, + record.activity, + ); + const countryCode = record.countryCode; + + if (!updatesMap.has(countryCode)) { + updatesMap.set(countryCode, { country_code: countryCode }); + } + const updateObj = updatesMap.get(countryCode); + updateObj[columnToUpdate] = record.projectSizeHa; + }); + + // Convert the updatesMap to an array + const updates = Array.from(updatesMap.values()); + await projectSizeRepo.upsert(updates, ['country_code']); }); } } + +function getProjectSizeColumn(ecosystem: string, activity: string): string { + if (ecosystem === 'Mangrove' && activity === 'Restoration') { + return 'mangrove_restored_area'; + } else if (ecosystem === 'Mangrove' && activity === 'Conservation') { + return 'mangrove_conserved_area'; + } else if (ecosystem === 'Seagrass' && activity === 'Restoration') { + return 'seagrass_restored_area'; + } else if (ecosystem === 'Seagrass' && activity === 'Conservation') { + return 'seagrass_conserved_area'; + } else if (ecosystem === 'Salt marsh' && activity === 'Restoration') { + return 'salt_marsh_restored_area'; + } else if (ecosystem === 'Salt marsh' && activity === 'Conservation') { + return 'salt_marsh_conserved_area'; + } + throw new Error('Invalid combination of ecosystem and activity'); +} diff --git a/api/src/modules/base-data/project-size.entity.ts b/api/src/modules/base-data/project-size.entity.ts index 59f82732..9f4d23ab 100644 --- a/api/src/modules/base-data/project-size.entity.ts +++ b/api/src/modules/base-data/project-size.entity.ts @@ -13,7 +13,7 @@ export class ProjectSize { @PrimaryGeneratedColumn() id: number; - @Column({ length: 3 }) + @Column({ length: 3, unique: true }) country_code: string; // Unidirectional relation @@ -21,21 +21,21 @@ export class ProjectSize { @JoinColumn({ name: 'country_code' }) country: Country; - @Column('numeric') + @Column('numeric', { nullable: true }) mangrove_restored_area: number; - @Column('numeric') + @Column('numeric', { nullable: true }) seagrass_restored_area: number; - @Column('numeric') + @Column('numeric', { nullable: true }) salt_marsh_restored_area: number; - @Column('numeric') + @Column('numeric', { nullable: true }) mangrove_conserved_area: number; - @Column('numeric') + @Column('numeric', { nullable: true }) seagrass_conserved_area: number; - @Column('numeric') + @Column('numeric', { nullable: true }) salt_marsh_conserved_area: number; }