-
Notifications
You must be signed in to change notification settings - Fork 0
/
decline_reservation.js
121 lines (110 loc) · 3.44 KB
/
decline_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
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
121
const aws = require('aws-sdk');
const moment = require('moment');
const { Expo } = require('expo-server-sdk');
const ReceiptsTableName = process.env.dynamodb_receipts_table_name;
let ReservationsTableName = process.env.dynamodb_reservations_table_name;
let UsersTableName = process.env.dynamodb_users_table_name;
function stagingOverwrite() {
ReservationsTableName = `Dev_${process.env.dynamodb_reservations_table_name}`;
UsersTableName = `Dev_${process.env.dynamodb_users_table_name}`;
}
const dynamoDB = new aws.DynamoDB.DocumentClient();
const expo = new Expo();
async function sendNotification(expoPushToken) {
const message = {
to: expoPushToken,
sound: 'default',
body: 'El conductor no puede llevarte 😞',
data: { action: 'decline', resource: 'reservation' }
};
try {
const ticket = await expo.sendPushNotificationsAsync([message]);
return ticket;
} catch (error) {
return false;
}
}
async function notifyPassenger(reservationId) {
const reservationData = await dynamoDB.get({
TableName: ReservationsTableName,
Key: { reservation_id: reservationId },
ProjectionExpression: 'passenger_id'
}).promise();
const passengerId = reservationData.Item.passenger_id;
const passengerData = await dynamoDB.get({
TableName: UsersTableName,
Key: { user_id: passengerId },
ProjectionExpression: 'expo_push_token'
}).promise();
const expoPushToken = passengerData.Item.expo_push_token;
if (!expoPushToken) { return false; }
const result = await sendNotification(expoPushToken);
return result;
}
async function declineReservation(reservationId) {
const updatedStatus = 'declined';
const timestamp = moment().format('YYYY-MM-DDTHH:mm:ss');
try {
const params = {
TableName: ReservationsTableName,
Key: {
reservation_id: reservationId
},
UpdateExpression: 'set reservation_status = :new_status, updated_at = :now',
ExpressionAttributeValues: {
':new_status': updatedStatus,
':now': timestamp
}
};
await dynamoDB.update(params).promise();
return { reservation_id: reservationId, reservation_status: updatedStatus };
} catch (e) {
return false;
}
}
async function saveReceipt(receiptId) {
const timestamp = moment().format('YYYY-MM-DDTHH:mm:ss');
const params = {
TableName: ReceiptsTableName,
Item: {
receipt_id: receiptId,
receipt_type: 'expo_push_notification',
checked: false,
created_at: timestamp,
updated_at: timestamp
}
};
return dynamoDB.put(params).promise();
}
exports.handler = async (event) => {
if (event.requestContext.stage === 'staging') { stagingOverwrite(); }
const reservationId = event.pathParameters.reservation;
const result = await declineReservation(reservationId);
if (!result) {
return {
statusCode: 409,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'decline',
success: false,
resource: 'reservation',
message: 'Wrong or missing parameters'
})
};
}
const tickets = await notifyPassenger(reservationId);
if (tickets && tickets.length && tickets[0].id) {
const ticketId = tickets[0].id;
await saveReceipt(ticketId);
}
return {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'decline',
success: true,
resource: 'reservation',
resource_id: reservationId
})
};
};