Skip to content

Commit

Permalink
feat(forward_driver_trip): validate trip consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Méndez committed Nov 9, 2019
1 parent 9e3c3c9 commit 74d1b38
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions forward_driver_trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,50 @@ async function forwardTrip(tripId, userId) {
return data.Attributes;
}

async function getTrip(tripId) {
const params = {
TableName: TripsTableName,
Key: {
trip_id: tripId
},
ProjectionExpression: 'current_point, route_points'
};
const data = await dynamoDB.get(params).promise();
return data.Item;
}

exports.handler = async (event) => {
const userId = event.requestContext.authorizer.user_id;
const tripId = event.pathParameters.trip;

const trip = await getTrip(tripId);

if (trip.current_point + 1 === trip.route_points.length) {
return {
statusCode: 409,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'forward',
success: false,
resource: 'trip',
resource_id: tripId,
message: 'Already at the last route point'
})
};
}

const result = await forwardTrip(tripId, userId);
const responseBody = {
action: 'forward',
success: true,
resource: 'trip',
resource_id: tripId,
next_point: result.current_point + 1 < result.route_points.length
? result.route_points[result.current_point + 1]
: null
};
return {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(responseBody)
body: JSON.stringify({
action: 'forward',
success: true,
resource: 'trip',
resource_id: tripId,
next_point: result.current_point + 1 < result.route_points.length
? result.route_points[result.current_point + 1]
: null
})
};
};

0 comments on commit 74d1b38

Please sign in to comment.