Skip to content

Commit

Permalink
feature(Specification): Deletes all ScenarioFeaturePreparations rows …
Browse files Browse the repository at this point in the history
…for a Scenario before processing the Specification
  • Loading branch information
KevSanchez committed Apr 17, 2024
1 parent 3e92fe9 commit d6c9e08
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ 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: [
TypeOrmModule.forFeature([
SpecificationApiEntity,
SpecificationFeatureConfigApiEntity,
]),
TypeOrmModule.forFeature(
[ScenarioFeaturesPreparation],
DbConnections.geoprocessingDB,
),
ApiEventsModule,
],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpecificationCandidateCreated>
{
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddScenarioIndexForScenarioFeaturePreparation1713363912361
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE INDEX scenario_features_preparation_scenario_id__idx ON scenario_features_preparation(scenario_id);
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX scenario_features_preparation_scenario_id__idx;`,
);
}
}

0 comments on commit d6c9e08

Please sign in to comment.