From c68beb7d59d819b46cd6e2b14633ba38da6c4105 Mon Sep 17 00:00:00 2001 From: kgajowy Date: Thu, 16 Sep 2021 10:09:37 +0200 Subject: [PATCH] fix(scenarios): provide project-id when updating scenario so that wdpa is assigned correctly --- .../scenarios/scenarios-crud.service.ts | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/api/apps/api/src/modules/scenarios/scenarios-crud.service.ts b/api/apps/api/src/modules/scenarios/scenarios-crud.service.ts index b0cde0c1b3..91570a34ef 100644 --- a/api/apps/api/src/modules/scenarios/scenarios-crud.service.ts +++ b/api/apps/api/src/modules/scenarios/scenarios-crud.service.ts @@ -21,6 +21,7 @@ import { AppConfig } from '@marxan-api/utils/config.utils'; import { WdpaAreaCalculationService } from './wdpa-area-calculation.service'; import { CommandBus } from '@nestjs/cqrs'; import { CalculatePlanningUnitsProtectionLevel } from '../planning-units-protection-level'; +import { assertDefined } from '@marxan/utils'; const scenarioFilterKeyNames = ['name', 'type', 'projectId', 'status'] as const; type ScenarioFilterKeys = keyof Pick< @@ -63,11 +64,14 @@ export class ScenariosCrudService extends AppBaseService< async actionAfterCreate( model: Scenario, createModel: CreateScenarioDTO, - _?: AppInfoDTO, + _?: ScenarioInfoDTO, ): Promise { if (this.wdpaCalculationsDetector.shouldTrigger(model, createModel)) { await this.commandBus.execute( - new CalculatePlanningUnitsProtectionLevel(model.id, model.protectedAreaFilterByIds), + new CalculatePlanningUnitsProtectionLevel( + model.id, + model.protectedAreaFilterByIds, + ), ); } } @@ -75,11 +79,14 @@ export class ScenariosCrudService extends AppBaseService< async actionAfterUpdate( model: Scenario, updateModel: UpdateScenarioDTO, - _?: AppInfoDTO, + _?: ScenarioInfoDTO, ): Promise { if (this.wdpaCalculationsDetector.shouldTrigger(model, updateModel)) { await this.commandBus.execute( - new CalculatePlanningUnitsProtectionLevel(model.id, model.protectedAreaFilterByIds), + new CalculatePlanningUnitsProtectionLevel( + model.id, + model.protectedAreaFilterByIds, + ), ); } } @@ -167,7 +174,7 @@ export class ScenariosCrudService extends AppBaseService< setFilters( query: SelectQueryBuilder, filters: ScenarioFilters, - _info?: AppInfoDTO, + _info?: ScenarioInfoDTO, ): SelectQueryBuilder { query = this._processBaseFilters( query, @@ -179,9 +186,10 @@ export class ScenariosCrudService extends AppBaseService< async setDataCreate( create: CreateScenarioDTO, - info?: AppInfoDTO, + info?: ScenarioInfoDTO, ): Promise { const model = await super.setDataCreate(create, info); + assertDefined(model.projectId); /** * We always compute the list of protected areas to associate to a scenario * from the list of IUCN categories and the list of project-specific protected @@ -208,8 +216,16 @@ export class ScenariosCrudService extends AppBaseService< async setDataUpdate( model: Scenario, update: UpdateScenarioDTO, - info?: AppInfoDTO, + info?: ScenarioInfoDTO, ): Promise { + update.projectId = ( + await this.projectRepository.findOne({ + where: { + id: model.projectId, + }, + }) + )?.id; + assertDefined(update.projectId); model = await super.setDataUpdate(model, update, info); /** * We always compute the list of protected areas to associate to a scenario @@ -251,7 +267,7 @@ export class ScenariosCrudService extends AppBaseService< }: | Pick | Pick, - _info?: AppInfoDTO, + _info?: ScenarioInfoDTO, ): Promise { /** * If no IUCN categories were supplied, we're done.