Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scenarios): provide project-id when updating scenario so that wdpa is assigned correctly #534

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions api/apps/api/src/modules/scenarios/scenarios-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -63,23 +64,29 @@ export class ScenariosCrudService extends AppBaseService<
async actionAfterCreate(
model: Scenario,
createModel: CreateScenarioDTO,
_?: AppInfoDTO,
_?: ScenarioInfoDTO,
): Promise<void> {
if (this.wdpaCalculationsDetector.shouldTrigger(model, createModel)) {
await this.commandBus.execute(
new CalculatePlanningUnitsProtectionLevel(model.id, model.protectedAreaFilterByIds),
new CalculatePlanningUnitsProtectionLevel(
model.id,
model.protectedAreaFilterByIds,
),
);
}
}

async actionAfterUpdate(
model: Scenario,
updateModel: UpdateScenarioDTO,
_?: AppInfoDTO,
_?: ScenarioInfoDTO,
): Promise<void> {
if (this.wdpaCalculationsDetector.shouldTrigger(model, updateModel)) {
await this.commandBus.execute(
new CalculatePlanningUnitsProtectionLevel(model.id, model.protectedAreaFilterByIds),
new CalculatePlanningUnitsProtectionLevel(
model.id,
model.protectedAreaFilterByIds,
),
);
}
}
Expand Down Expand Up @@ -167,7 +174,7 @@ export class ScenariosCrudService extends AppBaseService<
setFilters(
query: SelectQueryBuilder<Scenario>,
filters: ScenarioFilters,
_info?: AppInfoDTO,
_info?: ScenarioInfoDTO,
): SelectQueryBuilder<Scenario> {
query = this._processBaseFilters<ScenarioFilters>(
query,
Expand All @@ -179,9 +186,10 @@ export class ScenariosCrudService extends AppBaseService<

async setDataCreate(
create: CreateScenarioDTO,
info?: AppInfoDTO,
info?: ScenarioInfoDTO,
): Promise<Scenario> {
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
Expand All @@ -208,8 +216,16 @@ export class ScenariosCrudService extends AppBaseService<
async setDataUpdate(
model: Scenario,
update: UpdateScenarioDTO,
info?: AppInfoDTO,
info?: ScenarioInfoDTO,
): Promise<Scenario> {
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
Expand Down Expand Up @@ -251,7 +267,7 @@ export class ScenariosCrudService extends AppBaseService<
}:
| Pick<CreateScenarioDTO, 'projectId' | 'wdpaIucnCategories'>
| Pick<UpdateScenarioDTO, 'projectId' | 'wdpaIucnCategories'>,
_info?: AppInfoDTO,
_info?: ScenarioInfoDTO,
): Promise<string[] | undefined> {
/**
* If no IUCN categories were supplied, we're done.
Expand Down