Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scenario-pu): allow to reset lock status to default #532

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
import { EntityManager, Repository } from 'typeorm';

import { DbConnections } from '@marxan-api/ormconfig.connections';
import { ScenariosPlanningUnitGeoEntity } from '@marxan/scenarios-planning-unit';
import {
LockStatus,
ScenariosPlanningUnitGeoEntity,
} from '@marxan/scenarios-planning-unit';

@Injectable()
export class ScenarioPlanningUnitsService {
Expand All @@ -13,6 +16,8 @@ export class ScenarioPlanningUnitsService {
DbConnections.geoprocessingDB,
)
private readonly puRepo: Repository<ScenariosPlanningUnitGeoEntity>,
@InjectEntityManager(DbConnections.geoprocessingDB)
private readonly entityManager: EntityManager,
) {}

async get(scenarioId: string): Promise<ScenariosPlanningUnitGeoEntity[]> {
Expand All @@ -22,4 +27,30 @@ export class ScenarioPlanningUnitsService {
},
});
}

async resetLockStatus(scenarioId: string): Promise<void> {
return await this.entityManager.transaction(async (manager) => {
await manager.update(
ScenariosPlanningUnitGeoEntity,
{
scenarioId,
protectedByDefault: false,
},
{
lockStatus: LockStatus.Unstated,
},
);

await manager.update(
ScenariosPlanningUnitGeoEntity,
{
scenarioId,
protectedByDefault: true,
},
{
lockStatus: LockStatus.LockedIn,
},
);
});
}
}
9 changes: 9 additions & 0 deletions api/apps/api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ export class ScenariosController {
return;
}

@Delete(`:id/planning-units`)
@ApiOkResponse()
async resetPlanningUnitsLockStatus(
@Param('id', ParseUUIDPipe) id: string,
): Promise<void> {
await this.service.resetLockStatus(id);
return;
}

@Get(':id/planning-units')
@ApiOkResponse({ type: ScenarioPlanningUnitDto, isArray: true })
async getPlanningUnits(
Expand Down
5 changes: 5 additions & 0 deletions api/apps/api/src/modules/scenarios/scenarios.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,9 @@ export class ScenariosService {
getCostRange(scenarioId: string): Promise<CostRange> {
return this.costService.getRange(scenarioId);
}

async resetLockStatus(scenarioId: string) {
await this.assertScenario(scenarioId);
await this.planningUnitsService.resetLockStatus(scenarioId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { bootstrapApplication } from '../utils/api-application';
import { GivenScenarioPuDataExists } from '../steps/given-scenario-pu-data-exists';
import { v4 } from 'uuid';
import { Repository } from 'typeorm';
import {
LockStatus,
ScenariosPlanningUnitGeoEntity,
} from '@marxan/scenarios-planning-unit';
import { getRepositoryToken } from '@nestjs/typeorm';
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { ScenarioPlanningUnitsService } from '@marxan-api/modules/scenarios/planning-units/scenario-planning-units.service';
import { FixtureType } from '@marxan/utils/tests/fixture-type';

let fixtures: FixtureType<typeof getFixtures>;

beforeEach(async () => {
fixtures = await getFixtures();
});

afterEach(async () => {
await fixtures?.cleanup();
});

test(`resetting lock status`, async () => {
await fixtures.GivenScenarioPlanningUnitsExist();
await fixtures.WhenRequestingToResetLockStatus();
await fixtures.ThenPlanningUnitsAreChangedToDefaultState();
});

const getFixtures = async () => {
const app = await bootstrapApplication();
const repo: Repository<ScenariosPlanningUnitGeoEntity> = app.get(
getRepositoryToken(
ScenariosPlanningUnitGeoEntity,
DbConnections.geoprocessingDB,
),
);
const sut = app.get(ScenarioPlanningUnitsService);
const scenarioId = v4();

return {
GivenScenarioPlanningUnitsExist: async () =>
await GivenScenarioPuDataExists(repo, scenarioId),
WhenRequestingToResetLockStatus: async () =>
sut.resetLockStatus(scenarioId),
ThenPlanningUnitsAreChangedToDefaultState: async () => {
const lockStatuses = (
await repo.find({
where: {
scenarioId,
},
})
).map((pu) => pu.lockStatus);
expect(
lockStatuses.every((status) => status === LockStatus.LockedIn),
).toBeTruthy();
},
cleanup: async () => {
await repo.delete({
scenarioId,
});
await app.close();
},
};
};
3 changes: 3 additions & 0 deletions api/apps/api/test/steps/given-scenario-pu-data-exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ export const GivenScenarioPuDataExists = async (
lockStatus: LockStatus.Unstated,
puGeometryId: v4(),
planningUnitMarxanId: 1,
protectedByDefault: true,
}),
repo.create({
scenarioId,
lockStatus: LockStatus.LockedOut,
puGeometryId: v4(),
planningUnitMarxanId: 2,
protectedByDefault: true,
}),
repo.create({
scenarioId,
lockStatus: LockStatus.LockedIn,
puGeometryId: v4(),
planningUnitMarxanId: 3,
protectedByDefault: true,
}),
]);
return {
Expand Down