-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgift-card.command-handler.aggregate.controller.ts
79 lines (76 loc) · 2.11 KB
/
gift-card.command-handler.aggregate.controller.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
Controller,
Post,
Body,
Headers,
Logger,
OnModuleInit,
} from '@nestjs/common';
import { GiftCardCommand } from '../api/gift-card.commands';
import { GiftCardAggregate } from './gift-card.command-handler.aggregate';
import { AXONIQ_COMMANDNAME } from '../../axon.client';
import { AxonClient } from '../../axon.client';
import { GiftCardEvent } from '../api/gift-card.events';
import { GiftCardQuery } from '../api/gift-card.queries';
/**
* *** ADAPTER LAYER ***
* ___
* A command controller
* ___
* Callback entry point for all gift card commands that are published/posted by the Axon Server/Axon Synapse
*/
@Controller('commands')
export class GiftCardCommandHandlerController implements OnModuleInit {
private readonly logger = new Logger(GiftCardCommandHandlerController.name);
constructor(
private readonly giftCardAggregate: GiftCardAggregate,
private readonly axonClient: AxonClient<
GiftCardCommand,
GiftCardEvent,
GiftCardQuery
>,
) {}
/**
* Register the command handler for the `default` context - on module initialization
*/
async onModuleInit(): Promise<void> {
return await this.axonClient
.registerCommandHandler(
'7ec558ec-90c4-4d51-b444-a028830257bb',
[
'IssueGiftCardCommand',
'RedeemGiftCardCommand',
'CancelGiftCardCommand',
],
'giftcard-demo-1',
'Giftcard',
'/commands',
)
.then((response) => {
this.logger.log(
`registered command handlers with response ${JSON.stringify(
response,
)}`,
);
});
}
/**
* Handle the command published by AxonServer / AxonSynapse - a callback endpoint
* @param request
* @param headers
*/
@Post()
async handle(
@Body() request: GiftCardCommand,
@Headers() headers: Record<string, string>,
) {
this.logger.log(
`received command ${
headers[AXONIQ_COMMANDNAME]
} with body ${JSON.stringify(request)} and headers ${JSON.stringify(
headers,
)}`,
);
return await this.giftCardAggregate.handle(request);
}
}