Skip to content

Commit

Permalink
feat(custom-pu-grid): set project grid shape once geometries were set
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Sep 15, 2021
1 parent 6cf28ba commit bb697cc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<any>): Observable<ICommand> =>
events$.pipe(
ofType(CustomPlanningUnitGridSet),
map(
(event) =>
new SetProjectGridFromShapefile(new ProjectId(event.projectId)),
),
);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
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,
setPlanningUnitGridQueueProvider,
} 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],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Command } from '@nestjs-architects/typed-cqrs';
import { ProjectId } from './project.id';

export class SetProjectGridFromShapefile extends Command<void> {
constructor(public readonly projectId: ProjectId) {
super();
}
}
Original file line number Diff line number Diff line change
@@ -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<SetProjectGridFromShapefile> {
constructor(
@InjectRepository(Project) private readonly projects: Repository<Project>,
) {}

async execute({ projectId }: SetProjectGridFromShapefile): Promise<void> {
await this.projects.update(
{
id: projectId.value,
},
{
planningUnitGridShape: PlanningUnitGridShape.fromShapefile,
},
);
}
}
2 changes: 1 addition & 1 deletion api/apps/api/src/modules/projects/project.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const projectResource: BaseServiceResource = {
export enum PlanningUnitGridShape {
square = 'square',
hexagon = 'hexagon',
fromShapefile = 'irregular',
fromShapefile = 'from_shapefile',
}

@Entity('projects')
Expand Down

0 comments on commit bb697cc

Please sign in to comment.