-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
451 lines (413 loc) · 13.2 KB
/
api.ts
File metadata and controls
451 lines (413 loc) · 13.2 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import * as DynamoDbClient from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
import * as EventBridgeClient from "@aws-sdk/client-eventbridge";
import { v4 as uuid } from "uuid";
import * as Application from "./application";
import * as Domain from "./domain";
const docClient = new DynamoDbClient.DynamoDB({});
const eventBridgeClient = new EventBridgeClient.EventBridge({});
/**
* Represents the event data for creating an order.
*/
export type CreateOrderEvent = {
amount: number;
customerId: string;
};
/**
* Handles the creation of an order.
* @param {CreateOrderEvent} event - The event data for creating an order.
* @returns {Promise<void>} A promise that resolves when the order creation is complete.
*/
export async function createOrderLambda(
event: CreateOrderEvent
): Promise<void> {
// setup application dependencies.
const unitOfWork = createUnitOfWork(docClient);
const ordersRepository = createRepository<Domain.Order>(
process.env.ORDERS_TABLE_NAME!,
unitOfWork,
docClient
);
const integrationEventService: Application.IIntegrationEventService =
createIntegrationEventService(unitOfWork, docClient, eventBridgeClient);
const createOrderService = Application.buildCreateOrderService(
unitOfWork,
ordersRepository,
integrationEventService
);
// execute the application workflow.
await createOrderService({
amount: event.amount,
customerId: event.customerId,
});
await integrationEventService.publish(unitOfWork.transactionId);
return;
}
/**
* Represents the event data for reserving credit.
*/
export type ReserveCreditEvent = {
amount: number;
orderId: string;
customerId: string;
};
/**
* Handles the reservation of credit.
* @param {ReserveCreditEvent} event - The event data for reserving credit.
* @returns {Promise<void>} A promise that resolves when the credit reservation is complete.
*/
export async function reserveCreditLambda({
detail: event,
}: {
detail: ReserveCreditEvent;
}): Promise<void> {
// setup application dependencies.
const unitOfWork = createUnitOfWork(docClient);
const transactionsRepository: Domain.IRepository<Domain.Transaction> =
createRepository(
process.env.TRANSACTIONS_TABLE_NAME!,
unitOfWork,
docClient
);
const integrationEventService: Application.IIntegrationEventService =
createIntegrationEventService(unitOfWork, docClient, eventBridgeClient);
const reserveCreditService = Application.buildReserveCreditService(
unitOfWork,
transactionsRepository,
integrationEventService
);
// execute the application workflow.
await reserveCreditService({
amount: event.amount,
orderId: event.orderId,
customerId: event.customerId,
});
await integrationEventService.publish(unitOfWork.transactionId);
return;
}
/**
* Represents the event data for handling reservation outcome.
*/
export type HandleReservationOutcomeEvent = {
orderId: string;
isCreditReserved: boolean;
};
/**
* Handles the reservation outcome.
* @param {HandleReservationOutcomeEvent} event - The event data for handling reservation outcome.
* @returns {Promise<void>} A promise that resolves when the reservation outcome is handled.
*/
export async function handleReservationOutcomeLambda({
detail: event,
}: {
detail: HandleReservationOutcomeEvent;
}): Promise<void> {
// setup application dependencies.
const unitOfWork = createUnitOfWork(docClient);
const ordersRepository: Domain.IRepository<Domain.Order> = createRepository(
process.env.ORDERS_TABLE_NAME!,
unitOfWork,
docClient
);
const integrationEventService: Application.IIntegrationEventService =
createIntegrationEventService(unitOfWork, docClient, eventBridgeClient);
const reserveCreditService = Application.buildHandleReservationOutcomeService(
unitOfWork,
ordersRepository
);
// execute the application workflow.
await reserveCreditService({
orderId: event.orderId,
isCreditReserved: event.isCreditReserved,
});
await integrationEventService.publish(unitOfWork.transactionId);
return;
}
/**
* Represents a unit of work for performing a batch of operations.
*/
type DynamoDbUnitOfWork = Domain.IUnitOfWork & {
/**
* Registers an operation to be included in the unit of work.
* @param {DynamoDbClient.TransactWriteItem[]} operation - The operation details to register.
* @returns {void}
*/
register: (operation: DynamoDbClient.TransactWriteItem[]) => void;
};
/**
* Creates a unit of work for interacting with DynamoDB and performing batch operations.
* @param {DynamoDbClient.DynamoDB} docClient - The DynamoDB client instance.
* @returns {DynamoDbUnitOfWork} The created unit of work.
*/
function createUnitOfWork(
docClient: DynamoDbClient.DynamoDB
): DynamoDbUnitOfWork {
const transactionId = uuid();
const operations: Array<DynamoDbClient.TransactWriteItem> = [];
return {
transactionId,
register: (operation: DynamoDbClient.TransactWriteItem[]): void => {
operations.push(...operation);
console.debug(
`Operation added to unit of work ${JSON.stringify(operation)}`
);
},
commit: async (): Promise<void> => {
try {
const data = await docClient.transactWriteItems({
TransactItems: operations,
});
console.log("UnitOfWork committed successfully:", data);
} catch (error) {
console.error("Error committing UnitOfWork:", error);
throw error;
}
},
};
}
/**
* Creates a repository for performing operations on a specific table.
* @param {string} tableName - The name of the table.
* @param {DynamoDbUnitOfWork} unitOfWork - The unit of work associated with the repository.
* @param {DynamoDbClient.DynamoDB} docClient - The DynamoDB client instance.
* @returns {Domain.IRepository<T>} The created repository.
*/
function createRepository<T extends Domain.Entity>(
tableName: string,
unitOfWork: DynamoDbUnitOfWork,
docClient: DynamoDbClient.DynamoDB
): Domain.IRepository<T> {
function convertObjectToDynamoDBUpdate(obj: any): any {
const updateExpressionParts: string[] = [];
const expressionAttributeValues: any = {};
const expressionAttributeNames: any = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
const attributeName = `#${key}`;
const attributeValue = `:${key}`;
updateExpressionParts.push(`${attributeName} = ${attributeValue}`);
expressionAttributeNames[attributeName] = key;
expressionAttributeValues[attributeValue] = marshall(value);
}
}
const updateExpression = `SET ${updateExpressionParts.join(", ")}`;
return {
UpdateExpression: updateExpression,
ExpressionAttributeValues: expressionAttributeValues,
ExpressionAttributeNames: expressionAttributeNames,
};
}
return {
add: (entity: Domain.Entity): void => {
entity.version = 1;
unitOfWork.register([
{
Put: {
TableName: tableName,
Item: marshall(entity),
},
},
]);
},
update: (entity: Domain.Entity): void => {
const { id, ...entityValues } = entity;
entityValues.version++;
console.info(convertObjectToDynamoDBUpdate(entityValues));
const {
UpdateExpression,
ExpressionAttributeValues,
ExpressionAttributeNames,
} = convertObjectToDynamoDBUpdate(entityValues);
unitOfWork.register([
{
Update: {
TableName: tableName,
Key: { id: { S: id } },
UpdateExpression: UpdateExpression,
ConditionExpression: "#version = :currentVersion",
ExpressionAttributeNames: {
"#version": "version",
...ExpressionAttributeNames,
},
ExpressionAttributeValues: {
":currentVersion": { N: `${entityValues.version - 1}` },
...ExpressionAttributeValues,
},
},
},
]);
},
get: async (id: string): Promise<T | null> => {
const params: DynamoDbClient.GetItemCommandInput = {
TableName: tableName,
Key: {
id: {
S: id,
},
},
};
try {
const result = await docClient.getItem(params);
if (result.Item == null) {
return null;
}
return unmarshall(result.Item) as T;
} catch (error) {
console.error("Error retrieving item from the repository:", error);
throw error;
}
},
};
}
/**
* Creates an integration event service for handling integration events in DynamoDB.
* @param {DynamoDbUnitOfWork} unitOfWork - The unit of work associated with the integration event service.
* @param {DynamoDbClient.DynamoDB} docClient - The DynamoDB client instance.
* @returns {Application.IIntegrationEventService} The created integration event service.
*/
function createIntegrationEventService(
unitOfWork: DynamoDbUnitOfWork,
docClient: DynamoDbClient.DynamoDB,
eventBridgeClient: EventBridgeClient.EventBridge
): Application.IIntegrationEventService {
const tableName = process.env.INTEGRATION_EVENTS_TABLE_NAME!;
async function retrieveEventLogsPendingToPublish(
transactionId: string
): Promise<any[]> {
const params: DynamoDbClient.ScanCommandInput = {
TableName: tableName,
FilterExpression: "#status = :status AND transactionId = :transactionId",
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": {
S: "NOT_PUBLISHED",
},
":transactionId": {
S: transactionId,
},
},
};
try {
const result = await docClient.scan(params);
if (result.Items) {
return result.Items.map((item) => unmarshall(item));
} else {
return [];
}
} catch (error) {
console.error("Error fetching integration events:", error);
throw error;
}
}
async function markEventAsPublished(eventId: string): Promise<void> {
await updateEventStatus(eventId, "PUBLISHED");
}
async function markEventAsInProgress(eventId: string): Promise<void> {
await updateEventStatus(eventId, "IN_PROGRESS");
}
async function markEventAsFailed(eventId: string): Promise<void> {
await updateEventStatus(eventId, "FAILED");
}
async function updateEventStatus(
eventId: string,
status: string
): Promise<void> {
const params: DynamoDbClient.UpdateItemCommandInput = {
TableName: tableName,
Key: { id: { S: eventId } },
UpdateExpression: "SET #status = :status",
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":status": {
S: status,
},
},
};
try {
await docClient.updateItem(params);
} catch (error) {
console.error("Error updating integration event status:", error);
throw error;
}
}
return {
add<T>(event: Application.IntegrationEvent<T>): void {
unitOfWork.register([
{
Put: {
TableName: tableName,
Item: marshall({
...event,
payload: JSON.stringify(event.payload),
id: uuid(),
status: "NOT_PUBLISHED",
transactionId: unitOfWork.transactionId,
}),
},
},
]);
},
publish: async (transactionId: string): Promise<void> => {
try {
const events = await retrieveEventLogsPendingToPublish(transactionId);
await Promise.allSettled(
events.map(async (item) => {
try {
await markEventAsInProgress(item.id);
await retryWithExponentialBackoff(() => {
var event = {
EventBusName: process.env.INTEGRATION_EVENTS_EVENT_BUS!,
Source: process.env.INTEGRATION_EVENTS_SOURCE!,
DetailType: item.name,
Detail: item.payload,
};
console.debug(`Publishing event: ${JSON.stringify(event)}`);
return eventBridgeClient.putEvents({
Entries: [event],
});
});
console.debug(`Publish event with id ${item.id}`);
await markEventAsPublished(item.id);
} catch (err) {
console.error(`Error publishing integration event ${item.id}`);
await markEventAsFailed(item.id);
}
})
);
} catch (error) {
console.error("Error publishing integration events:", error);
throw error;
}
},
};
}
const retryWithExponentialBackoff = async (
func: () => Promise<any>,
maxRetries: number = 5,
baseDelay: number = 1000,
maxDelay: number = 5000
): Promise<any> => {
let retries = 0;
let delay = baseDelay;
while (retries < maxRetries) {
try {
return await func();
} catch (err) {
console.error(err);
if (retries === maxRetries - 1) {
throw err;
}
retries++;
await sleep(delay);
delay = Math.min(delay * 2, maxDelay);
}
}
};
const sleep = (delay: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, delay));
};