-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgift-card.command-gateway.ts
42 lines (40 loc) · 1.22 KB
/
gift-card.command-gateway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Injectable, Logger } from '@nestjs/common';
import { GiftCardCommand } from '../api/gift-card.commands';
import { GiftCardEvent } from '../api/gift-card.events';
import { AxonClient } from '../../axon.client';
import { ConfigService } from '@nestjs/config';
import { GiftCardQuery } from '../api/gift-card.queries';
/**
* *** ADAPTER LAYER ***
* ___
* A command gateway
* ___
* A component responsible for publishing commands to Axon Synapse, over HTTP protocol
*/
@Injectable()
export class GiftCardCommandGateway {
private readonly logger = new Logger(GiftCardCommandGateway.name);
constructor(
private readonly axonClient: AxonClient<
GiftCardCommand,
GiftCardEvent,
GiftCardQuery
>,
private readonly configService: ConfigService,
) {}
async publishCommand(
c: GiftCardCommand,
contextProvider: (c: GiftCardCommand) => string = () =>
this.configService.get<string>('AXON_CONTEXT', 'default'),
): Promise<readonly [GiftCardEvent, number][]> {
this.logger.log(
`dispatching command ${c.kind} with body ${JSON.stringify(c)}`,
);
return await this.axonClient.publishCommand(
c,
(c) => c.kind,
(c) => c.id,
contextProvider,
);
}
}