forked from Estevemartin/gestordecontratos
/
routes.js
550 lines (498 loc) · 21 KB
/
routes.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*jshint -W033 */
const express = require('express');
const router = express.Router();
const bcrypt = require("bcryptjs");
const fs = require('fs-extra')
const multer=require('multer')
const upload=multer({dest:"uploadedContracts"})
const path=require('path')
const XLSX = require('xlsx');
const nodemailer=require('nodemailer')
const User = require('./models/user-model');
const Contract = require('./models/contract-model');
const { Console } = require('console');
const { findOne } = require("./models/user-model");
router.get('/',(req,res,next)=>{
// console.log(req.session)
let template = {
layout: false
}
if (req.session.currentUser) {
res.redirect('/displayPendingContracts');
} else {
res.render('login-register/login', template);
}
})
router.get('/register',(req,res,next)=>{
// if (req.session!==undefined) {
// res.render('contracts');
// } else {
let template = {
layout: false
}
res.render('login-register/register', template);
// }
})
router.post('/register',async (req,res,next)=>{
const{username, usersurname, email, repeatemail, password, repeatedpassword, role} = req.body;
var errorMsg = [];
errorMsg = await createErrorMsgRegister(username, usersurname, email, repeatemail, password, repeatedpassword, role)
formData={
errorMsg:errorMsg,
succesMsg:null,
username:username,
usersurname:usersurname,
email:email,
repeatemail:repeatemail,
password:password,
repeatedpassword:repeatedpassword,
role:role
};
if (errorMsg.length===0){
const bcryptSalt = 10;
const salt = bcrypt.genSaltSync(bcryptSalt);
const hashPass = bcrypt.hashSync(password, salt);
await User.create({name:username,surname:usersurname,email,password:hashPass,role});
formData.succesMsg="User succesfully created.";
res.render("login-register/login",{formData, layout: false});
} else{
res.render("login-register/register",{formData, layout: false});
}
})
router.post('/', async(req,res,next)=>{
console.log("Entering Login POST Method")
const{email, password} = req.body;
var errorMsg = '';
//Validates that the fields are not empty
if (email==="" && password ===""){
errorMsg = "You forgot to write your email and password." //Email and Password not Filled.
formData={errorMsg:errorMsg,layout:false}
res.render("login-register/login",formData); //Render Login and Error Message.
}else if (email===""){
errorMsg = "You forgot to write your email." //Email not Filled.
formData={errorMsg:errorMsg,layout:false}
res.render("login-register/login",formData); //Render Login and Error Message.
}else if (password ===""){
errorMsg = "You forgot to write your password." //Password not Filled.
formData={errorMsg:errorMsg,email:email,layout:false}
res.render("login-register/login",formData); //Render Login and Error Message.
} else {
const user = await User.findOne({ email: email }); //Search User to BD by Email.
if (!user){
errorMsg="This email doesn't exist." //User doesn't exists.
formData={errorMsg:errorMsg,email:email,layout:false}
res.render("login-register/login",formData);
} else{
//User Exists.
if (bcrypt.compareSync(password, user.password)) { //Check if password match.
req.session.currentUser = user; //Save User Session.
// await deleteDir(path.join(__dirname,"uploadedContracts"))
res.redirect("/displayPendingContracts") //Redirect to home.
}else{
errorMsg="Incorrect email or password." //Password is inccorrect.
formData={errorMsg:errorMsg,email:email,layout:false}
res.render("login-register/login",formData); //Render Login and Error Message.
}
}
}
})
router.get('/logout', (req, res, next) => {
req.session.destroy((err) => {
res.redirect('/');
})
})
router.get('/createContract',(req,res,next)=>{
if (req.session!==undefined) {
res.render('contracts');
} else {
res.redirect('/');
}
})
router.post("/uploadNewContractToDB",upload.any(), async (req,res)=>{
if (req.session===undefined) {
res.redirect("/")
}else{
console.log("--------------- UPLOADING NEW CONTRACT ---------------");
// console.log(req.files)
if (req.files.length===0){
noFileSelected="No se ha seleccionado ningún contrato.";
res.render("contracts.hbs",{noFileSelected});
}else {
//Save Excel document to "temporaryFiles" folder to read it
let tempFolder = path.join(__dirname,"temporaryFiles");
let tempFile = path.join(tempFolder,req.files[0].originalname);
let ext = tempFile.substr(tempFile.lastIndexOf('.') + 1);
console.log("Extensión: " ,ext)
if (ext!=="xlsx" && ext!=="xls" && ext!=="xlsm"){
noFileSelected="La hoja de firmas no se encuentra en formato Excel."
res.render("contracts.hbs",{noFileSelected});
}
await saveFile(req.files[0].path,tempFile)
//Reads Excel File Variables
const pq=readExcel(tempFile,'V7');
const pqFolderName = editPQ(pq)
const comercial=readExcel(tempFile,'F7');
const cliente=readExcel(tempFile,'F9');
const obra=readExcel(tempFile,'E11');
const usuarioFinal=readExcel(tempFile,'G13');
const nPedido=readExcel(tempFile,'W11');
const importe=readExcel(tempFile,'G17');
const fechaStatusWon=readExcel(tempFile,'H19');
const fechaRecepcion=readExcel(tempFile,'U19');
// console.log("PQ: " + pq + " | Comercial: " + comercial + " | Cliente: " + cliente + " | Obra: " + obra + " | Usuario Final: " + usuarioFinal + " | Nº de Pedido: " + nPedido + " | Importe: " + importe + " | Fecha Status Won: " + fechaStatusWon + " | Fecha Recepción: " + fechaRecepcion);
// await deleteFile(tempFile)
var errorMsg = createErrorMessageOnNewContract(pq,comercial,cliente,obra,usuarioFinal,nPedido,importe,fechaStatusWon,fechaRecepcion)
//Check that the contract doesn't exists in the DB.
const contract = await Contract.findOne({ pq: pq });
if (contract!==null){errorMsg.push("The contract "+pqFolderName+" already exists. Edit the existing contract.")}
if (errorMsg.length>0){
deleteFile(tempFile)
res.render("contracts.hbs",{errorMsg});
} else {
//Create PQ Folder
let contractsFolder = path.join(__dirname, "contracts");
const pqFolder=path.join(contractsFolder,pqFolderName);
let uploadedFiles=[]
//Save all the files to PQ Folder
for (i=0;i<req.files.length;i++){
let uploadedFile=req.files[i]
let fileToSave = path.join(pqFolder,uploadedFile.originalname)
await saveFile(uploadedFile.path,fileToSave)
uploadedFiles.push(fileToSave)
}
//Create Contract to DB
await Contract.create({pq,comercial,cliente,obra,usuarioFinal,nPedido,importe,fechaStatusWon,fechaRecepcion,uploadedFiles});
emailParams={
host:"smtp-mail.outlook.com",
port:587,
secure:false,
auth: {
user: "",
pass: ""
},
from:'"Esteve Martín - MPA Solutions"<estevemartinmauri@hotmail.com>',
to:"esteve.martin@mpasolutions.es",
subject:"Test Email",
html: "<b> NO ME CREO QUE HAYA LLEGADO </b>",
attachments:uploadedFiles
}
const contractList = await Contract.find({visible:true,mainStatus:"Pending"},'pq cliente importe comercial')
console.log(contractList)
// //Send Email
// await sendEmail(emailParams)
formData={
succesMsg:"Contrato "+pq+" Creado Correctamente.",
contractList:contractList
}
res.render("contracts.hbs",{formData});
}
}
}
});
router.get("/displayClosedContracts", async (req,res)=>{
if (req.session===undefined) {
res.redirect("/")
}else{
const contractList = await Contract.find({visible:true,mainStatus:"Closed"},'pq cliente importe comercial')
// console.log(contractList)
contractList.forEach(pq=>{
pq.importe = numberToCurrency(pq.importe)
})
// contractList.importe=numberToCurrency(contractList.importe)
// console.log(contractList)
formData={
showClosed:true,
contractList:contractList
}
res.render("contracts.hbs",{formData});
}
})
router.get("/displayPendingContracts", async (req,res)=>{
const errorMsg = req.query.errorMsg
console.log("ERROR MESSAGE THROUGH QUERY: ", errorMsg)
await deleteDir(path.join(__dirname,"uploadedContracts"))
if (req.session===undefined) {
res.redirect("/")
}else{
const contractList = await Contract.find({visible:true,mainStatus:"Pending"},'pq cliente importe comercial')
// console.log(contractList)
contractList.forEach(pq=>{
pq.importe = numberToCurrency(pq.importe)
})
// console.log(contractList)
formData={
errorMsg:errorMsg,
showClosed:false,
contractList:contractList
}
res.render("contracts.hbs",{formData});
}
})
router.get("/deleteContract/:id",async(req,res)=>{
// console.log(req.params.id)
const id=req.params.id
// console.log("GET INSIDE ID: ", id)
await Contract.findByIdAndUpdate({_id:id},{visible:false})
res.redirect("/displayPendingContracts")
})
router.post("/rejectContract/:id",async(req,res)=>{
console.log("ENTERED REJECT CONTRACT / ID")
const {role,reason}=req.body
const id=req.params.id
// const id=req.params.id
// const role=req.params.role
// const reason=req.params.reason
// const userName = req.session.currentUser.userName
// const userSurname = req.session.currentUser.userSurname
// console.log(id)
// console.log(userName)
// console.log(userSurname)
console.log(role)
console.log(reason)
errorMsg = createErrorMsgReject(role,reason)
console.log(errorMsg)
let contract = await Contract.find({_id:id})
let historico = contract[0].historico
console.log(historico)
if (errorMsg!==""){
res.redirect('/displayPendingContracts?errorMsg='+errorMsg)
}else{
//Save Reject Action
//Send Rejection Email
successMsg = "Contrato Rechazado Correctamente"
res.redirect('/displayPendingContracts?successMsg='+successMsg)
}
// res.render("contracts",{errorMsg})
})
async function sendEmail(emailParams){
let attachmentsObj = []
for (i=0;i<emailParams.attachments.length;i++){
attachmentsObj.push(
{
path:emailParams.attachments[i],
filename:emailParams.attachments[i].split("\\")[emailParams.attachments[i].split("\\").length-1]
}
)
}
// console.log(attachmentsObj)
let transporter = nodemailer.createTransport({
host: emailParams.host,
port: emailParams.port,
secure:false,
auth: {
user: emailParams.auth.user,
pass: emailParams.auth.pass
}
})
let info=await transporter.sendMail({
from: emailParams.from,
to: emailParams.to,
subject: emailParams.subject,
html: emailParams.html,
attachments:attachmentsObj
})
}
async function deleteFile (filePath) {
try {
// console.log(filePath)
await fs.remove(filePath)
// console.log('File Removed: '+filePath)
} catch (err) {
console.error(err)
}
}
async function createDirectory(dir){
try{
await fs.ensureDir(dir)
// console.log("Directory Created: " +dir)
} catch (err){
console.error(err)
}
}
async function saveFile(src,dest){
try{
await fs.ensureLink(src,dest)
// console.log("File Saved from: " + src + " to " + dest)
} catch (err){
console.error(err)
}
}
async function deleteDir(dir){
console.log("deleting uploadedContracts")
try {
await fs.remove(dir)
console.log('Uploaded Contracts Deleted!')
} catch (err) {
console.error(err)
}
}
function editPQ(pq){
if (pq.split('-').length-1===2){return pq;}
if (pq===undefined){return ""}else{return pq.split('-')[0] + "-"+pq.split('-')[1];}
}
function createErrorMessageOnNewContract(pq,comercial,cliente,obra,usuarioFinal,nPedido,importe,fechaStatusWon,fechaRecepcion){
var errorMsg = [];
//Check Empty Variables.
if (pq===undefined){errorMsg.push('PQ');}
if (comercial ==undefined){errorMsg.push('nombre del comercial');}
if (cliente ==undefined){errorMsg.push('cliente');}
if (obra ==undefined){errorMsg.push('obra');}
if (usuarioFinal ==undefined){errorMsg.push('usuario final');}
if (nPedido ==undefined){errorMsg.push('Nº de pedido');}
if (importe ==undefined){errorMsg.push('importe');}
if (fechaStatusWon ==undefined){errorMsg.push('fecha de status Won');}
if (fechaRecepcion ==undefined){errorMsg.push('fecha de recepción del contrato');}
//Create the Error Message Parts.
let errorMsgStart = "Los campos "
let emptyFields = ""
let errorMsgEnd = " se encuentran vacíos en la hoja de firmas."
//Concatenates empty fields.
for(i=0;i<errorMsg.length;i++){
if (i!== errorMsg.length){
emptyFields += errorMsg[i]+", "
}else{
emptyFields += errorMsg[i]
}
}
//Create the error message to be returned.
if(emptyFields!==""){
returnErrorMsg = [errorMsgStart+emptyFields+errorMsgEnd]
} else {
returnErrorMsg = []
}
return returnErrorMsg;
}
function readExcel(excelPath,cell){
var workbook = XLSX.readFile(excelPath);
var first_sheet_name = workbook.SheetNames[0];
var address_of_cell = cell;
var worksheet = workbook.Sheets[first_sheet_name];
var desired_cell = worksheet[address_of_cell];
var desired_value = (desired_cell ? desired_cell.v : undefined);
if (cell==='H19' || cell==="U19"){
// console.log("Previous Value: " +desired_value)
if(desired_value!==undefined){desired_value=ExcelDateToJSDate(desired_value).toLocaleString().split(' ')[0]}
// desired_value=SSF.format(fmt:Number,val:desired_value)
// console.log("After Value: " + desired_value)
}
// console.log(desired_value)
return desired_value;
}
function validateEmail(email) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
function ExcelDateToJSDate(serial) {
var utc_days = Math.floor(serial - 25569);
var utc_value = utc_days * 86400;
var date_info = new Date(utc_value * 1000);
// var fractional_day = serial - Math.floor(serial) + 0.0000001;
// var total_seconds = Math.floor(86400 * fractional_day);
// var seconds = total_seconds % 60;
// total_seconds -= seconds;
// var hours = Math.floor(total_seconds / (60 * 60));
// var minutes = Math.floor(total_seconds / 60) % 60;
returnDate = new Date(date_info.getFullYear(), date_info.getMonth(), date_info.getDate());
returnDate=returnDate.toLocaleString().split(' ')[0]
var year=returnDate.split('-')[0]
var month=returnDate.split('-')[1]
var day=returnDate.split('-')[2]
if (month.length===1){month='0'+month}
returnDate=day+"/"+month+"/"+year
// console.log(returnDate)
return returnDate;
}
async function createErrorMsgRegister(username, usersurname, email, repeatemail, password, repeatedpassword, role){
let resultErrorMsg=[]
//INSERT ERRORS (Validates that the fields are not empty).
let insertErrorMsg = []
let insertErrorMsgOutPut=''
if (username=== ""||username ==null){insertErrorMsg.push('name')}
if (usersurname=== ""||usersurname ==null){insertErrorMsg.push('surname')}
if (email=== ""||email ==null){insertErrorMsg.push('email')}
if (password=== ""||password ==null){insertErrorMsg.push('password')}
if (role=== ""||role ==null || role === "Select your Role and Department"){insertErrorMsg.push('role/department')}
switch (insertErrorMsg.length){
case 1:
insertErrorMsgOutPut = "You forgot to fill your " + insertErrorMsg[0] + "."
break;
case 2:
insertErrorMsgOutPut = "You forgot to fill your " + insertErrorMsg[0] + " and " + insertErrorMsg[1] + "."
break;
case 3:
insertErrorMsgOutPut = "You forgot to fill your " + insertErrorMsg[0] + ", " + insertErrorMsg[1] + " and " + insertErrorMsg[2] + "."
break;
case 4:
insertErrorMsgOutPut = "You forgot to fill your " + insertErrorMsg[0] + ", " + insertErrorMsg[1] + ", " + insertErrorMsg[2] + " and " + insertErrorMsg[3] + "."
break;
case 5:
insertErrorMsgOutPut = "You forgot to fill your " + insertErrorMsg[0] + ", " + insertErrorMsg[1] + ", " + insertErrorMsg[2] + ", " + insertErrorMsg[3] + " and " + insertErrorMsg[4] + "."
break;
}
if (insertErrorMsgOutPut!==''){
resultErrorMsg.push(insertErrorMsgOutPut)
}
//REPEAT ERRORS (Validates that you repeated email and password).
let repeatErrorMsgOutPut=''
let repeatErrorMsg=[]
if (repeatemail=== "" && !insertErrorMsgOutPut.includes('email')){repeatErrorMsg.push('email')}
if (repeatedpassword=== "" && !insertErrorMsgOutPut.includes('password')){repeatErrorMsg.push('password')}
switch (repeatErrorMsg.length){
case 1:
repeatErrorMsgOutPut = "You forgot to repeat your " + repeatErrorMsg[0] + "."
break;
case 2:
repeatErrorMsgOutPut = "You forgot to repeat your " + repeatErrorMsg[0] + " and " + repeatErrorMsg[1] + "."
break;
}
if (repeatErrorMsgOutPut!==''){
resultErrorMsg.push(repeatErrorMsgOutPut)
}
//MATCH ERRORS (Validdates email and passwords match).
let matchErrorMsgOutPut=''
let matchErrorMsg=[]
if (email!==repeatemail && !repeatErrorMsgOutPut.includes('email') && !insertErrorMsgOutPut.includes('email')){matchErrorMsg.push('emails')}
if (password!==repeatedpassword && !repeatErrorMsgOutPut.includes('password') && !insertErrorMsgOutPut.includes('password')){matchErrorMsg.push('passwords')}
switch (matchErrorMsg.length){
case 1:
matchErrorMsgOutPut = "The " + matchErrorMsg[0] + " doesn't match."
break;
case 2:
matchErrorMsgOutPut = "The " + matchErrorMsg[0] + " and the " + matchErrorMsg[1] + " doesn't match."
break;
}
if (matchErrorMsgOutPut!==''){
resultErrorMsg.push(matchErrorMsgOutPut)
}
//Validates Password Lenght
if (password.length<6 && !repeatErrorMsgOutPut.includes('password') && !insertErrorMsgOutPut.includes('password')){resultErrorMsg.push("The password must have at least 6 characters.")}
// console.log(resultErrorMsg)
return resultErrorMsg
}
function createErrorMsgReject(role,reason){
if(role === "Select your Role and Department" && reason ==="Select a reason"){
errorMsg = "Select a role and a reason."
} else if (role === "Select your Role and Department"){
errorMsg = "Select a role."
} else if (reason ==="Select a reason"){
errorMsg = "Select a reason."
} else {
errorMsg = ""
}
return errorMsg;
}
function numberToCurrency(number){
// console.log(number)
result = new Intl.NumberFormat("de-DE" ,{style: "currency", currency: "EUR"}).format(number)
result = result.slice(2)+"€"
result = result.replace(".","!").replace(",",".").replace("!",",").replace(",00","")
// console.log(result)
return result
}
router.get('/editContracts',(req,res,next)=>{
res.render('editContracts')
});
router.get('/alertsContracts',(req,res,next)=>{
res.render('alertsContracts')
});
module.exports=router;