-
Notifications
You must be signed in to change notification settings - Fork 3
/
new-order.js
62 lines (48 loc) · 1.61 KB
/
new-order.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
const mysql = require("mysql2/promise");
exports.handler = async function(context, event, callback) {
function fulfillmentCreator (txt, ...args){
let a = {
"fulfillment_response": {
"messages": [
{
"text": {
"text": [txt]
}
}
]
},
"sessionInfo":{
"parameters": {
}
}
}
if (args) {
for (let i = 0; i < args.length; i += 2) {
a.sessionInfo.parameters[args[i]] = args[i + 1];
}
}
return a;
}
const config = {
host: context.host,
port: context.port,
user: context.user,
database: context.databaseName,
};
let orderName = `"${event.sessionInfo.parameters.item}"`;
let shippingTime = Math.floor(Math.random() * 365);
let price = 5;
const sql2 = `INSERT INTO orders (name, price, time) VALUES (${orderName}, ${price}, ${shippingTime})` //, customer ${phoneNumber}
try{
const db = await mysql.createConnection(config);
const [confirmation] = await db.execute(sql2);
await db.end(); // Close the connection
console.log(confirmation);
let txt = `Your order for a ${orderName} will arrive in ${shippingTime} days. Your confirmation number is ${confirmation.insertId}.`
const jsonResp = fulfillmentCreator(txt, "order-id", confirmation.insertId, price);
return callback(null, jsonResp);
} catch (err){
console.log(err)
return callback(null, err);
}
}