-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
408 lines (357 loc) · 12.1 KB
/
index.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
var express = require("express");
var app = express();
const nodemailer = require('nodemailer');
const bcrypt = require("bcrypt");
// Generate a salt for hashing
const salt = bcrypt.genSaltSync(10);
var mysql = require("mysql");
var multer = require("multer"); // Import Multer
var bodyParser = require("body-parser");
var cors = require("cors");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set("view engine", "ejs");
var conn = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "webproject",
});
conn.connect(function (err) {
if (err) throw err;
console.log("Connection Sucessful");
});
app.get("/", function (req, res) {
res.render("form");
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads"); // Directory where uploaded files will be stored
},
filename: function (req, file, cb) {
cb(null, Date.now() + "-" + file.originalname); // Set the file name
},
});
var upload = multer({ storage: storage });
var recipt;
app.post("/insert", upload.single("image"), function (req, res) {
var firstname = req.body.firstname;
var lastname = req.body.lastname;
var email = req.body.email;
var password = req.body.password;
var cpassword = req.body.cpassword;
var phoneno = req.body.phoneno;
var latitude = req.body.latitude;
var longitude = req.body.longitude;
var domain = req.body.domain;
var companyname = req.body.companyname;
var image = req.file.filename;
const phash = bcrypt.hashSync(password, salt);
if (bcrypt.compareSync(cpassword, phash)) {
var sql = `insert into users(firstname, lastname, email, password, phone, latitude, longitude, domain,companyname,image) values('${firstname}', '${lastname}', '${email}', '${phash}', '${phoneno}', '${latitude}', '${longitude}', '${domain}','${companyname}','${image}')`;
conn.query(sql, function (err, results) {
if (err) throw err;
var data = {
res: 1
};
res.send(JSON.stringify(data));
});
}
else {
var data = {
res: -1
};
res.send(JSON.stringify(data));
}
});
app.post("/insertproject", upload.single("image"), function (req, res) {
var resu;
var fid;
var pname = req.body.pname;
var pd = req.body.pd;
var pdate = req.body.pdate;
var pm = req.body.pm;
var ps = req.body.ps;
var loc = req.body.loc;
var budget = req.body.budget;
var mater = req.body.mater;
var phasename = req.body.phasename;
var phasedes = req.body.phasedes;
var phasebudget = req.body.phasebudget;
var phasedate = req.body.phasedate;
var image = req.file.filename;
var totalphases = req.body.totalphases;
var noofplot = req.body.noofplot;
phasename = phasename.split(",");
phasebudget = phasebudget.split(",");
phasedes = phasedes.split(",");
phasedate = phasedate.split(",");
console.log(phasename);
console.log(phasedes);
var sql = `insert into projects(pname, pdescription, pduration, pmanager, status, location, budget, material,image,totalphases,plots) values('${pname}', '${pd}', '${pdate}', '${pm}', '${ps}', '${loc}', '${budget}', '${mater}', '${image}','${totalphases}','${noofplot}')`;
conn.query(sql, function (err, results) {
if (err) throw err;
});
var sql2 = `select id from projects ORDER BY id DESC LIMIT 1`;
conn.query(sql2, function (err, results) {
if (err) throw err;
console.log(results);
if (results.length > 0) {
var id = results[0].id;
fid = id;
}
});
for (var i = 0; i < phasename.length; i++) {
var sql = `insert into phases(name, description, budget,duration, projectid,status) values('${phasename[i]}', '${phasedes[i]}', '${phasebudget[i]}', '${phasedate[i]}', (select id from projects ORDER BY id DESC LIMIT 1),'not active')`;
console.log(sql);
conn.query(sql, function (err, results) {
if (err) throw err;
resu = results;
});
}
res.send(resu);
});
app.post("/login", function (req, res) {
var email = req.body.email;
var password = req.body.password;
var sql = `Select * from users where email = '${email}'`;
conn.query(sql, function (err, results) {
if (err) throw err;
if (results.length > 0) {
results.forEach(function (obj) {
if (bcrypt.compareSync(password, obj.password) && obj.domain == "user") {
var data = {
id: obj.ID
};
res.send(JSON.stringify(data));
}
else {
var data = {
id: -1
};
res.send(JSON.stringify(data));
}
});
}
else {
var data = {
id: -1
};
console.log(data);
res.send(JSON.stringify(data));
}
});
});
app.get("/getproject", function (req, res) {
var sql = `Select p.id,p.pname,p.status as pstatus,p.pdescription,ph.name,ph.status,p.totalphases,p.completephase from projects p join phases ph on p.id = ph.projectid`;
conn.query(sql, function (err, results) {
if (err) throw err;
res.send(results);
});
});
app.get("/getprojectuser", function (req, res) {
var sql = `Select p.id,p.pname,p.status,p.pdescription,p.totalphases,p.completephase,p.plots,p.bookedplots from projects p`;
conn.query(sql, function (err, results) {
if (err) throw err;
res.send(results);
});
});
app.post("/getprojectupdate", function (req, res) {
var id = req.body.id;
var sql = `Select p.id,p.pname,p.status as pstatus,p.pdescription,p.budget,p.totalphases,p.completephase,p.pmanager,ph.name,ph.status,ph.id as phaseid from projects p join phases ph on p.id = ph.projectid && p.id = ${id}`;
conn.query(sql, function (err, results) {
if (err) throw err;
res.send(results);
});
});
app.post("/getindividualproject", function(req,res){
console.log("hello");
var id = req.body.id;
var sql = `Select id,pname,pdescription,image,plots,bookedplots,budget,location from projects where id = ${id}`;
conn.query(sql, function (err, results) {
if (err) throw err;
res.send(results);
});
});
app.get("/getallprojectorder", function(req,res){
var sql = `Select id,pname,pdescription,image,plots,bookedplots from projects`;
conn.query(sql, function (err, results) {
if (err) throw err;
res.send(results);
});
});
app.post("/updateproject", upload.single("image"), function (req, res) {
var result;
var id = req.body.id;
var pname = req.body.pname;
var pdescription = req.body.pdescription;
var pmanager = req.body.pmanager;
var image = req.file.filename;
var pstatus = req.body.pstatus;
var budget = req.body.budget;
var phasedata = req.body.phasedata;
var phasesids = req.body.phasesids;
var comphases = req.body.comphases;
console.log(phasedata);
console.log(phasesids);
phasesids= phasesids.split(",");
phasedata= phasedata.split(",");
var query1 = `UPDATE projects SET pname = ?, pdescription = ?, pmanager = ?, image = ? , status = ? , budget = ? , completephase = ? WHERE id = ?`;
var data = [pname, pdescription, pmanager, image, pstatus, budget, comphases, id];
conn.query(query1, data, function (err, results) {
if (err)
throw err;
});
console.log(phasesids.length);
for (var i = 0; i < phasesids.length; i++) {
var sql = `Update phases set status = '${phasedata[i]}' where id = ${phasesids[i]}`;
conn.query(sql, function (err, results) {
if (err)
throw err;
result = results;
});
}
res.send(result);
});
app.post("/loginadmin", function (req, res) {
var email = req.body.email;
var password = req.body.password;
var sql = `Select * from users where email = '${email}'`;
conn.query(sql, function (err, results) {
if (err) throw err;
if (results.length > 0) {
results.forEach(function (obj) {
if (bcrypt.compareSync(password, obj.password) && obj.domain == "company") {
var data = {
id: obj.ID
};
res.send(JSON.stringify(data));
}
else {
var data = {
id: -1
};
res.send(JSON.stringify(data));
}
});
}
else {
var data = {
id: -1
};
console.log(data);
res.send(JSON.stringify(data));
}
});
});
app.post("/bookplot",function(req,res){
var pid = req.body.id;
var uid = req.body.userid;
var sql = `Update projects set plots = plots - 1 , bookedplots = bookedplots + 1 where id = '${pid}'`;
var sql2 = `insert into Porder (uid,pid,status) values ('${uid}','${pid}','inprogress')`;
conn.query(sql,function (err, results) {
if(err)
throw err;
});
conn.query(sql2,function (err, results) {
if(err)
throw err;
res.send(results);
});
});
app.get("/showallorders",function (req, res) {
var sql = `(SELECT (SELECT Concat(firstname," ",lastname) FROM users where id = po.uid) as uname, (SELECT pname FROM projects where id = po.pid) as pname,(SELECT location FROM projects where id = po.pid) as location, status from porder po)`
conn.query(sql, function (err, results) {
res.send(results);
});
});
app.get("/showpendingorders",function (req, res) {
var sql = `(SELECT id,(SELECT Concat(firstname," ",lastname) FROM users where id = po.uid) as uname, (SELECT pname FROM projects where id = po.pid) as pname,(SELECT location FROM projects where id = po.pid) as location, status from porder po where status = "inprogress");`
conn.query(sql, function (err, results) {
res.send(results);
});
});
app.post("/updateOrderList",function (req, res) {
var id = req.body.id;
var status = req.body.status;
var sql = `Update porder set status = '${status}' where id = '${id}'`;
conn.query(sql, function (err, results) {
res.send(results);
});
});
app.get("/dashboarddata1",function (req, res) {
var sql = `Select p.pname,p.pmanager,p.plots from projects p`;
conn.query(sql, function (err, results) {
res.send(results);
});
});
app.get("/dashboarddata2",function (req, res) {
var sql2 = `Select count(*) as projects from projects`;
conn.query(sql2, function (err, results) {
res.send(results);
});
});
app.get("/dashboarddata4",function (req, res) {
var sql4 = `Select sum(plots) as plots from projects`;
conn.query(sql4, function (err, results) {
res.send(results);
});
});
app.get("/dashboarddata3",function (req, res) {
var sql4 = `Select count(*) as users from users`;
conn.query(sql4, function (err, results) {
res.send(results);
});
});
app.get("/dashboarddata5",function (req, res) {
var sql4 = `Select count(*) as orders from porder`;
conn.query(sql4, function (err, results) {
res.send(results);
});
});
app.post("/getdata", function (req, res) {
var id = req.body.id;
var sql = `Select * from users where id = '${id}'`;
conn.query(sql, function (err, results) {
res.send(results);
});
});
var otpCode;
app.post("/sendotp", function (req, res) {
recipt = req.body.email;
sendOTP();
var data = { otpCode: otpCode };
res.send(data);
});
// Create a transporter object with your email service provider credentials
function sendOTP() {
console.log(recipt);
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'xwrabjsaxaxxabpn'
}
});
// Generate a random OTP code
otpCode = Math.floor(1000 + Math.random() * 9000);
// Define the email content
const mailOptions = {
from: '[email protected]',
to: `${recipt}`,
subject: 'OTP Code',
text: `Your OTP code is: ${otpCode}`
};
// Send the email
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
var server = app.listen(4000, function () {
console.log("App running on port 4000 s");
});