-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.js
More file actions
449 lines (398 loc) · 11.4 KB
/
Copy pathfinal.js
File metadata and controls
449 lines (398 loc) · 11.4 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
require("dotenv").config();
const express = require("express");
const fs = require("fs");
const parse = require("csv-parser");
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const cors = require("cors"); // Import the cors module
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
//https://www.browserling.com/tools/bcrypt
const uri = process.env.MONGO_URL;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
const dataBase = client.db(process.env.DATABASE_DEV);
const collection = dataBase.collection("products");
const app = express();
const appVersion = "v03.03.24.02";
app.use(express.json());
app.use(cors()); // Use cors middleware
//Intial Route
app.get("/", (req, res) => {
res.send("Welcome to the authentication app!");
});
const secretKey = "your-secret-key";
const usersCollection = dataBase.collection("testUsers");
app.get("/users", async (req, res) => {
try {
const users = await getUsers();
res.status(200).json(users);
} catch (error) {
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
const getUsers = async () => {
try {
const result = await usersCollection.find().toArray();
const data = result.map((e) => {
const { password, ...rest } = e;
return rest;
});
return data;
} catch (error) {
console.log(`ERROR : ${error}`);
}
};
const getExistingUsers = async () => {
try {
const result = await usersCollection.find().toArray();
return result;
} catch (error) {
console.log(`ERROR : ${error}`);
}
};
app.post("/sigin", async (req, res) => {
try {
if (Object.keys(req.body).length === 0) {
return res
.status(400)
.json({ error: "Invalid data. Please provide valid data." });
}
const { userId, password } = req.body;
const users = await getExistingUsers();
const isUser = users.find((e) => e.userId === userId);
console.log("Found user:", isUser);
if (isUser && bcrypt.compareSync(password, isUser.password)) {
// Generate a JWT token
const token = jwt.sign(
{
userId: isUser.userId,
},
secretKey,
{ expiresIn: "1h" }
);
res.status(200).json({
message: "success",
token: token,
});
} else {
console.log("Invalid username or password.");
res.status(401).json({ message: "Invalid username or password." });
}
} catch (error) {
console.error("Error", error);
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
app.post("/sigup", async (req, res) => {
try {
if (Object.keys(req.body).length === 0) {
return res
.status(400)
.json({ error: "Invalid data. Please provide valid data." });
}
const { userId, password, firstName, lastName } = req.body;
const users = await getExistingUsers();
const isUserExist = users.find((e) => e.userId === userId);
if (isUserExist) {
res
.status(400)
.json({ message: "Username already exists. Choose another one." });
} else {
// Hash the password before storing (using bcrypt)
const hashedPassword = bcrypt.hashSync(password, 10);
// Store the user (replace with database insert)
const result = await usersCollection.insertOne({
userId,
firstName: firstName,
lastName: lastName,
password: hashedPassword,
});
console.log(`Saved response with ID: ${result.insertedId}`);
// users.push({ userId, password: hashedPassword });
res.status(200).json({
message: "success",
});
}
} catch (error) {
console.error("Error", error);
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
// Protected route
// Middleware to verify JWT token
const verifyToken = (req, res, next) => {
const token = req.headers["authorization"];
if (!token) {
return res.status(403).json({ message: "No token provided." });
}
jwt.verify(token, secretKey, (err, decoded) => {
if (err) {
return res.status(401).json({ message: "Failed to authenticate token." });
}
req.user = decoded;
next();
});
};
app.get("/dashboard", verifyToken, (req, res) => {
res.json({ message: "You have access to this protected route!" });
});
//CRUD OP
app.post("/addInventory", verifyToken, (req, res) => {
try {
console.log(req.body);
// Check if req.body is empty or undefined
if (!req.body || Object.keys(req.body).length === 0) {
return res
.status(400)
.json({ error: "Invalid data. Please provide valid data." });
}
let dataJson = JSON.parse(JSON.stringify(req.body));
console.log(dataJson, "dataJson");
addProducts(dataJson);
res.status(200).json({ message: "success", data: dataJson });
} catch (error) {
res.status(500).json({ error: `Internal Server Error: ${error.message}` });
}
});
const addProducts = async (dataJson) => {
try {
const result = await collection.insertOne(dataJson);
console.log(`Saved response with ID: ${result.insertedId}`);
} catch (error) {
res.status(500).json({ error: `Internal Server Error: ${error.message}` });
}
};
app.get("/inventoryProducts", verifyToken, async (req, res) => {
try {
const products = await getInventory();
res.status(200).json(products);
} catch (error) {
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
const getInventory = async () => {
try {
const result = await collection.find().toArray();
return result.splice(0, 200);
} catch (error) {
console.log(`ERROR : ${error}`);
}
};
app.get("/getProductDetail/:id", verifyToken, async (req, res) => {
const user = await getUserById(req.params.id);
res.status(200).json(user);
});
const getUserById = async (id) => {
const user = await collection.findOne({ _id: new ObjectId(id) });
return user;
};
app.get("/querieSingle", async (req, res) => {
try {
if (!req.query.q || Object.keys(req.query.q).length === 0) {
return res
.status(400)
.json({ error: "Invalid data. Please provide query data." });
}
const querieItem = req.query.q;
const queryResult = await collection
.find({
movie: querieItem,
})
.toArray();
res.status(200).json(queryResult);
} catch (error) {
console.error("Error", error);
res.status(500).json({
error: `Internal Serever Error: ${error.message}`,
});
}
});
app.get("/querieMulti", async (req, res) => {
try {
if (!req.query.q || Object.keys(req.query.q).length === 0) {
return res
.status(400)
.json({ error: "Invalid data. Please provide query data." });
}
const queryFilters = [];
Object.keys(req.query.q).forEach((key) => {
console.log(req.query.q[key]);
queryFilters.push({ [key]: req.query.q[key] });
});
const queryResult = await collection.find({ $and: queryFilters }).toArray();
res.status(200).json(queryResult);
} catch (error) {
console.error("Error", error);
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
app.put("/updateProduct/:id", verifyToken, async (req, res) => {
try {
const id = req.params.id;
if (!req.body || Object.keys(req.body)?.length === 0) {
return res.status(400).json({
error: "Request body is Empty",
});
}
const data = JSON.parse(JSON.stringify(req.body));
console.log(data);
const result = await updateProductById(id, data);
if (!result) {
return res.status(404).json({
error: "Product not Found",
});
}
res.status(200).json({
message: "Product updated successfully",
});
} catch (error) {
console.error("Error updating product:", error);
res.status(500).json({
error: "Internal Server Error",
});
}
});
const updateProductById = async (id, data) => {
try {
if (!ObjectId.isValid(id)) {
return null;
}
const result = await collection.updateOne(
{ _id: new ObjectId(id) },
{ $set: data }
);
return result;
} catch (error) {
console.error("Error", error);
res.status(500).json({
error: "Internal Serever Error",
});
}
};
app.delete("/deleteProduct/:id", verifyToken, async (req, res) => {
const id = req.params.id;
const success = await deleteProductById(id);
if (success) {
res.json({ message: "User Deleted Successfully" });
} else {
res.status(404).json({ message: "User not found" });
}
});
const deleteProductById = async (id) => {
const result = await collection.deleteOne({ _id: new ObjectId(id) });
return result.deletedCount > 0;
};
//Fetch by URL
app.get("/dummyApi", async (req, res) => {
try {
const data = await getDummyData();
res.status(200).json(data);
} catch (error) {
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
const getDummyData = async () => {
try {
const response = await fetch(
"https://jsonplaceholder.typicode.com/comments"
);
return await response.json();
} catch (error) {
console.log(`ERROR : ${error}`);
}
};
//Filter Data
app.post("/filterDummy", async (req, res) => {
try {
const { postId } = req.body;
if (!postId) {
return res.status(400).json({ error: "postId is required" });
}
const fetchedData = await getDummyData();
const filteredData = fetchedData.filter((e) => e.postId === postId);
res.status(200).json(filteredData);
} catch (error) {
console.error("Error in filterDummy:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
// Get Dynamic URL's
app.get("/get/:tableName", async (req, res) => {
try {
const tableName = req.params.tableName;
const dataResponse = await getDataByTableName(tableName);
res.status(200).json(dataResponse);
} catch (error) {
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
});
const getDataByTableName = async (tableName) => {
console.log(tableName);
try {
const dataBase = client.db("sample_analytics");
const collection = dataBase.collection(tableName);
const result = await collection.find().toArray();
return result;
} catch (error) {
console.log(`ERROR : ${error}`);
}
};
const readCSVFile = (filePath) => {
const data = [];
const finalData = [];
fs.createReadStream(filePath)
.pipe(parse())
.on("row", (row) => {
data.push({
sku: row[0],
productName: row[1],
description: row[2],
brand: row[3],
category: row[4],
finish: row[5],
cost: row[6],
price: row[7],
qty: row[8],
discontinued: row[9],
});
})
.on("data", async (query) => {
finalData.push(query);
})
.on("end", async () => {
console.log(finalData, "data");
// await collection.insertMany(finalData);
// console.log(
// "CSV file successfully processed and data inserted into MongoDB"
// );
})
.on("error", (error) => {
console.log(`Error reading CSV file: ${error}`);
});
};
const csvFilePath = "../CSV/BL_Inventory_Products.csv";
// readCSVFile(csvFilePath);
const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`Server running on ${PORT}`);
console.log(`http://localhost:${PORT}`);
});