-
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.
chore(geoprocessing): protected-areas: geo extractor: emit api events
- Loading branch information
Showing
20 changed files
with
314 additions
and
40 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
api/src/migrations/api/1621963684141-ProjectSpecificWdpaEvents.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,18 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class CostSurfaceEvents1621963684141 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
INSERT INTO api_event_kinds (id) values | ||
('project.protectedAreas.submitted/v1/alpha'), | ||
('project.protectedAreas.finished/v1/alpha'), | ||
('project.protectedAreas.failed/v1/alpha'); | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DELETE FROM api_event_kinds WHERE id like 'project.protectedAreas.%';`, | ||
); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
api/src/modules/projects/protected-areas/__mocks__/api-service.fake.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,11 @@ | ||
import { CreateApiEventDTO } from '../../../api-events/dto/create.api-event.dto'; | ||
import { AppInfoDTO } from '../../../../dto/info.dto'; | ||
import { ApiEvent } from '../../../api-events/api-event.api.entity'; | ||
|
||
export class ApiServiceFake { | ||
mock = jest.fn(); | ||
|
||
create(createModel: CreateApiEventDTO, info?: AppInfoDTO): Promise<ApiEvent> { | ||
return this.mock(createModel, info); | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
geoprocessing/src/modules/api-events/api-events-subscriber.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,63 @@ | ||
import { ApiEventsSubscriber } from './api-events-subscriber'; | ||
import { CqrsModule, EventBus } from '@nestjs/cqrs'; | ||
import { Test } from '@nestjs/testing'; | ||
import { ApiEventsService } from './api-events.service'; | ||
import { API_EVENT_KINDS } from './events.enum'; | ||
import { ApiEvent } from './api.event'; | ||
|
||
let eventForwarder: FakeForwarder; | ||
let eventBus: EventBus; | ||
|
||
beforeEach(async () => { | ||
const sandbox = await Test.createTestingModule({ | ||
imports: [CqrsModule], | ||
providers: [ | ||
{ | ||
provide: ApiEventsService, | ||
useClass: FakeForwarder, | ||
}, | ||
ApiEventsSubscriber, | ||
], | ||
}) | ||
.compile() | ||
.then((app) => app.init()); | ||
|
||
eventBus = sandbox.get(EventBus); | ||
eventForwarder = sandbox.get(ApiEventsService); | ||
}); | ||
|
||
describe(`when emitting event`, () => { | ||
beforeEach(async () => { | ||
await eventBus.publish( | ||
new ApiEvent( | ||
'resource', | ||
API_EVENT_KINDS.project__protectedAreas__failed__v1__alpha, | ||
{ 1: 'one' }, | ||
), | ||
); | ||
}); | ||
|
||
it(`should forward it`, () => { | ||
expect(eventForwarder.mock.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
"resource", | ||
"project.protectedAreas.failed/v1/alpha", | ||
Object { | ||
"1": "one", | ||
}, | ||
] | ||
`); | ||
}); | ||
}); | ||
|
||
class FakeForwarder { | ||
mock = jest.fn(); | ||
|
||
async create<T>( | ||
resourceId: string, | ||
kind: API_EVENT_KINDS, | ||
data?: T, | ||
): Promise<void> { | ||
return this.mock(resourceId, kind, data); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
geoprocessing/src/modules/api-events/api-events-subscriber.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,39 @@ | ||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; | ||
import { EventBus } from '@nestjs/cqrs'; | ||
import { Subscription } from 'rxjs'; | ||
import { ApiEventsService } from './api-events.service'; | ||
import { ApiEvent } from './api.event'; | ||
|
||
@Injectable() | ||
export class ApiEventsSubscriber implements OnModuleDestroy { | ||
#sub: Subscription; | ||
private readonly logger = new Logger(ApiEventsSubscriber.name); | ||
|
||
constructor( | ||
private readonly eventBus: EventBus, | ||
private readonly eventForwarder: ApiEventsService, // "repository" | ||
) { | ||
this.#sub = eventBus.subscribe({ | ||
next: async (value) => { | ||
if (value instanceof ApiEvent) { | ||
try { | ||
await this.eventForwarder.create( | ||
value.resourceId, | ||
value.kind, | ||
value.data, | ||
); | ||
} catch (error) { | ||
this.logger.error( | ||
`Unable to emit ApiEvent ${value.resourceId}+${value.kind}`, | ||
error, | ||
); | ||
} | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
onModuleDestroy() { | ||
this.#sub.unsubscribe(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { HttpModule, Module } from '@nestjs/common'; | ||
import { CqrsModule } from '@nestjs/cqrs'; | ||
import { ApiEventsSubscriber } from './api-events-subscriber'; | ||
import { ApiEventsService } from './api-events.service'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [ApiEventsService], | ||
imports: [HttpModule, CqrsModule], | ||
providers: [ApiEventsService, ApiEventsSubscriber], | ||
exports: [ApiEventsService], | ||
}) | ||
export class ApiEventsModule {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IEvent } from '@nestjs/cqrs'; | ||
import { API_EVENT_KINDS } from './events.enum'; | ||
|
||
export class ApiEvent<T extends Record<string, unknown>> implements IEvent { | ||
constructor( | ||
public readonly resourceId: string, | ||
public readonly kind: API_EVENT_KINDS, | ||
public readonly data?: T, | ||
) {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { ApiEventsModule } from './api-events.module'; | ||
export { ApiEvent } from './api.event'; |
Oops, something went wrong.