-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharge.ts
More file actions
32 lines (30 loc) · 973 Bytes
/
charge.ts
File metadata and controls
32 lines (30 loc) · 973 Bytes
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
import type { Handler, Context, Callback } from "aws-lambda";
import { chargeInternal } from "./src/table/balance.js";
import { publishChargeAlgolia } from "./src/publish/algolia.js";
const MS_PER_MINA = 200_000;
export const run: Handler = async (
event: any,
context: Context,
callback: Callback
) => {
console.log("charge", event);
const {
id,
billedDuration,
jobId,
}: { id: string; billedDuration: number; jobId: string } = event;
console.log("charge", { id, billedDuration, jobId });
if (id === undefined || billedDuration === undefined || jobId === undefined) {
console.error("Missing required parameters for charge function");
return 200;
}
try {
const amount = billedDuration / MS_PER_MINA;
await chargeInternal({ id, amount });
await publishChargeAlgolia({ id, billedDuration, jobId, amount });
return 200;
} catch (error) {
console.error("catch", (error as any).toString());
return 200;
}
};