-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
248 lines (223 loc) · 6.34 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const express = require("express");
require("dotenv").config();
const app = express();
const bodyParser = require("body-parser");
const { Parser } = require("json2csv");
const fs = require("fs");
const cors = require("cors");
const mongoose = require("mongoose");
const inventory_routes = express.Router();
const PORT = process.env.PORT || 4000;
let Inventory = require("./inventory.model");
let PurchaseOrders = require("./purchase-orders.model");
var path = require("path");
const stripe = require("stripe")(process.env.STRPE_KEY);
app.use(cors());
app.use(bodyParser.json());
app.use("/inventory", inventory_routes);
app.use(express.static(path.join(__dirname, "build")));
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
// mongoose.connect("mongodb://127.0.0.1:27017/inventory_mst", {
// useNewUrlParser: true,
// });
mongoose.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 100,
});
const connection = mongoose.connection;
connection.once("open", function () {
console.log("MongoDB database connection established successfully");
});
app.listen(PORT, function () {
console.log("Server is running on Port: " + PORT);
});
// csv download of master
inventory_routes.route("/downloadmst/:option").get(function (req, res) {
let option = req.params.option;
let currentTable = Inventory;
let filename = "";
let fields = [];
if (option == 1) {
currentTable = Inventory;
fields = [
"product_id",
"brand_id",
"name",
"desciption",
"category",
"per_quanitity_price",
"current_stock",
"required_stock",
];
filename = "InventoryMaster.csv";
} else {
fields = [
"product_id",
"order_status",
"order_quantity",
"total_price",
"payment_status",
"order_date",
];
currentTable = PurchaseOrders;
filename = "PurchaseOrdersMaster.csv";
}
currentTable.find(function (err, mst) {
if (err) {
console.log(err);
} else {
const options = { fields };
const parser = new Parser(options);
const csv = parser.parse(mst);
res.attachment(filename);
res.status(200).send(csv);
}
});
});
function csvDownload(mst, filename) {
const fields = [
"product_id",
"brand_id",
"name",
"desciption",
"category",
"per_quanitity_price",
"current_stock",
"required_stock",
];
const ops = { fields };
const parser = new Parser(ops);
const csv = parser.parse(mst);
fs.writeFileSync(filename, csv);
}
// Payment checkout
inventory_routes.route("/checkout").post(function (req, res) {
let payment_order = new PurchaseOrders({
product_id: req.body.product_id,
payment_status: req.body.payment_status,
order_status: req.body.order_status,
total_price: req.body.total_price,
order_quantity: req.body.order_quantity,
order_date: req.body.order_date,
});
payment_order
.save()
.then((inventory_item) => {
res.status(200).json({ mst: "success" });
})
.catch((err) => {
res.status(400).send("failed");
});
});
// Get All Items
inventory_routes.route("/").get(function (req, res) {
Inventory.find(function (err, mst) {
if (err) {
console.log(err);
} else {
res.json(mst);
}
});
});
inventory_routes.route("/purchaseorders").get(function (req, res) {
PurchaseOrders.find(function (err, mst) {
if (err) {
console.log(err);
} else {
res.json(mst);
}
});
});
// Get an Item By _ID from MST
inventory_routes.route("/:id").get(function (req, res) {
let id = req.params.id;
Inventory.findById(id, function (err, mst) {
res.json(mst);
});
});
// Get an Item By _ID from PurchaseOrders
inventory_routes.route("/purchaseorders/:id").get(function (req, res) {
let id = req.params.id;
PurchaseOrders.findById(id, function (err, mst) {
res.json(mst);
});
});
// Get an Item By Product ID
inventory_routes.route("/product_id/:product_id").get(function (req, res) {
let id = req.params.product_id;
Inventory.find({ product_id: id }, function (err, mst) {
res.json(mst);
});
});
// Add Operation
inventory_routes.route("/add").post(function (req, res) {
let inventory_item = new Inventory(req.body);
inventory_item
.save()
.then((inventory_item) => {
res.status(200).json({ mst: "mst item added successfully" });
})
.catch((err) => {
res.status(400).send("adding new mst item failed");
});
});
inventory_routes.route("/purchaseorders/add").post(function (req, res) {
let purchase_order = new PurchaseOrders(req.body);
purchase_order
.save()
.then((inventory_item) => {
res.status(200).json({ mst: "purchase order created successfully!" });
})
.catch((err) => {
res.status(400).send("purchase could not be created");
});
});
// Update Operation
inventory_routes.route("/update/:id").post(function (req, res) {
Inventory.findById(req.params.id, function (err, mst) {
if (!mst) res.status(404).send("mst item is not found");
else mst.product_id = req.body.product_id;
mst.brand_id = req.body.brand_id;
mst.name = req.body.name;
mst.description = req.body.description;
mst.quantity = req.body.quantity;
mst.per_quanitity_price = req.body.per_quanitity_price;
mst.sum_quantity_price = req.body.sum_quantity_price;
mst.current_stock = req.body.current_stock;
mst.category = req.body.category;
mst.required_stock = req.body.required_stock;
mst
.save()
.then((mst) => {
res.json("mst item updated!");
})
.catch((err) => {
res.status(400).send("mst item Update not possible");
});
});
});
//Update operation for PurchaseOrders
inventory_routes.route("/purchaseorders/update/:id").post(function (req, res) {
PurchaseOrders.findById(req.params.id, function (err, mst) {
if (!mst) res.status(404).send("mst item is not found");
else mst.order_status = req.body.order_status;
mst
.save()
.then((mst) => {
res.json("mst item updated!");
})
.catch((err) => {
res.status(400).send("mst item Update not possible");
});
});
});
// Delete Operation
inventory_routes.route("/delete/:id").get(function (req, res) {
let id = req.params.id;
Inventory.deleteOne({ _id: id }, function (err, mst) {
res.json("Deleted the record");
});
});