-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftrial.js
390 lines (312 loc) · 10.7 KB
/
ftrial.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
if (process.env.NODE_ENV !== "production"){
require("dotenv").config()
}
const express=require("express")
const app=express()
const mongoose=require("mongoose")
const bodyParser=require("body-parser")
const port=8500
const User = require('./model/product');
const path=require("path"); //register
const static_path=path.join(__dirname,"./public")//register
app.use(express.static(static_path));
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
app.use(express.static('public'));
// const alerts =require('./public/alert')
/////code for flash msg /////
const initializePassport = require("./passport-config")
const flash = require("express-flash")
const session = require("express-session")
const passport = require("passport")
initializePassport(
passport,
email => User.find({email : User.email}),
id => User.find({_id: User._id})
)
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
///////////
var fs = require('node:fs');
var pie = fs.readFileSync('./views/analysis.ejs', 'utf-8')
var tablehtml = fs.readFileSync('./views/index6.ejs', 'utf-8')
var piefile6;
let productarray;
var exana;
// console.log(tablehtml.indexOf('end-->'));
var maintable = tablehtml.slice(3067,3905);
var finaltable
// console.log(maintable);
// var logoutbtn= tablehtml.slice(1039,1105)
// console.log(logoutbtn);
mongoose.connect('mongodb+srv://kankariyakiran1507:kiran1507@cluster0.iinq753.mongodb.net/moneymanager')
.then(()=>{console.log("Connected to MongoDB")})
.catch((err)=>{console.log(err)})
app.set("view engine","ejs")
app.engine('ejs', require('ejs').__express);
app.get('/',async(req,res)=>{
await update_table(res)
})
app.get('/analysis',(req,res)=>{
res.end(piefile6)
})
app.get('/budgets',(req,res)=>{
res.render("Budget")
})
app.get("/register",(req,res)=>{ //index login
res.render("register")
});
app.get('/login/',(req,res)=>{
res.render("login")
})
const test=require("./model/product") // inputing the created dB collection
const bud = require('./model/budget')
const expenseA = require('./model/analysis')
const Login=require("./model/signup")
const { log } = require("node:console")
// const budget = require("./model/budget")
var byuser = '';
var gEA,medEA,rEA,pEA,misEA,sEA,iEA;
app.post('/',async (req,res)=>{
const data=new test(req.body)
if (byuser === ''){
res.redirect("/login")
}else{
if(data.category==="Grocery")
{
data.Grocery=data.amount
gEA=data.amount
}
else if(data.category==="Medical")
{
data.Medical=data.amount
medEA=data.amount
}
else if(data.category==="Stationary")
{
data.Stationary=data.amount
sEA=data.amount
}
else if(data.category==="Petrol")
{
data.Petrol=data.amount
pEA=data.amount
}
else if(data.category==="Rent")
{
data.Rent=data.amount
rEA=data.amount
}
else if(data.category==="Miscelleneous")
{
data.Miscelleneous=data.amount
misEA=data.amount
}
else if(data.category==="Income Credited")
{
data.Income=data.amount
iEA = data.amount
}
data.byuser = byuser
await data.save()
const e = new expenseA(req.body)
const datas = await expenseA.findOne({byuser:byuser})
exana = datas
await update_table(res)
await update_expenses()
await piechart()
}
})
const piechart = async ()=>{
const datas = await expenseA.findOne({byuser:byuser})
const piefile1 = pie.replace('{{%groceryE%}}', datas.GroceryA)
const piefile2 = piefile1.replace('{{%medicalE%}}', datas.MedicalA)
const piefile3 = piefile2.replace('{{%petrolE%}}', datas.PetrolA)
const piefile4 = piefile3.replace('{{%rentE%}}', datas.RentA)
const piefile5 = piefile4.replace('{{%stationaryE%}}', datas.StationaryA)
piefile6 = piefile5.replace('{{%miscelleneousE%}}', datas.MiscelleneousA)
}
const update_expenses = async ()=>{
exana.GroceryA += gEA
exana.MedicalA += medEA
exana.RentA += rEA
exana.PetrolA += pEA
exana.MiscelleneousA += misEA
exana.StationaryA += sEA
exana.IncomeA += iEA
exana.byuser = byuser
gEA = 0; medEA = 0; rEA = 0; pEA = 0; misEA = 0; sEA = 0; iEA = 0;
const result = await expenseA.findOneAndUpdate({byuser:byuser},{$set:exana})
}
app.post("/delete",async(req,res) => {
try {
const del = new test(req.body)
let datas = await expenseA.findOne({byuser:byuser})
delarr = await test.findById({"_id":del.deleteid})
ncategory = delarr.category
ncategory = ncategory.charAt(0).toUpperCase() + ncategory.slice(1) + 'A';
datas[ncategory] -= delarr.amount
await expenseA.findOneAndUpdate({byuser:byuser},{$set:datas})
await test.findByIdAndDelete({"_id":del.deleteid})
await update_table(res)
await piechart()
} catch (error) {
console.log(error);
}
})
//code for getting the db data in json file and updating it in the index 6 copy file
app.get("/categories", async (req, res)=> {
try {
console.log("Inside try")
User.find({byuser:byuser}).then((datas)=>{
fs.writeFile('data.json',JSON.stringify(datas), function (err) {
if (err){console.log(err);}
else{console.log('Replaced!');
var products = JSON.parse(fs.readFileSync('./data.json','utf-8'))
productarray = products.map((prod)=>{
let content = maintable.replace('{{%DATE%}}',prod.createdAt);
content = content.replace('{{%CATEGORY%}}',prod.category)
content = content.replace('{{%AMOUNT%}}',prod.amount)
content = content.replace('{{%BUTTON%}}',prod._id)
return content;
})
finaltable = tablehtml.replace(maintable , productarray.join('<br>'))
res.end(finaltable)
}
});
})
} catch (err) {
console.log(err)
}
})
const update_table = async (res)=>{
User.find({byuser:byuser}).then((datas)=>{
fs.writeFile('data.json',JSON.stringify(datas), function (err) {
if (err){console.log(err);}
else{
// finaltable = tablehtml.replace(logoutbtn,'<a href="/logout">Logout</a>')
var products = JSON.parse(fs.readFileSync('./data.json','utf-8'))
productarray = products.map((prod)=>{
let content = maintable.replace('{{%DATE%}}',prod.createdAt);
content = content.replace('{{%CATEGORY%}}',prod.category)
content = content.replace('{{%AMOUNT%}}',prod.amount)
content = content.replace('{{%BUTTON%}}',prod._id)
return content;
})
finaltable = tablehtml.replace(maintable , productarray.join('<br>'))
res.end(finaltable)
}
});
})
}
var B = {
GroceryB: 0,
MedicalB: 0,
StationaryB: 0,
PetrolB: 0,
RentB: 0,
MiscelleneousB: 0,
}
app.post('/budgets/',async (req,res)=>{
const Bdata=new bud(req.body)
if (byuser === ''){
res.redirect("/login")
}else{
if(Bdata.Bcategory==="Grocery")
{
B.GroceryB=Bdata.Bamount
}
else if(Bdata.Bcategory==="Medical")
{
B.MedicalB=Bdata.Bamount
}
else if(Bdata.Bcategory==="Stationary")
{
B.StationaryB=Bdata.Bamount
}
else if(Bdata.Bcategory==="Petrol")
{
B.PetrolB=Bdata.Bamount
}
else if(Bdata.Bcategory==="Rent")
{
B.RentB=Bdata.Bamount
}
else if(Bdata.Bcategory==="Miscelleneous")
{
B.MiscelleneousB=Bdata.Bamount
}
await updatebudget(B)
}
})
const updatebudget = async (B)=>{
console.log(B);
const result = await bud.findOneAndUpdate({byuser:byuser},{$set:B})
}
app.listen(port,()=>{
console.log(`Listening on port ${port}`)
})
//login and signup by shri
app.use(express.urlencoded({extended:false})); //register
app.post("/register/",async(req,res)=>{ //register
try { //register
const password=req.body.password; //register
const confirm=req.body.confirmP; //register
if(password===confirm){ //register
const details= new Login({ //register
firstname:req.body.firstname, //register
lastname:req.body.lastname, //register
email:req.body.email, //register
phone:req.body.phone, //register
password:req.body.password, //register
confirmP:req.body.confirmP
})
details.save(); //register
const b = new bud(req.body)
b.byuser = details.email
b.save()
const exa = new expenseA(req.body)
exa.byuser = details.email
exa.save()
res.redirect("/login"); //register
}else{ //register
req.send(error)
}
} catch (error) {
res.status(400).send(error);
} //register
}); //register
app.post('/login',passport.authenticate("local",{ /// new trail code as a middleware
successRedirect:"/",
failureRedirect:"/login",
failureFlash: true
}))
app.post('/login/',async(req,res)=>{
try {
const email=req.body.email;
const password=req.body.password;
const useremail=await Login.findOne({email:email});
if (useremail.password===password){
byuser = email
uname = useremail.firstname + ' ' +useremail.lastname
console.log(uname)
const datas = await expenseA.findOne({byuser:byuser})
exana = datas
await piechart()
res.redirect("/") //redirect
}else{
res.send("incorrect");
}
} catch (error) {
console.log(error);
res.status(400).send("invalid email")
}
})
const budget_for_new = async (byuser)=>{
}