-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_vendor_selector.js
54 lines (45 loc) · 1.59 KB
/
best_vendor_selector.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
var dbfun = require('./dbfun');
var wait= require('wait.for');//aync modules
setTimeout(function(){
wait.launchFiber(function(){//running async code in sync
var orders = wait.for(dbfun.getUnProcessedOrders);
for(var i=0;i<orders.length;i++){
var customer = wait.for(dbfun.findCustomerById,orders[i].ordered_by);//find the customer who has given the order
//find customers current location
var curr_loc=[];
curr_loc.push(customer.current_lon);
curr_loc.push(customer.current_lat);
//find items in the order
var items=[];
for(var j=0;j<orders[i].items.length;j++){
items.push(orders[i].items[j].id);
}
//get users max walk distance
var max_walk_dist=customer.max_walk_dist;
var final_vendors=[];
if(orders[i].order_mode==0){
final_vendors=wait.for(dbfun.getPickUpVendors,curr_loc,items,max_walk_dist);
if(JSON.stringify(final_vendors)!='[]'){
console.log("\n\nThe Pick Up order "+i+" can be picked up from following vendors ");
for(var j=0;j<final_vendors.length;j++){
console.log(final_vendors[j].name+" distance = "+Math.ceil(final_vendors[j].distance));
}
}
else{
console.log("\n\norder "+i+" cannot be serviced");
}
}
else{
final_vendors=wait.for(dbfun.getHomeDelVen,curr_loc,items);
if(JSON.stringify(final_vendors)!='[]'){
console.log("\n\nThe Home delivery order "+i+" will be served by follwing vendor ");
console.log(final_vendors[0].name+" distance = "+Math.ceil(final_vendors[0].distance));
}
else{
console.log("\n\norder "+i+" cannot be serviced");
}
}
}
});
},10000);
//