-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
431 lines (386 loc) · 10.5 KB
/
app.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
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
// Author :- Atul kumar
// Intern at The Sparks Foundation
require('dotenv').config();
const express = require("express");
//hadle data from form
const bodyParser = require("body-parser");
//for connecting to database
const mongoose = require("mongoose");
const ejs = require("ejs");
const date = require(__dirname + "/date.js");
//for capitalize user name after singup
const _ = require("lodash");
const app = express();
//for checking the form is submitted
let result = false;
let exits = false;
let lowamount = false;
const user = process.env.USER;
const password = process.env.PASS;
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// const password=private.password;
// mongoose
// .connect("mongodb://localhost:27017/bankDB", {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// })
// .then(function () {
// console.log("connected to bankDB");
// })
// .catch((err) => console.error(err));
//connecting database with database name bankDB
(async function () {
try {
await mongoose.connect(`mongodb+srv://${user}:${password}@cluster0.iz4lpfy.mongodb.net/bankDB`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to bankDB");
} catch (err) {
console.error(err);
}
})();
//schema for user
const userSchema = new mongoose.Schema({
name: String,
email: String,
balance: Number,
});
//make model of user
const User = mongoose.model("User", userSchema);
//creating dummy data for Users
const user1 = new User({
name: "Atul kumar",
email: "[email protected]",
balance: 3000,
});
const user2 = new User({
name: "Alok kumar",
email: "[email protected]",
balance: 6700,
});
const user3 = new User({
name: "Chandan kumar",
email: "[email protected]",
balance: 3800,
});
const user4 = new User({
name: "Chandrama kumar",
email: "[email protected]",
balance: 1200,
});
const user5 = new User({
name: "Ankita kumari",
email: "[email protected]",
balance: 3000,
});
const user6 = new User({
name: "Subodh kumar",
email: "[email protected]",
balance: 3900,
});
const user7 = new User({
name: "Ashwani kumar",
email: "[email protected]",
balance: 3000,
});
const user8 = new User({
name: "Harsh kumar",
email: "[email protected]",
balance: 1000,
});
const user9 = new User({
name: "Pranav kumar",
email: "[email protected]",
balance: 9800,
});
const user10 = new User({
name: "Sushant kumar",
email: "[email protected]",
balance: 6700,
});
//made array of users
const userData = [
user1,
user2,
user3,
user4,
user5,
user6,
user7,
user8,
user9,
user10,
];
//if user collection is empty,then insert dummy data
User.find()
.then((foundItems) => {
if (foundItems.length == 0) {
User.insertMany(userData)
.then(() => console.log("inserted dummy data in User collection"))
.catch((err) => console.error(err));
}
})
.catch((err) => console.error(err));
//transaction schema
const transactionSchema = new mongoose.Schema({
sender: String,
receiver: String,
amount: Number,
date: String,
});
//make model of transaction
const Transaction = mongoose.model("Transaction", transactionSchema);
//dummy data for transaction
const t1 = new Transaction({
sender: "Atul kumar",
receiver: "Alok kumar",
amount: 1000,
date: date.getDate(),
});
const t2 = new Transaction({
sender: "Chandan kumar",
receiver: "Alok kumar",
amount: 900,
date: date.getDate(),
});
const t3 = new Transaction({
sender: "Pranav kumar",
receiver: "Alok kumar",
amount: 1000,
date: date.getDate(),
});
const t4 = new Transaction({
sender: "Sushant kumar",
receiver: "Harsh kumar",
amount: 1200,
date: date.getDate(),
});
const t5 = new Transaction({
sender: "Atul kumar",
receiver: "Pranav kumar",
amount: 1800,
date: date.getDate(),
});
const t6 = new Transaction({
sender: "Ashwani kumar",
receiver: "Subodh kumar",
amount: 8762,
date: date.getDate(),
});
const t7 = new Transaction({
sender: "Harsh kumar",
receiver: "Alok kumar",
amount: 900,
date: date.getDate(),
});
const transactionData = [t1, t2, t3, t4, t5, t6, t7];
//if transaction collection is empty,insert dummy transactions
Transaction.find()
.then((foundItems) => {
if (foundItems.length == 0) {
Transaction.insertMany(transactionData)
.then(() =>
console.log("inserted dummy data in Transaction collection")
)
.catch((err) => console.error(err));
}
})
.catch((err) => console.error(err));
//home page render
app.get("/", function (req, res) {
res.render("home");
});
//customerlist page render,with customer name
app.get("/customerlist", function (req, res) {
User.find()
.then((foundUsers) => {
//if new user register then this work
if (result === true) {
result = false;
res.render("customerlist", {
customers: foundUsers,
i: 1,
message: "welcome to spark foundation bank",
});
}
// if user already present ,then directly goes to customerlist page with message
else if (exits === true) {
exits = false;
res.render("customerlist", {
customers: foundUsers,
i: 1,
message: "user already exist!",
});
} else if (lowamount === true) {
lowamount = false;
res.render("customerlist", {
customers: foundUsers,
i: 1,
message: "Not present required money",
});
} else {
res.render("customerlist", {
customers: foundUsers,
i: 1,
message: null,
});
}
})
.catch((err) => console.error(err));
});
//render transaction page
app.get("/transaction", function (req, res) {
Transaction.find()
.then((foundTransaction) => {
res.render("transaction", { transactions: foundTransaction, i: 1 });
})
.catch((err) => console.error(err));
});
//render contact page
app.get("/contact", function (req, res) {
res.render("contact");
});
//if new user sign up then handle data and insrt in db
app.post("/", function (req, res) {
const user = new User({
name: _.capitalize(req.body.name),
email: req.body.email,
balance: req.body.balance,
});
// console.log(user);
User.findOne({ email: req.body.email })
.then((foundUser) => {
if (!foundUser) {
user
.save()
.then(() => console.log("registered new user."))
.catch((err) => console.error(err));
User.find()
.then((foundUsers) => {
result = true;
res.redirect("/customerlist");
})
.catch((err) => console.error(err));
} else {
console.log("user exits");
User.find()
.then((foundUsers) => {
exits = true;
res.redirect("/customerlist");
})
.catch((err) => console.error(err));
}
})
.catch((err) => console.error(err));
});
app.post("/customerlist", async function (req, res) {
// //make new trnsaction
// var senderBalance;
// var receiverBalance;
// //finding sender data
// var amount=parseInt(req.body.amount);
// // console.log(typeof(amount));
// User.findOne({ name: req.body.sender })
// .then((foundUser) => {
// // if(foundUser)
// // console.log("user in founderuserj");
// // console.log(typeof(foundUser.balance));
// // console.log(foundUser.balance);
// if (foundUser.balance >=amount) {
// // console.log("user exits");
// const transaction = new Transaction({
// sender: req.body.sender,
// receiver: req.body.receiver,
// amount: amount,
// date: date.getDate(),
// });
// transaction
// .save()
// .then(() => console.log("transaction successfull"))
// .catch((err) => console.error(err));
// senderBalance = foundUser.balance - amount;
// } else {
// lowamount = true;
// // console.log("user not exits");
// console.log("balance insuffiecent");
// res.redirect("/customerlist");
// }
// })
// .catch((err) => console.error(err));
// //finding receiver data
// User.findOne({ name: req.body.receiver })
// .then((foundUser) => {
// receiverBalance = foundUser.balance + amount;
// console.log("updated");
// })
// .catch((err) => console.error(err));
// console.log(receiverBalance);
// console.log(senderBalance);
// //updating sender data
// User.updateOne({name:req.body.sender},{
// $set:{
// balance:senderBalance,
// }
// });
// //updaing receiver data
// User.updateOne({name:req.body.receiver},{
// $set:{
// balance:receiverBalance,
// }
// });
// console.log(senderBalance)
// console.log(receiverBalance)
// res.redirect("/customerlist");
try {
const amount = parseInt(req.body.amount);
// Finding sender data
const foundSender = await User.findOne({ name: req.body.sender });
if (!foundSender || foundSender.balance <= amount) {
console.log("Sender not found or balance insufficient");
lowamount = true;
res.redirect("/customerlist");
return;
}
// Finding receiver data
const foundReceiver = await User.findOne({ name: req.body.receiver });
if (!foundReceiver) {
console.log("Receiver not found");
res.redirect("/customerlist");
return;
}
// Create new transaction
const transaction = new Transaction({
sender: req.body.sender,
receiver: req.body.receiver,
amount: amount,
date: date.getDate(),
});
await transaction.save();
console.log("Transaction successful");
// Update sender balance
const senderBalance = foundSender.balance - amount;
await User.updateOne(
{ name: req.body.sender },
{ $set: { balance: senderBalance } }
);
// Update receiver balance
const receiverBalance = foundReceiver.balance + amount;
await User.updateOne(
{ name: req.body.receiver },
{ $set: { balance: receiverBalance } }
);
console.log(`Sender balance updated to ${senderBalance}`);
console.log(`Receiver balance updated to ${receiverBalance}`);
res.redirect("/customerlist");
} catch (err) {
console.error(err);
res.redirect("/customerlist");
}
});
//check for server is listen or not
app.listen(3000 || process.env.PORT, function () {
console.log("server started at port 3000");
});