/
index.js
47 lines (39 loc) · 913 Bytes
/
index.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
const AWS = require("aws-sdk")
const ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const TABLE_NAME = process.env.TABLE_NAME
class BookFlightError extends Error {
constructor(message) {
super(message)
this.name = "BookFlightError"
}
}
exports.handler = async (event) => {
const {
confirmation_id,
checkin_date,
checkout_date
} = event
var params = {
TableName: TABLE_NAME,
Item: {
'id': { S: confirmation_id },
'checkin_date': { S: checkin_date },
'checkout_date': { S: checkout_date }
}
};
try {
await ddb.putItem(params).promise();
console.log('Success')
} catch (error) {
console.log('Error: ', error)
throw new Error("Unexpected Error")
}
if (confirmation_id.startsWith("22")) {
throw new BookFlightError("Expected Error")
}
return {
confirmation_id,
checkin_date,
checkout_date
};
};