-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (91 loc) · 2.57 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
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
const loki = require('lokijs')
,db = new loki('./data/data.json',{})
,express = require('express')
,app = express()
,moment = require('./src/moment.min.js')
,bodyParser = require('body-parser');
;
let server = app.listen(9001, function(){
console.log('server is running on port 9001')
});
io = require('socket.io')(server);
app.use(express.static('dist'));
io.on('connection', function(socket) {
// console.log(socket.id+' connected')
})
app.use(bodyParser.json());
// app.use(bodyParser());
db.loadDatabase({},()=> {
let base = db.getCollection('base')
io.on('connection', (client) => {
// get all
client.on('start',() => {
io.sockets.emit('start',base.find())
// console.log(base.find())
})
// edit one
client.on('update',(data) => {
let obj = base.findOne({['$loki']:data.$loki });
if (obj) {
obj = data
base.update(obj);
db.saveDatabase();
}
})
// delete one
client.on('delete',(id) => {
let obj = base.findOne({['$loki']:id });
if (obj) {
base.remove(obj);
db.saveDatabase();
}
})
// add one
client.on('add',(data) => {
base.insert(data)
db.saveDatabase();
})
// test request
client.on('request',(req)=>{
// console.log(data)
let res = get_rate(req)
io.sockets.emit('result',res)
})
})
// http api
app.post('/get_rate', function(request, response){
let res = get_rate(request.body)
response.send('{"result":'+res+'}')
});
// example
// { "date": "25.01.2018",
// "prog": "01.01.00",
// "targ": "155",
// "cat_id": "62",
// "term": "8",
// "amount": "1500000",
// "ask_kds": false,
// "ask_zp": false }
// make decision
function get_rate(req) {
res = base.find().filter((el) =>
moment(req.date, 'DD.MM.YYYY', true).isBetween(moment(el.dstart, 'DD.MM.YYYY', true), moment(el.dend, 'DD.MM.YYYY', true), 'days', '[]')
&& el.prog_list.includes(req.prog)
&& el.targ_list.includes(req.targ)
&& el.cat_id.includes(req.cat_id)
&& req.term >= el.srokmin && req.term <= el.srokmax
&& req.amount >= el.sum_min && req.amount <= el.sum_max
);
console.log(req)
console.log(base.find())
// беру только первое вхождение
let obj = res[0]
// если решение не нашлось = 0
if (!obj) {
return 0
}
if (req.ask_kds) {obj.kds_disc = 0} else {obj.kds_disc = obj.kds_rate}
if (req.ask_zp) {obj.zp_disc = 0} else {obj.zp_disc = obj.zp_rate}
return obj.rate + obj.kds_disc + obj.zp_disc
}
})