-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
62 lines (54 loc) · 1.5 KB
/
index.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
import { randomUUID } from 'node:crypto';
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import type { Context } from 'aws-lambda';
import type { Request, Response, SubscriptionResult } from './types.js';
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
PutCommand,
} from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const dynamo = DynamoDBDocumentClient.from(client);
const persistenceStore = new DynamoDBPersistenceLayer({
tableName: process.env.IDEMPOTENCY_TABLE_NAME as string,
});
const createSubscriptionPayment = async (
event: Request
): Promise<SubscriptionResult> => {
const id = randomUUID();
await dynamo.send(
new PutCommand({
TableName: process.env.PAYMENT_TABLE_NAME,
Item: {
id: id,
productid: event.productId,
user: event.user,
},
})
);
return {
id: id,
productId: event.productId,
};
};
export const handler = makeIdempotent(
async (event: Request, _context: Context): Promise<Response> => {
try {
const payment = await createSubscriptionPayment(event);
const body = {
message: 'Payment created',
paymentId: payment.id,
};
return {
statusCode: 200,
body: JSON.stringify(body),
};
} catch (error) {
throw new Error('Error creating payment');
}
},
{
persistenceStore,
}
);