-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (173 loc) · 4.8 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());
// Connection to mongodb
const mongoose = require("mongoose");
mongoose.connect(
"mongodb://localhost/drugs-stock",
{ useNewUrlParser: true }
);
// Declare models/collections
const DrugModel = mongoose.model("Drug", {
name: String,
quantity: Number
});
const TraceModel = mongoose.model("Trace", {
timeStamp: Number,
drugId: String,
command: String,
changeInfo: String
});
// Add a new drug to stock
app.post("/create", async (req, res) => {
const drugDb = await DrugModel.findOne({ name: req.body.name });
if (drugDb) {
return res.status(400).json({
error: {
message: "Drug already exists"
}
});
}
const newDrug = new DrugModel({
name: req.body.name,
quantity: req.body.quantity
});
const drug = await newDrug.save();
let drugResponse = {};
drugResponse.id = drug.id;
drugResponse.name = drug.name;
drugResponse.quantity = drug.quantity;
logEvent(drug.id, "CREATE", JSON.stringify(req.body));
//JSON.stringify(drugAdded, ['id','name','quantity']);
res.status(201).json(drugResponse);
});
// Drugs stock
app.get("/", async (req, res) => {
const stock = await DrugModel.find();
res.json(stock);
});
// Increase quantity of a drug in stock
app.post("/add", async (req, res) => {
try {
const drug = await DrugModel.findById(req.body.id);
if (drug) {
drug.quantity += req.body.quantity;
await drug.save();
logEvent(drug.id, "ADD QTY", `Add ${req.body.quantity} of ${drug.name}`);
return res.status(202).json({ message: "Quantity updated" });
} else {
return res
.status(204)
.json({ message: `No drug found with id :"${req.body.id}" ` });
}
} catch (error) {
return res.status(400).json({ error: error.message });
}
});
// Remove from stock a drug by its drug id
app.post("/remove", async (req, res) => {
try {
const drug = await DrugModel.findById(req.body.id);
if (drug) {
if (req.body.quantity > drug.quantity) {
return res.status(400).json({
error: {
message: "Invalid quantity"
}
});
} else {
drug.quantity -= req.body.quantity;
drug.save();
logEvent(
drug.id,
"REMOVE QTY",
`Remove ${req.body.quantity} of ${drug.name}`
);
return res.json({ message: "Quantity removed" });
}
} else {
return res
.status(204)
.json({ message: `No drug found with id :"${req.body.id}" ` });
}
} catch (error) {
return res.status(400).json({ error: error.message });
}
});
// Get drug's quantity in stock
app.get("/quantity", async (req, res) => {
const drug = await DrugModel.findOne({ name: req.query.name });
if (drug) {
return res.json(drug);
}
return res
.status(204)
.json({ message: `No drug found with name :"${req.query.name}" ` });
});
// Change name of a drug
app.post("/rename", async (req, res) => {
try {
const drug = await DrugModel.findById(req.body.id);
if (drug) {
const previousName = drug.name;
drug.name = req.body.name;
await drug.save();
logEvent(
drug.id,
"RENAME",
`Rename drug ${previousName} by ${drug.name}`
);
return res.status(202).json({ message: "Name updated" });
} else {
return res
.status(204)
.json({ message: `No drug found with id :"${req.body.id}" ` });
}
} catch (error) {
return res.status(400).json({ error: error.message });
}
});
// Delete a drug from stock
app.post("/delete", async (req, res) => {
try {
const drug = await DrugModel.findById(req.body.id);
if (drug) {
const idDeleted = drug._id;
const nameDeleted = drug.name;
drug.remove();
logEvent(idDeleted, "DELETE", `Delete drug ${drug.name}`);
return res.json({ message: "Drug is deleted" });
} else {
return res
.status(204)
.json({ message: `No drug found with id :"${req.body.id}" ` });
}
} catch (error) {
return res.status(400).json({ error: error.message });
}
});
app.get("/history", async (req, res) => {
const lastEvents = await TraceModel.find()
.limit(10)
.sort({ timeStamp: "descending" });
res.json(lastEvents);
});
const logEvent = (id, action, info) => {
var tsInMilli = new Date().getTime();
console.log(`${tsInMilli} : ${id} : ${action} : ${info}`);
const trace = new TraceModel({
timeStamp: tsInMilli,
drugId: id,
command: action,
changeInfo: info
});
trace.save();
};
// All others routes
app.all("*", function(req, res) {
sendError(res, "Page not found!", 404);
});
app.listen(3000, () => {
console.log("<>-(*(*(*(*- Drugs Stock Server started... -*)*)*)*)-<>");
});