-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDistributeDomainEvents.ts
54 lines (46 loc) · 1.61 KB
/
DistributeDomainEvents.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
import { IInvoker, LambdaInvoker } from './LambdaInvoker';
import { DomainEventBase } from '../../domain/events/DomainEventBase';
import { DomainEventTypes } from '../../domain/events/DomainEventTypes';
// Add all process.env used:
const { notifySlackChannel, someWork, createAccount, notifyFE, storeEvent } =
process.env;
if (
!notifySlackChannel ||
!someWork ||
!createAccount ||
!notifyFE ||
!storeEvent
) {
console.log('process.env', process.env);
throw new Error(`Undefined env var!`);
}
const { UserCreatedEvent, TransactionCreatedEvent } = DomainEventTypes;
const subscribers = {
// Set up permissions in MyStack.ts:
// Events/keys: allowEmittingDomainEvents
// Subscribers/values: allowSubscribeToDomainEvents
[UserCreatedEvent]: [storeEvent, notifySlackChannel, someWork, createAccount],
[TransactionCreatedEvent]: [storeEvent, notifyFE],
};
class DistributeDomainEvents {
private invoker: IInvoker;
public constructor() {
this.invoker = new LambdaInvoker();
}
public async execute(event: DomainEventBase) {
console.log('event', event);
console.log(`subscribers[${event.type}]`, subscribers[event.type]);
await Promise.all(
subscribers[event.type].map((lambda) => {
return this.invoker.invoke(event, lambda);
})
);
if (!subscribers[event.type] || !subscribers[event.type].length) {
const msg = `Unexpected event`;
console.log(msg, event, subscribers);
throw new Error(msg);
}
}
}
const distributeDomainEvents = new DistributeDomainEvents();
export const handler = distributeDomainEvents.execute.bind(distributeDomainEvents);