From a3ef1302a752f696583e1b53f677a50d57463876 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Apr 2024 16:30:30 +0200 Subject: [PATCH] feature(Specification): Deletes all ScenarioFeaturePreparations rows for a Scenario before processing the Specification --- .../adapters/specification-adapters.module.ts | 6 ++++++ .../specification-candidate-created.handler.ts | 15 ++++++++++++++- ...cenarioIndexForScenarioFeaturePreparation.ts | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 api/apps/geoprocessing/src/migrations/geoprocessing/1713363912361-AddScenarioIndexForScenarioFeaturePreparation.ts diff --git a/api/apps/api/src/modules/specification/adapters/specification-adapters.module.ts b/api/apps/api/src/modules/specification/adapters/specification-adapters.module.ts index 0bb6e61f78..abb49de937 100644 --- a/api/apps/api/src/modules/specification/adapters/specification-adapters.module.ts +++ b/api/apps/api/src/modules/specification/adapters/specification-adapters.module.ts @@ -9,6 +9,8 @@ import { SpecificationFeatureConfigApiEntity } from './specification-feature-con import { SpecificationCandidateCreatedHandler } from './specification-candidate-created.handler'; import { SpecificationRepository } from '../application/specification.repository'; +import { ScenarioFeaturesPreparation } from '@marxan/features'; +import { DbConnections } from '@marxan-api/ormconfig.connections'; @Module({ imports: [ @@ -16,6 +18,10 @@ import { SpecificationRepository } from '../application/specification.repository SpecificationApiEntity, SpecificationFeatureConfigApiEntity, ]), + TypeOrmModule.forFeature( + [ScenarioFeaturesPreparation], + DbConnections.geoprocessingDB, + ), ApiEventsModule, ], providers: [ diff --git a/api/apps/api/src/modules/specification/adapters/specification-candidate-created.handler.ts b/api/apps/api/src/modules/specification/adapters/specification-candidate-created.handler.ts index 907b2f4954..4940289d81 100644 --- a/api/apps/api/src/modules/specification/adapters/specification-candidate-created.handler.ts +++ b/api/apps/api/src/modules/specification/adapters/specification-candidate-created.handler.ts @@ -2,15 +2,28 @@ import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; import { SpecificationCandidateCreated } from '../domain'; import { API_EVENT_KINDS } from '@marxan/api-events'; import { ApiEventsService } from '@marxan-api/modules/api-events/api-events.service'; +import { InjectEntityManager } from '@nestjs/typeorm'; +import { DbConnections } from '@marxan-api/ormconfig.connections'; +import { EntityManager } from 'typeorm'; +import { ScenarioFeaturesPreparation } from '@marxan/features'; @EventsHandler(SpecificationCandidateCreated) export class SpecificationCandidateCreatedHandler implements IEventHandler { - constructor(private readonly apiEvents: ApiEventsService) {} + constructor( + private readonly apiEvents: ApiEventsService, + @InjectEntityManager(DbConnections.geoprocessingDB) + private readonly geoEntityManager: EntityManager, + ) {} async handle(event: SpecificationCandidateCreated) { if (event.draft) return; + + await this.geoEntityManager.delete(ScenarioFeaturesPreparation, { + scenarioId: event.scenarioId, + }); + await this.apiEvents.create({ kind: API_EVENT_KINDS.scenario__specification__submitted__v1__alpha1, topic: event.scenarioId, diff --git a/api/apps/geoprocessing/src/migrations/geoprocessing/1713363912361-AddScenarioIndexForScenarioFeaturePreparation.ts b/api/apps/geoprocessing/src/migrations/geoprocessing/1713363912361-AddScenarioIndexForScenarioFeaturePreparation.ts new file mode 100644 index 0000000000..cf66949d2c --- /dev/null +++ b/api/apps/geoprocessing/src/migrations/geoprocessing/1713363912361-AddScenarioIndexForScenarioFeaturePreparation.ts @@ -0,0 +1,17 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddScenarioIndexForScenarioFeaturePreparation1713363912361 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE INDEX scenario_features_preparation_scenario_id__idx ON scenario_features_preparation(scenario_id); + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DROP INDEX scenario_features_preparation_scenario_id__idx;`, + ); + } +}