Skip to content

Commit

Permalink
fix(slots): create slot
Browse files Browse the repository at this point in the history
The slot can be created only if there are available seats on the trip
  • Loading branch information
brunocalderon committed Oct 21, 2019
1 parent 210cfef commit 8df1711
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions create_slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,48 @@ const moment = require('moment');
async function createSlot(tripId, userId) {
let slotId = 'slo_'+uuidv4();
let createdAt = moment().format('YYYY-MM-DDTHH:mm:ss-04:00');
let data = await dynamoDB.transactWrite({
TransactItems: [
{
Put: {
TableName: process.env.dynamodb_table_name_slots,
Item: {
"slot_id": slotId,
"trip_id": tripId,
"user_id": userId,
"slot_status": "requested",
"created_at": createdAt
}
}
},
{
Update: {
TableName: process.env.dynamodb_table_name_trips,
Key: {
"trip_id": tripId
},
UpdateExpression: "set available_seats = available_seats - :requested_seats",
ExpressionAttributeValues: {
":requested_seats": 1
}
}
}
]
}).promise();
return data;
try {
await dynamoDB.transactWrite({
TransactItems: [
{
ConditionCheck: {
TableName: process.env.dynamodb_table_name_trips,
Key: {
"trip_id": tripId
},
ConditionExpression: "available_seats >= :available_seats",
ExpressionAttributeValues: {
":available_seats": 1,
}
}
},
{
Put: {
TableName: process.env.dynamodb_table_name_slots,
Item: {
"slot_id": slotId,
"trip_id": tripId,
"user_id": userId,
"slot_status": "requested",
"created_at": createdAt
}
}
}
]
}).promise();
return true;
}
catch(e) {
return false;
}
}

exports.handler = async (event) => {
let tripId = event.trip_id;
let userId = event.user_id;

await createSlot(tripId, userId);

let result = {
created: true
created: await createSlot(tripId, userId)
};

return result;
Expand Down

0 comments on commit 8df1711

Please sign in to comment.