-
Notifications
You must be signed in to change notification settings - Fork 2
/
cars.ts
120 lines (95 loc) · 3.45 KB
/
cars.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
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
/*
* Copyright (c) 2024 - Restate Software, Inc., Restate GmbH
*
* This file is part of the Restate examples released under the MIT license.
*
* You can find a copy of the license in file LICENSE in the root
* directory of this repository or package, or at
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
*/
import * as restate from "@restatedev/restate-sdk";
import { TerminalError } from "@restatedev/restate-sdk";
import { DeleteItemCommand, DynamoDBClient, PutItemCommand, UpdateItemCommand } from "@aws-sdk/client-dynamodb";
import * as process from "process";
const dynamo = new DynamoDBClient({ endpoint: process.env.AWS_ENDPOINT });
export type CarReserveParams = {
depart_city: string;
depart_time: string;
arrive_city: string;
arrive_time: string;
run_type?: string;
};
const reserve = async (ctx: restate.ObjectContext, event: CarReserveParams) => {
console.log("reserve car:", ctx.key, JSON.stringify(event, undefined, 2));
const carRentalReservationID = ctx.rand.uuidv4();
console.log("carRentalReservationID:", carRentalReservationID);
// Pass the parameter to fail this step
if (event.run_type === "failCarRentalReservation") {
throw new TerminalError("Failed to book the car rental");
}
const put = new PutItemCommand({
TableName: process.env.CARS_TABLE_NAME,
Item: {
pk: { S: ctx.key },
sk: { S: carRentalReservationID },
trip_id: { S: ctx.key },
id: { S: carRentalReservationID },
depart_city: { S: event.depart_city },
depart_time: { S: event.depart_time },
arrive_city: { S: event.arrive_city },
arrive_time: { S: event.arrive_time },
transaction_status: { S: "pending" },
},
});
const result = await ctx.run(() => dynamo.send(put));
console.log("inserted car rental reservation:");
console.log(result);
return { booking_id: carRentalReservationID };
};
type ConfirmParams = { booking_id: string; run_type?: string };
const confirm = async (ctx: restate.ObjectContext, event: ConfirmParams) => {
console.log("confirm car:", ctx.key, JSON.stringify(event, undefined, 2));
// Pass the parameter to fail this step
if (event.run_type === "failCarRentalConfirmation") {
throw new TerminalError("Failed to book the flights");
}
const update = new UpdateItemCommand({
TableName: process.env.CARS_TABLE_NAME,
Key: {
pk: { S: ctx.key },
sk: { S: event.booking_id },
},
UpdateExpression: "set transaction_status = :booked",
ExpressionAttributeValues: {
":booked": { S: "confirmed" },
},
});
const result = await ctx.run(() => dynamo.send(update));
console.log("confirmed car rental reservation:");
console.log(result);
return { booking_id: event.booking_id };
};
type CancelParams = { booking_id: string };
const cancel = async (ctx: restate.ObjectContext, event: CancelParams) => {
console.log("cancel car:", ctx.key, JSON.stringify(event, undefined, 2));
const del = new DeleteItemCommand({
TableName: process.env.CARS_TABLE_NAME,
Key: {
pk: { S: ctx.key },
sk: { S: event.booking_id },
},
});
const result = await ctx.run(() => dynamo.send(del));
console.log("deleted car rental reservation:");
console.log(result);
return {};
};
export const carsObject = restate.object({
name: "cars",
handlers: { reserve, confirm, cancel },
});
export type CarsObject = typeof carsObject;
export const handler = restate
.endpoint()
.bind(carsObject)
.lambdaHandler();