-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(get_passenger_trips): add get trips as passenger function
- Loading branch information
Franco Méndez
committed
Oct 24, 2019
1 parent
4f46198
commit 0727c46
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const aws = require('aws-sdk'); | ||
|
||
// eslint-disable-next-line import/no-absolute-path | ||
const bearerToUserId = require('/opt/nodejs/bearer_to_user_id.js'); | ||
|
||
const dynamoDB = new aws.DynamoDB.DocumentClient(); | ||
|
||
async function getReservations(userId) { | ||
const params = { | ||
TableName: process.env.dynamodb_reservations_table_name, | ||
IndexName: process.env.dynamodb_index_name, | ||
KeyConditionExpression: 'passenger_id = :userId', | ||
ExpressionAttributeValues: { | ||
':userId': userId | ||
} | ||
}; | ||
const data = await dynamoDB.query(params).promise(); | ||
return data.Items; | ||
} | ||
|
||
async function getTrip(tripId) { | ||
const params = { | ||
TableName: process.env.dynamodb_trips_table_name, | ||
Key: { | ||
trip_id: tripId | ||
}, | ||
ProjectionExpression: 'trip_id, route_points, trip_status, etd' | ||
}; | ||
const data = await dynamoDB.get(params).promise(); | ||
return data.Item; | ||
} | ||
|
||
async function getTrips(reservations) { | ||
return Promise.all( | ||
reservations.slice(0, 15).map(async (r) => getTrip(r.trip_id)) | ||
); | ||
} | ||
|
||
exports.handler = async (event) => { // eslint-disable-line no-unused-vars | ||
const userId = await bearerToUserId.bearerToUserId(event.headers.Authorization.substring(7)); | ||
|
||
const reservations = await getReservations(userId); | ||
const allPassengerTrips = await getTrips(reservations); | ||
|
||
const response = { | ||
statusCode: 200, | ||
headers: { 'Access-Control-Allow-Origin': '*' }, | ||
body: JSON.stringify(allPassengerTrips) | ||
}; | ||
return response; | ||
}; |