-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (152 loc) · 3.64 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
const express = require('express')
const app = express();
const morgan = require('morgan')
const mongoose = require('mongoose')
const Customer = require('./models/customer')
const Transaction = require('./models/transaction')
//Database Connection URL
const dbURI = ''
app.set('view engine','ejs');
app.use(morgan('dev'))
app.use(express.static('static'))
app.use(express.urlencoded({extended:true}))
mongoose.connect(dbURI,{useNewUrlParser:true, useUnifiedTopology:true})
.then((result)=>{
console.log("connected to db");
//console.log(result);
PORT=process.env.PORT||3000;
app.listen(PORT,()=>{
console.log(`Listening on port ${PORT}`);
})
})
.catch((err)=>{
console.log(err)
})
app.get("/",(req,res)=>{
res.redirect("/home");
})
//HOME
app.get('/home',(req,res)=>{
res.render('home');
})
//CUSTOMERS
app.get('/customers',(req,res)=>{
//get all customers
Customer.find()
.then((result)=>{
res.render('customers',{customers:result})
})
.catch((err)=>{
console.log(err)
})
})
//CUSTOMER
app.get('/customers/:id',(req,res)=>{
id = req.params.id
Customer.findById(id)
.then((result)=>{
res.render('customer',{customer:result});
})
.catch((err)=>{
console.log(err)
})
})
//MONEY TRANSFER
app.get('/transfer/:id',(req,res)=> {
//show available recepients except the sender itself
senderId = req.params.id
Customer.findById(senderId)
.then((result)=>{
senderName = result.name;
max_transfer = result.balance;
Customer.find({ "email": { "$ne": result.email }})
.then((result)=>{
res.render("transfer",{senderName,senderId,limit:max_transfer,customers:result})
})
.catch((err)=>{
console.log("Error in finding potential recepients");
console.log(err);
})
})
.catch((err)=>{
console.log(err)
})
})
//TRANSFER OPERATION
app.post('/transfer',(req,res)=> {
let senderId = req.body.sender
let receiverId = req.body.recepient
let amount = req.body.amount
let sender_name = ""
let receiver_name = ""
Customer.findById(senderId)
.then((doc)=>{
doc.balance = doc.balance-Number(amount)
sender_name = doc.name
doc.save()
.then((updated_doc)=>{
console.log("Amount deducted successfully")
})
.catch((err)=>{
console.log("Deduction unsuccessful")
console.log(err)
})
})
.catch((err)=>{
console.log("Cannot find sender")
console.log(err)
})
Customer.findById(receiverId)
.then((doc)=>{
doc.balance = doc.balance + Number(amount)
receiver_name = doc.name
doc.save()
.then((updated_doc)=>{
console.log("Amount added successfully")
//TRANSACTION
const transaction = new Transaction({
sender_id : senderId,
receiver_id : receiverId,
sender_name : sender_name,
receiver_name : receiver_name,
amount_transferred : amount
})
transaction.save()
.then((result)=>{
console.log("TRANSACTION")
console.log(result)
})
.catch((err)=>{
console.log("Error in logging transaction")
console.log(err)
})
res.redirect('/customers')
})
.catch((err)=>{
console.log("Addition unsuccessful")
console.log(err)
})
})
.catch((err)=>{
console.log("Cannot find receiver")
console.log(err)
})
})
//TRANSFERS
app.get('/transfers',(req,res)=>{
Transaction.find()
.then((result)=>{
res.render('transfers',{transfers:result})
})
.catch((err)=>{
console.log(err)
})
})
//ABOUT
app.get('/about',(req,res)=>{
res.render('about')
})
//404 PAGE
app.use((req,res)=>{
res.status(404).render('generic',{val: "PAGE NOT FOUND"})
})