-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (27 loc) · 1.09 KB
/
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
const Sequelize = require("sequelize");
const POSTGRES_CONNECTION_STRING = process.env.POSTGRES_CONNECTION_STRING || "postgres://postgres:password@localhost:6432/postgres";
const drivers = [95, 96, 97, 98, 99, 100];
function getRandomDriver() {
return drivers[Math.floor(Math.random() * 5)];
}
async function assignAgent(order){
try {
var sequelize = new Sequelize(
POSTGRES_CONNECTION_STRING, {}
);
var driverId = getRandomDriver();
var res = await sequelize.query('BEGIN;' +
'INSERT INTO assignment (order_id, driver_id) values (:orderId, :driverId); ' +
'UPDATE orders SET driver_assigned=true WHERE order_id = :orderId ;' +
'COMMIT;',
{ replacements: { orderId: order.order_id, driverId: driverId } }
);
return res;
} catch(e) {
console.log(e);
throw new Error(e);
} finally {
sequelize.close();
}
}
exports.assignAgent = assignAgent;