-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (91 loc) · 3.56 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
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
123
124
125
126
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var expressWs = require('express-ws')(app);
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(3000)
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
next();
});
// mongoose instance connection url connection
mongoose.Promise = global.Promise;
const dbConfig = require('./config/database.config.js');
// Connecting to the database
mongoose.connect(dbConfig.url, {
useNewUrlParser: true,
useCreateIndex:true,
useFindAndModify: false,
useUnifiedTopology: true
}).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();
});
require('./app/models/package_model');
require('./app/models/delivery_model');
const Delivery = mongoose.model('Delivery');
const routes = require('./app/routes/router');
app.get('/test',function(req,res){
res.json({message:'Working Properly'});
});
app.use('/api',routes);
app.ws('/ws', function(ws, req) {
ws.on('message', function(msg) {
const message = JSON.parse(msg)
if(message.event === 'status_changed') {
const status = message.status
const delivery_id = message.delivery_id
let data = {
status: status
}
if(status === 'picked-up') {
data['picked-up'] = new Date().toString()
} else if (status === 'in-transit') {
data['start_time'] = new Date().toString()
} else if (status === 'delivered' || status === 'failed') {
data['end_time'] = new Date().toString()
}
Delivery.findByIdAndUpdate({_id: delivery_id}, data, {new: true})
.then(delivery => {
const msgSend = {
event: 'delivery_updated',
delivery_object: delivery
}
ws.broadcast(JSON.stringify(msgSend))
})
.catch(err => {
console.log(err);
});
} else if (message.event === 'location_changed') {
const location = message.location
const delivery_id = message.delivery_id
Delivery.findByIdAndUpdate({_id: delivery_id}, {
location: location
}, {new: true})
.then(delivery => {
const msgSend = {
event: 'delivery_updated',
delivery_object: delivery
}
ws.broadcast(JSON.stringify(msgSend))
})
.catch(err => {
console.error(err);
});
}
});
ws.broadcast = function broadcast(msg) {
expressWs.getWss().clients.forEach(function each(client) {
client.send(msg);
});
};
});
module.exports = app;