-
Notifications
You must be signed in to change notification settings - Fork 6
/
delivery-handler.ts
38 lines (31 loc) · 1.1 KB
/
delivery-handler.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
import pino from 'pino'
import { v4 as uuidv4 } from 'uuid'
import { EventBridgeHandler } from 'aws-lambda'
import { Tracer } from '@aws-lambda-powertools/tracer'
import { EventSender } from './event-util'
import { middify } from './lambda-common'
const { SERVICE_IDENTIFIER } = process.env
if (!SERVICE_IDENTIFIER) {
throw new Error('SERVICE_IDENTIFIER env var is required')
}
const log = pino({ name: SERVICE_IDENTIFIER })
const tracer = new Tracer({ serviceName: SERVICE_IDENTIFIER })
const eventSender = new EventSender(SERVICE_IDENTIFIER, tracer)
/**
* Order Delivery processing - handle EventBridge events for Order.Created
* and emit a Delivery.UpdatedEvent
*/
export const handleOrderCreated: EventBridgeHandler<string, any, any> = middify(async function (event, context) {
log.info({ event })
const order = event.detail.data
// Sleep to simulate some delivery processing
await new Promise((resolve) => {
setTimeout(resolve, 5000)
})
const deliveryUpdate = {
order,
deliveredAt: Date.now(),
deliveryId: uuidv4()
}
await eventSender.send('Delivery.Updated', deliveryUpdate)
})