-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (99 loc) · 3.25 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
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
import express from 'express';
import bodyParser from 'body-parser';
import Order from './models/order';
import mongoose from 'mongoose';
const http = require('http').Server(app);
const io = require('socket.io')(http);
mongoose.Promise = global.Promise; mongoose.connect("mongodb://localhost:27017/freshly-picking");
const app = express();
const port = process.env.PORT || 5656;
// routes go here
app.listen(port, () => {
console.log(`http://localhost:${port}`)
})
const Enums = {
Status: {
ENVIADO: 4,
ACEPTADO: 2
}
};
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/api/order', (req, res) => {
// do something
var order = req.body;
var datet = new Date();
console.log(`[${datet}] Incoming request to ${req.route.path}`);
console.log(order.id_order);
if (order.id_order) {
// check if order exists in database
var query = Order.find({ id_order: order.id_order });
query.exec(function (err, retrievedOrders) {
if (retrievedOrders.length) {
let retrievedOrder = new Order(retrievedOrders[0]); // Should only be one, another options is to use Order.findOneBy
// Order exist, lets check the status
if (order.status === Enums.Status.ENVIADO || order.status === Enums.Status.ACEPTADO) {
console.log('############ Order found');
console.log(retrievedOrder);
// update saved order with new order info
Order.findOneAndUpdate({ _id: retrievedOrder.id }, order, function (error, orderUpdated) {
if (error) return handleError(error, res);
res.status(200).send({
success: 'true',
message: 'Order updated successfully',
data: { id_order: orderUpdated.id_order }
})
});
} else {
Order.findByIdAndRemove(retrievedOrder.id, function (error) {
if (error) return handleError(error, res);
res.status(200).send({
success: 'true',
message: 'Order deleted successfully',
data: { id_order: retrievedOrder.id_order }
})
});
}
} else {
// Order do not exist, check status
// Only insert Status Enviados y Aceptados (case study requirement)
if (order.status === Enums.Status.ENVIADO || order.status === Enums.Status.ACEPTADO) {
console.log('############ Order NOT found');
console.log(order);
let newOrder = new Order(order);
newOrder.save();
res.status(200).send({
success: 'true',
message: 'Order saved successfully',
data: newOrder
})
}
}
});
} else {
res.status(500).send({
success: 'false',
message: 'Malformed order, missing order id',
data: order,
});
}
// Emit orders
Order.find({}, function (err, orders) {
io.emit("orders", orders)
});
});
// Just to check in the navigator if it works
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.get('/api/getOrders', function (req, res) {
Order.find({}, function (err, orders) {
res.send(orders);
});
});
function handleError(error, res) {
res.status(500).send({
success: 'false',
message: 'error'
});
}