diff --git a/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid-set.saga.ts b/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid-set.saga.ts new file mode 100644 index 0000000000..f65d5e1688 --- /dev/null +++ b/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid-set.saga.ts @@ -0,0 +1,19 @@ +import { ICommand, ofType, Saga } from '@nestjs/cqrs'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; + +import { CustomPlanningUnitGridSet } from '../events/custom-planning-unit-grid-set.event'; +import { SetProjectGridFromShapefile } from './set-project-grid-from-shapefile.command'; +import { ProjectId } from './project.id'; + +export class PlanningUnitGridSetSaga { + @Saga() + puGridSet = (events$: Observable): Observable => + events$.pipe( + ofType(CustomPlanningUnitGridSet), + map( + (event) => + new SetProjectGridFromShapefile(new ProjectId(event.projectId)), + ), + ); +} diff --git a/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid.module.ts b/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid.module.ts index b3517ae262..92816c41b8 100644 --- a/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid.module.ts +++ b/api/apps/api/src/modules/projects/planning-unit-grid/planning-unit-grid.module.ts @@ -1,8 +1,11 @@ import { Module } from '@nestjs/common'; import { QueueApiEventsModule } from '@marxan-api/modules/queue-api-events'; import { ApiEventsModule } from '@marxan-api/modules/api-events'; +import { TypeOrmModule } from '@nestjs/typeorm'; import { CqrsModule } from '@nestjs/cqrs'; +import { Project } from '@marxan-api/modules/projects/project.api.entity'; + import { setPlanningUnitGridEventsFactoryProvider, setPlanningUnitGridQueueEventsProvider, @@ -10,15 +13,24 @@ import { } from './queue.providers'; import { PlanningUnitGridEventsHandler } from './planning-unit-grid-events.handler'; import { PlanningUnitGridService } from './planning-unit-grid.service'; +import { PlanningUnitGridSetSaga } from './planning-unit-grid-set.saga'; +import { SetProjectGridFromShapefileHandler } from './set-project-grid-from-shapefile.handler'; @Module({ - imports: [QueueApiEventsModule, ApiEventsModule, CqrsModule], + imports: [ + QueueApiEventsModule, + ApiEventsModule, + CqrsModule, + TypeOrmModule.forFeature([Project]), + ], providers: [ setPlanningUnitGridQueueProvider, setPlanningUnitGridQueueEventsProvider, setPlanningUnitGridEventsFactoryProvider, PlanningUnitGridEventsHandler, PlanningUnitGridService, + PlanningUnitGridSetSaga, + SetProjectGridFromShapefileHandler, ], exports: [PlanningUnitGridService], }) diff --git a/api/apps/api/src/modules/projects/planning-unit-grid/set-project-grid-from-shapefile.command.ts b/api/apps/api/src/modules/projects/planning-unit-grid/set-project-grid-from-shapefile.command.ts new file mode 100644 index 0000000000..e37aa454de --- /dev/null +++ b/api/apps/api/src/modules/projects/planning-unit-grid/set-project-grid-from-shapefile.command.ts @@ -0,0 +1,8 @@ +import { Command } from '@nestjs-architects/typed-cqrs'; +import { ProjectId } from './project.id'; + +export class SetProjectGridFromShapefile extends Command { + constructor(public readonly projectId: ProjectId) { + super(); + } +} diff --git a/api/apps/api/src/modules/projects/planning-unit-grid/set-project-grid-from-shapefile.handler.ts b/api/apps/api/src/modules/projects/planning-unit-grid/set-project-grid-from-shapefile.handler.ts new file mode 100644 index 0000000000..8fda4b057f --- /dev/null +++ b/api/apps/api/src/modules/projects/planning-unit-grid/set-project-grid-from-shapefile.handler.ts @@ -0,0 +1,28 @@ +import { CommandHandler, IInferredCommandHandler } from '@nestjs/cqrs'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; + +import { + PlanningUnitGridShape, + Project, +} from '@marxan-api/modules/projects/project.api.entity'; +import { SetProjectGridFromShapefile } from './set-project-grid-from-shapefile.command'; + +@CommandHandler(SetProjectGridFromShapefile) +export class SetProjectGridFromShapefileHandler + implements IInferredCommandHandler { + constructor( + @InjectRepository(Project) private readonly projects: Repository, + ) {} + + async execute({ projectId }: SetProjectGridFromShapefile): Promise { + await this.projects.update( + { + id: projectId.value, + }, + { + planningUnitGridShape: PlanningUnitGridShape.fromShapefile, + }, + ); + } +} diff --git a/api/apps/api/src/modules/projects/project.api.entity.ts b/api/apps/api/src/modules/projects/project.api.entity.ts index d1010128b2..21987a00ca 100644 --- a/api/apps/api/src/modules/projects/project.api.entity.ts +++ b/api/apps/api/src/modules/projects/project.api.entity.ts @@ -27,7 +27,7 @@ export const projectResource: BaseServiceResource = { export enum PlanningUnitGridShape { square = 'square', hexagon = 'hexagon', - fromShapefile = 'irregular', + fromShapefile = 'from_shapefile', } @Entity('projects')