-
Notifications
You must be signed in to change notification settings - Fork 1
/
on-message.ts
43 lines (37 loc) · 1.45 KB
/
on-message.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
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import { APIGatewayEvent } from "aws-lambda";
import { getApiGatewayManagementApiClient, getDynamoDBClient } from "./utils";
dayjs.extend(utc);
dayjs.extend(timezone);
export async function handler(event: APIGatewayEvent) {
try {
const dynamoDbClient = getDynamoDBClient();
const ApiGatewayManagementApiClient = getApiGatewayManagementApiClient(`${event.requestContext.domainName}/${event.requestContext.stage}`);
const response = await dynamoDbClient.scan({
TableName: "socket-connections",
}).promise();
const connectionIds = response.Items?.map(({ connectionId }) => connectionId) ?? [];
const body = event.body ? JSON.parse(event.body) : {};
const sentDate = dayjs()
.tz("America/Fortaleza")
.format("HH:mm DD/MM/YYYY");
for await (const connectionId of connectionIds) {
await ApiGatewayManagementApiClient.postToConnection({
ConnectionId: connectionId,
Data: JSON.stringify({
...body,
sentDate
})
}).promise();
}
return { statusCode: 200, body: "" };
} catch (error: any) {
console.log(error)
return {
statusCode: 500,
body: JSON.stringify({ message: error.message })
};
}
}