Skip to content

Commit

Permalink
fix(scenario): planning units lockin change
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Jul 21, 2021
1 parent ee82915 commit 2dbc261
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@nestjs/common';
import {
UpdatePlanningUnitsEventsPort,
UpdatePlanningUnitsState,
} from '../update-planning-units-events.port';

@Injectable()
export class ApiEventsMock implements UpdatePlanningUnitsEventsPort {
eventMock = jest.fn();

event(
scenarioId: string,
state: UpdatePlanningUnitsState,
context?: Record<string, unknown> | Error,
): Promise<void> {
return this.eventMock(scenarioId, state, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { Test } from '@nestjs/testing';

import { ArePuidsAllowedPort } from '../shared/are-puids-allowed.port';
import { RequestJobPort } from './request-job.port';
import { UpdatePlanningUnitsEventsPort } from './update-planning-units-events.port';

import { ArePuidsAllowedMock } from '../shared/__mocks__/are-puuids-allowed.mock';
import { RequestJobPortMock } from './__mocks__/request-job-port.mock';
import { validGeoJson } from './__mocks__/geojson';
import { ApiEventsMock } from './__mocks__/api-events.mock';

let sut: UpdatePlanningUnitsService;

let puIdValidator: ArePuidsAllowedMock;
let apiEvents: ApiEventsMock;
let jobRequester: RequestJobPortMock;

const scenarioId = 'fake-scenario-id';
Expand All @@ -26,13 +29,18 @@ beforeEach(async () => {
provide: RequestJobPort,
useClass: RequestJobPortMock,
},
{
provide: UpdatePlanningUnitsEventsPort,
useClass: ApiEventsMock,
},
UpdatePlanningUnitsService,
],
}).compile();

sut = sandbox.get(UpdatePlanningUnitsService);
puIdValidator = sandbox.get(ArePuidsAllowedPort);
jobRequester = sandbox.get(RequestJobPort);
apiEvents = sandbox.get(UpdatePlanningUnitsEventsPort);
});

describe(`when PU IDs are not available`, () => {
Expand All @@ -53,6 +61,24 @@ describe(`when PU IDs are not available`, () => {
},
}),
).rejects.toThrow(/does not match any planning unit/);
expect(apiEvents.eventMock.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"fake-scenario-id",
"submitted",
undefined,
],
Array [
"fake-scenario-id",
"failed",
Object {
"errors": Array [
"Oups, you tricky person!",
],
},
],
]
`);
expect(puIdValidator.mock.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"fake-scenario-id",
Expand Down Expand Up @@ -89,5 +115,14 @@ describe(`when PU IDs are available`, () => {
scenarioId,
},
]);
expect(apiEvents.eventMock.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"fake-scenario-id",
"submitted",
undefined,
],
]
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import { AdjustPlanningUnitsInput } from '../../entry-points/adjust-planning-uni

import { ArePuidsAllowedPort } from '../shared/are-puids-allowed.port';
import { RequestJobPort } from './request-job.port';
import {
UpdatePlanningUnitsEventsPort,
UpdatePlanningUnitsState,
} from './update-planning-units-events.port';

@Injectable()
export class UpdatePlanningUnitsService implements AdjustPlanningUnits {
constructor(
private readonly puUuidValidator: ArePuidsAllowedPort,
// TODO: ApiEvents Service wrapper - similar to one used in CostSurfaceFacade
private readonly apiEvents: UpdatePlanningUnitsEventsPort,
private readonly jobRequester: RequestJobPort,
) {}

async update(
scenarioId: string,
constraints: AdjustPlanningUnitsInput,
): Promise<void> {
// TODO: ApiEvents: submitted

await this.apiEvents.event(scenarioId, UpdatePlanningUnitsState.Submitted);
const targetPuIds = [
...(constraints.include?.pu ?? []),
...(constraints.exclude?.pu ?? []),
Expand All @@ -29,19 +32,31 @@ export class UpdatePlanningUnitsService implements AdjustPlanningUnits {
targetPuIds,
);
if (errors.length > 0) {
// TODO: ApiEvents: failed - remove throw
await this.apiEvents.event(
scenarioId,
UpdatePlanningUnitsState.Failed,
{
errors,
},
);
throw new Error(
'One or more of the planning units provided for exclusion or inclusion does not match any planning unit of the present scenario.',
);
}
}

await this.jobRequester.queue({
scenarioId,
...constraints,
});

// TODO: ApiEvents: failed - if adding to queue failed
try {
await this.jobRequester.queue({
scenarioId,
...constraints,
});
} catch (error) {
await this.apiEvents.event(
scenarioId,
UpdatePlanningUnitsState.Failed,
error,
);
}

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ export class ArePuidsAllowedAdapter
puIds: string[],
): Promise<{ errors: unknown[] }> {
const allowedFeaturesIds = (
await this.findAll(undefined, {
params: {
scenarioId,
await this.findAll(
{
disablePagination: true,
},
})
{
params: {
scenarioId,
},
},
)
)[0]
.map((scenario) => scenario.puGeometryId)
.filter(isDefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export class ScenarioPlanningUnitsInclusionProcessor
);
}

if (!includeGeo && !excludeGeo) {
geometriesIdsToInclude.push(...(job.data.include?.pu ?? []));
geometriesIdsToExclude.push(...(job.data.exclude?.pu ?? []));
}

await this.scenarioPlanningUnitsRepo.update(
{
scenarioId,
Expand Down

0 comments on commit 2dbc261

Please sign in to comment.