-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancel_reservation.js
62 lines (56 loc) · 1.7 KB
/
cancel_reservation.js
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
const aws = require('aws-sdk');
const moment = require('moment');
let ReservationsTableName = process.env.dynamodb_reservations_table_name;
function stagingOverwrite() {
ReservationsTableName = `Dev_${process.env.dynamodb_reservations_table_name}`;
}
const dynamoDB = new aws.DynamoDB.DocumentClient();
async function cancelReservation(reservationId, userId) {
const timestamp = moment().format('YYYY-MM-DDTHH:mm:ss');
const params = {
TableName: ReservationsTableName,
Key: {
reservation_id: reservationId
},
UpdateExpression: 'set reservation_status = :new_status, updated_at = :now',
ConditionExpression:
'reservation_status = :expectedStatus and passenger_id = :self',
ExpressionAttributeValues: {
':new_status': 'canceled',
':now': timestamp,
':self': userId,
':expectedStatus': 'pending'
}
};
const data = await dynamoDB.update(params).promise();
return data.Item;
}
exports.handler = async (event) => {
if (event.requestContext.stage === 'staging') { stagingOverwrite(); }
const userId = event.requestContext.authorizer.user_id;
const reservationId = event.pathParameters.reservation;
try {
await cancelReservation(reservationId, userId);
} catch (err) {
return {
statusCode: 409,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'cancel',
success: false,
resource: 'reservation',
message: 'Conflict'
})
};
}
return {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'cancel',
success: true,
resource: 'reservation',
resource_id: reservationId
})
};
};