-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_driver_trip.js
168 lines (151 loc) · 4.35 KB
/
create_driver_trip.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const aws = require('aws-sdk');
const uuidv4 = require('uuid/v4');
const moment = require('moment');
// const UsersTableName = process.env.dynamodb_users_table_name;
const PlacesTableName = process.env.dynamodb_places_table_name;
let TripsTableName = process.env.dynamodb_trips_table_name;
let VehiclesTableName = process.env.dynamodb_vehicles_table_name;
function stagingOverwrite() {
TripsTableName = `Dev_${process.env.dynamodb_trips_table_name}`;
VehiclesTableName = `Dev_${process.env.dynamodb_vehicles_table_name}`;
}
const dynamoDB = new aws.DynamoDB.DocumentClient();
function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
function mapIdKeys(ids, key) {
return ids.map((i) => ({
[key]: i
}));
}
function repeated(value, index, self) {
return self.indexOf(value) === index;
}
async function createTrip(driverId, vehicleId, availableSeats, etdInfo, routePoints) {
const tripId = `tri_${uuidv4()}`;
const timestamp = moment().format('YYYY-MM-DDTHH:mm:ss-04:00');
const params = {
TableName: TripsTableName,
Item: {
trip_id: tripId,
driver_id: driverId,
vehicle_id: vehicleId,
etd_info: etdInfo,
available_seats: availableSeats,
route_points: routePoints,
trip_status: 'open',
current_point: -1,
created_at: timestamp,
updated_at: timestamp
}
};
await dynamoDB.put(params).promise();
return tripId;
}
function alreadyPassed(time) {
return moment(time) < moment();
}
// async function getUser(userId) {
// const params = {
// TableName: UsersTableName,
// Key: {
// user_id: userId
// },
// ProjectionExpression: 'user_verifications.driver_license'
// };
// const data = await dynamoDB.get(params).promise();
// return data.Item;
// }
async function getVehicle(vehicleId) {
const params = {
TableName: VehiclesTableName,
Key: {
vehicle_id: vehicleId
}
};
const data = await dynamoDB.get(params).promise();
return data.Item;
}
async function getPlaces(placeIds) {
const params = {
RequestItems: {
[PlacesTableName]: {
Keys: mapIdKeys(placeIds, 'place_id')
}
}
};
const data = await dynamoDB.batchGet(params).promise();
return data.Responses[PlacesTableName];
}
exports.handler = async (event) => {
if (event.requestContext.stage === 'staging') { stagingOverwrite(); }
const userId = event.requestContext.authorizer.user_id;
const body = JSON.parse(event.body);
const etdInfo = body.etd_info;
const routePoints = body.route_points;
const vehicleId = body.vehicle_id;
const availableSeats = body.available_seats;
if (
!etdInfo || isEmpty(etdInfo) || !etdInfo.etd || alreadyPassed(etdInfo.etd)
|| !routePoints || !(routePoints.length > 0) || !vehicleId || !(availableSeats > 0)
) {
return {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'create',
success: false,
resource: 'trip',
message: 'Wrong or missing parameters'
})
};
}
try {
const points = routePoints.filter(repeated);
const vehicle = await getVehicle(vehicleId);
const places = await getPlaces(points);
if (!vehicle || places.length !== points.length) {
throw new Error('Unexistent resource');
}
} catch (err) {
return {
statusCode: 400,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'create',
success: false,
resource: 'trip',
message: 'Wrong or missing parameters'
})
};
}
// const user = await getUser(userId);
// if (
// !user.user_verifications.driver_license.front
// || !user.user_verifications.driver_license.back
// ) {
// return {
// statusCode: 400,
// headers: { 'Access-Control-Allow-Origin': '*' },
// body: JSON.stringify({
// action: 'create',
// success: false,
// resource: 'trip',
// message: 'Missing driver license verification'
// })
// };
// }
const tripId = await createTrip(
userId, vehicleId, availableSeats, etdInfo, routePoints
);
return {
statusCode: 201,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'create',
success: true,
resource: 'trip',
resource_id: tripId
})
};
};