-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
93 lines (82 loc) · 3.11 KB
/
server.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
const http = require('http');
const path = require('path');
const util = require('util');
const express = require('express');
const ws = require('ws');
const session = require('express-session');
const bodyParser = require('body-parser');
const cron = require('cron');
const customerRoutes = require('./routes/customer');
const canteenRoutes = require('./routes/canteen');
const ingredientApiRoutes = require('./routes/api/ingredient');
const itemApiRoutes = require('./routes/api/item');
const orderApiRoutes = require('./routes/api/order');
function run(db) {
const app = express();
// HTTP Server Middleware
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static('./static'));
// HTTP Server Routes
app.get('/', (req, res) => {
res.sendFile('html/temp_home.html', {root: path.join(__dirname, './static/')});
});
const server = http.createServer(app);
const wss = new ws.Server({server});
app.use('/customer', customerRoutes(db, wss));
app.use('/canteen', canteenRoutes(db, wss));
app.use('/api/ingredient', ingredientApiRoutes(db, wss));
app.use('/api/item', itemApiRoutes(db, wss));
app.use('/api/order', orderApiRoutes(db, wss));
wss.broadcast = function (data) {
wss.clients.forEach(client => {
if (client.readyState === ws.OPEN) {
client.send(data);
}
});
};
const inventoryJob = new cron.CronJob({
cronTime: '* * * * *', // Y '00 00 18 * * *' '00 05 * * * *'
onTick: async () => {
const itemsCollection = db.collection('items');
const ingredientsCollection = db.collection('ingredients');
itemsCollection.aggregate = util.promisify(itemsCollection.aggregate);
ingredientsCollection.update = util.promisify(ingredientsCollection.update);
console.log('tick');
try {
const aggrResult = await itemsCollection.aggregate([
{$match: {indailymenu: true}},
{$unwind: '$ingredients'},
{$project: {_id: 1, ingredients: {_id: '$ingredients._id', quantity: {$multiply: ['$ingredients.quantity', 100]}}}},
{$group: {_id: '$ingredients._id', quantity: {$sum: '$ingredients.quantity'}}},
{$lookup: {from: 'ingredients', localField: '_id', foreignField: '_id', as: 'actualItem'}},
{$project: {_id: 1, toSubtract: '$quantity', actualItem: {$arrayElemAt: ['$actualItem', 0]}}},
{$project: {_id: 1, name: '$actualItem.name', price: '$actualItem.price', quantity: {$subtract: ['$actualItem.quantity', '$toSubtract']}}}
]);
await Promise.all(aggrResult.map(ingredientDoc => ingredientsCollection.update({_id: ingredientDoc._id}, ingredientDoc)));
const cursor = ingredientsCollection.find({});
cursor.toArray = util.promisify(cursor.toArray);
const ingredients = await cursor.toArray();
wss.broadcast(JSON.stringify({inventoryUpdate: ingredients}));
} catch (err) {
console.log(err);
}
},
start: false,
timeZone: 'Asia/Kolkata'
});
return new Proxy(server, {
get(target, propKey) {
if (propKey === 'listen') {
inventoryJob.start();
}
return target[propKey];
}
});
}
module.exports = run;