-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.js
122 lines (90 loc) · 3.76 KB
/
push.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
exports.pushMethods = function(server, app){
var sio = require("socket.io");
var logger = require('./libs/logger')
var io = sio.listen(server);
io.sockets.on('connection', function(socket){
//send message to user
socket.on("add order", function(msg) {
logger.info("send message msg body: " + msg.body + " msg sender: " + msg.sender );
let item = JSON.parse(msg.body);
//check if already exists
let existsItem ;
app.locals.items.forEach(function(element) {
if(element.id === item.id){
existsItem = element;
}
})
if(existsItem != undefined ){
existsItem.amount = item.amount
logger.info("^update item: " + existsItem.id);
}
else{
app.locals.items.push(item);
logger.info("**insert item: " + item.id);
}
io.sockets.emit("update orders", JSON.stringify(app.locals.items));
});
socket.on("removeItem", function(msg) {
;
let itemId = msg.body;
let index = app.locals.items.findIndex(function(element){
return element.id === itemId;
});
if(index != -1){
logger.info("remove at index " + index );
app.locals.items.splice(index, 1);
}
logger.info("rest of items " + JSON.stringify(app.locals.items) );
//Emit
io.sockets.emit("update orders", JSON.stringify(app.locals.items));
});
socket.on("doPayment", function(msg) {
let index = app.locals.paymentTable.findIndex(function(element){
return element.paymentFor === msg.paymentFor;
});
//remove if exist
if(index != -1){
app.locals.paymentTable.splice(index, 1);
}
//insert
app.locals.paymentTable.push(msg);
logger.info("payment items " + JSON.stringify(app.locals.paymentTable) );
io.sockets.emit("update payment", JSON.stringify(app.locals.paymentTable));
});
function removeByIndex(array, index){
return array.filter(function(elem, _index){
return index != _index;
});
}
function removeByValue(array, value){
return array.filter(function(elem, _index){
return value != elem;
});
}
socket.on("removePayment", function(msg) {
let index = app.locals.paymentTable.findIndex(function(element){
return element.paymentFor === msg.paymentFor;
});
//remove if exist
if(index != -1){
logger.info("remove index " + index + " item: " + JSON.stringify(app.locals.paymentTable[index]));
app.locals.paymentTable = removeByIndex(app.locals.paymentTable, index);
}
//remove all items for it
let itemsToRemove = [];
app.locals.items.forEach(function(element) {
if(element.nick === msg.paymentFor){
itemsToRemove.push(element);
}
});
itemsToRemove.forEach(function(element) {
app.locals.items = removeByValue(app.locals.items, element);
logger.info("remove item " + JSON.stringify(element) + " By: " + msg.approvedBy);
});
//Emit
io.sockets.emit("update payment", JSON.stringify(app.locals.paymentTable));
io.sockets.emit("update orders", JSON.stringify(app.locals.items));
});
});
return io;
}