-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderHandler.js
68 lines (59 loc) · 1.98 KB
/
orderHandler.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
63
64
65
66
67
68
const orders = require('./model/orders');
module.exports = (io, socket) => {
console.log('connected success fully' + socket.id)
//load data from mongodB
try {
orders.find((err, result) => {
if (err) {
console.log(err)
return
}
io.emit('message-recive', result)
//console.log(result)
});
} catch (error) {
throw error
}
socket.on('disconnect', () => {
console.log('disconnected')
})
//recive orders
socket.on('kitchen_orders', async (data,callback) => {
console.log(data.fdOrder)
console.log(socket.id)
//add in db and send to user
try {
const mongoData = new orders({
fdShopId: data.fdShopId,
fdOrder: data.fdOrder,
fdOrderStatus: data.fdOrderStatus,
fdOrderType: data.fdOrderType,
})
mongoData.save((err, result) => {
if (err) {
callback('error')
console.log(err.message)
return
}
callback('success')
var sum = result.fdOrder.map(item => item.price * item.qnt ).reduce((prev, next) => prev + next,0);
var newResult = {
"_id":result._id,
"error":false,
"errorCode":"no",
"totalSize":1,
"fdOrderStatus":result.fdOrderStatus,
"fdOrderType":result.fdOrderType,
"totelPrice":sum,
"fdOrder":result.fdOrder
}
console.log(newResult)
io.emit('kitchen_orders_recive', newResult)
});
}
catch (error) {
throw error
console.log(error)
}
})
}