-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): prevent creating scenario if project is not ready
- Loading branch information
Showing
9 changed files
with
183 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
api/apps/api/src/modules/scenarios/project-checker.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { In } from 'typeorm'; | ||
import { Test } from '@nestjs/testing'; | ||
import { FixtureType } from '@marxan/utils/tests/fixture-type'; | ||
import { API_EVENT_KINDS } from '@marxan/api-events'; | ||
import { ApiEventsService } from '@marxan-api/modules/api-events'; | ||
import { ProjectChecker } from './project-checker.service'; | ||
|
||
let fixtures: FixtureType<typeof getFixtures>; | ||
|
||
beforeEach(async () => { | ||
fixtures = await getFixtures(); | ||
}); | ||
|
||
it(`should form valid request`, () => { | ||
// given | ||
const service = fixtures.getService(); | ||
// when | ||
service.isProjectReady(`projectId`); | ||
// then | ||
fixtures.ThenShouldAskAllPlanningUnitsStatuses(); | ||
}); | ||
|
||
it.each( | ||
Object.values(API_EVENT_KINDS).filter( | ||
(kind) => | ||
kind !== API_EVENT_KINDS.project__planningUnits__finished__v1__alpha, | ||
), | ||
)(`should return false for %s`, async (kind) => { | ||
// given | ||
const service = fixtures.getService(); | ||
// and | ||
fixtures.fakeApiEventsService.getLatestEventForTopic.mockResolvedValueOnce({ | ||
kind, | ||
timestamp: new Date(), | ||
topic: `projectId`, | ||
}); | ||
// when | ||
const isReady = await service.isProjectReady(`projectId`); | ||
// then | ||
expect(isReady).toBe(false); | ||
}); | ||
|
||
it(`should return true for finished`, async () => { | ||
// given | ||
const service = fixtures.getService(); | ||
// and | ||
fixtures.fakeApiEventsService.getLatestEventForTopic.mockResolvedValueOnce({ | ||
kind: API_EVENT_KINDS.project__planningUnits__finished__v1__alpha as const, | ||
timestamp: new Date(), | ||
topic: `projectId`, | ||
}); | ||
// when | ||
const isReady = await service.isProjectReady(`projectId`); | ||
// then | ||
expect(isReady).toBe(true); | ||
}); | ||
|
||
async function getFixtures() { | ||
const fakeApiEventsService: jest.Mocked< | ||
Pick<ApiEventsService, 'getLatestEventForTopic'> | ||
> = { | ||
getLatestEventForTopic: jest.fn<any, any>(), | ||
}; | ||
const testingModule = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: ApiEventsService, | ||
useValue: fakeApiEventsService, | ||
}, | ||
ProjectChecker, | ||
], | ||
}).compile(); | ||
|
||
return { | ||
fakeApiEventsService, | ||
getService() { | ||
return testingModule.get(ProjectChecker); | ||
}, | ||
ThenShouldAskAllPlanningUnitsStatuses() { | ||
expect(fakeApiEventsService.getLatestEventForTopic).toBeCalledTimes(1); | ||
expect(fakeApiEventsService.getLatestEventForTopic).toBeCalledWith({ | ||
topic: `projectId`, | ||
kind: In( | ||
Object.values(API_EVENT_KINDS) | ||
.filter((kind) => kind.startsWith(`project.planningUnits.`)) | ||
.sort(), | ||
), | ||
}); | ||
}, | ||
}; | ||
} |
25 changes: 25 additions & 0 deletions
25
api/apps/api/src/modules/scenarios/project-checker.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { In } from 'typeorm'; | ||
|
||
import { API_EVENT_KINDS } from '@marxan/api-events'; | ||
import { ApiEventsService } from '@marxan-api/modules/api-events'; | ||
|
||
@Injectable() | ||
export class ProjectChecker { | ||
constructor(private readonly apiEvents: ApiEventsService) {} | ||
|
||
async isProjectReady(projectId: string): Promise<boolean> { | ||
const event = await this.apiEvents.getLatestEventForTopic({ | ||
topic: projectId, | ||
kind: In([ | ||
API_EVENT_KINDS.project__planningUnits__failed__v1__alpha, | ||
API_EVENT_KINDS.project__planningUnits__finished__v1__alpha, | ||
API_EVENT_KINDS.project__planningUnits__submitted__v1__alpha, | ||
]), | ||
}); | ||
return ( | ||
event?.kind === | ||
API_EVENT_KINDS.project__planningUnits__finished__v1__alpha | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters