-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.js
564 lines (532 loc) · 18.1 KB
/
edit.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
const routes = require("express").Router();
let database = require("../services/database");
const fs = require("fs");
let pdf_export = require("../middleware/export");
let diffPdf = require("../middleware/diff");
let accounting_pdf_export = require("../middleware/export_glk");
const tooltipsCsv = require("../lib/tooltips");
const diff = require("diff");
const { body, check, validationResult } = require("express-validator");
var multer = require("multer");
const { authUser, authRole, authAgency } = require("../middleware/auth");
const tables = require("../lib/tables");
const ministries = require("../lib/ministries");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./public/uploads/");
},
filename: function (req, file, cb) {
cb(null, Date.now() + "_" + file.originalname); //Date().toLocaleString("el-GR", { timeZone: "Europe/Athens" })
},
});
var upload = multer({ storage: storage }).fields([
{ name: "field_21_upload", maxCount: 10 },
{ name: "field_23_upload", maxCount: 10 },
{ name: "field_36_upload", maxCount: 10 },
{ name: "signed_pdf_upload", maxCount: 1 },
{ name: "nomosxedio" },
{ name: "signed_glk_pdf_upload", maxCount: 1 },
]);
routes.get(
"/:entry_id",
authUser,
authRole,
authAgency,
async (req, res, next) => {
try {
let entry = await database.analysis.findOne({
where: {
id: req.params.entry_id,
},
}); //TODO: add error handling
const user = req.session.user;
pdf_name = `${entry.title}.pdf`;
pdf_name = pdf_name.replace(/\s+/g, ""); //buggy?
var pdf_exists;
fs.existsSync(`./public/exports/${pdf_name}`)
? (pdf_exists = true)
: (pdf_exists = false);
const data = entry.dataValues.data;
const accountingData = entry.dataValues.accountingData;
const uploads = entry.dataValues.uploads;
const accountingUploads = entry.dataValues.accountingUploads;
const type = entry.dataValues.type;
const id = entry.dataValues.id;
const status = entry.dataValues.status;
const indexesResult = await database.indexes.findAll();
const indexTablesResult = await database.index_tables.findAll();
const indexes = {};
const indexTables = [];
for (i in indexTablesResult) {
indexTables.push(indexTablesResult[i].dataValues.name);
}
for (let i in indexTables) {
indexes[`${indexTables[i]}`] = [];
for (let j in indexesResult) {
if (
indexesResult[i].dataValues.id ===
indexesResult[j].dataValues.indexTableId
) {
indexes[`${indexTables[i]}`].push(indexesResult[j].name);
}
}
indexes[`${indexTables[i]}`].sort();
}
const field_18 = await tables.getCheckboxTableData(
data,
"field_18",
true
);
const field_19 = await tables.getCheckboxTableData(
data,
"field_19",
true
);
const field_20 = await tables.getCheckboxTableData(
data,
"field_20",
true
);
const field_14 = await tables.getTableData(
["field_14_arthro", "field_14_stoxos"],
data
);
const field_29 = await tables.getTableData(
["field_29_diatakseis_rythmisis", "field_29_yfistamenes_diatakseis"],
data
);
const field_30 = await tables.getTableData(
["field_30_diatakseis_katargisi", "field_30_katargoumenes_diatakseis"],
data
);
const field_31 = await tables.getTableData(
[
"field_31_sxetiki_diataksi",
"field_31_synarmodia_ypoyrgeia",
"field_31_antikeimeno_synarmodiotitas",
],
data
);
const field_32 = await tables.getTableData(
[
"field_32_eksousiodotiki_diataksi",
"field_32_eidos_praksis",
"field_32_armodio_ypoyrgeio",
"field_32_antikeimeno",
"field_32_xronodiagramma",
],
data
);
const signatories = await tables.getTableData(
["minister_name", "minister_role", "minister_ministry"],
data
);
const field_17_signatories = await tables.getTableData(
[
"field_17_minister_name",
"field_17_minister_role",
"field_17_minister_ministry",
],
accountingData
);
const processes = await tables.getTableData(["process"], data);
const field_9 = await tables.getField9(data);
const tooltips = JSON.stringify(await tooltipsCsv.getTooltips());
const ministriesResult = await ministries.getMinistries();
const unsortedMinistries = await ministries.getUnsortedMinistries();
const ministersResult = await ministries.getMinisters(unsortedMinistries);
res.render("edit_analysis", {
//TODO: review endpoint name
id: id,
type: type,
status: status,
data: data,
accountingData: accountingData,
accountingUploads: accountingUploads[0],
tables: {
field_9: field_9,
field_14: field_14,
field_17_signatories: field_17_signatories,
field_18: field_18,
field_19: field_19,
field_20: field_20,
field_29: field_29,
field_30: field_30,
field_31: field_31,
field_32: field_32,
processes: processes,
signatories: signatories,
}, // TODO: create tables
role: req.session.user.role,
pdf_exists: pdf_exists,
tooltips: tooltips,
ministries: ministriesResult,
ministers: ministersResult,
user: user,
indexes: indexes,
uploads: uploads[0],
});
} catch (err) {
console.log("error: " + err);
}
}
);
routes.post(
"/:entry_id/export/accounting",
authUser,
authRole,
authAgency,
accounting_pdf_export.exportGlk
);
routes.post(
"/:entry_id/export",
authUser,
authRole,
authAgency,
pdf_export.exportPDF
);
////////////////////////////////////////////////////////////////////////////////////////////////////////
routes.put(
"/:entry_id",
authUser,
authRole,
authAgency,
upload,
//VALIDATION RULES
// [check('title', 'Ο τίτλος είναι υποχρεωτικός.').notEmpty(),
// check('title').custom( async(value) => {
// var title = await database.ekthesi.count({ where: {title: value}})//count tables which have given title
// console.log(title)
// if (title > 1) {//title already exists
// return Promise.reject('Υπάρχει ήδη ανάλυση με αυτόν τον τίτλο.');
// }
// }),
// check('epispeudon_foreas', 'Ο επισπεύδων φορέας είναι υποχρεωτικός.').notEmpty(),
// body('field_10_amesi_comments').if(body('field_10_amesi').notEmpty()).notEmpty().withMessage('Εάν είναι άμεση, εξηγήστε.'),
// body('field_10_emmesi_comments').if(body('field_10_emmesi').notEmpty()).notEmpty().withMessage('Εάν είναι έμμεση, εξηγήστε.'),
// body('field_11_comments')
// .if(body('field_10_emmesi').notEmpty())//if user checked field_10_amesi
// .if(body('field_10_amesi').notEmpty())//or if user checked field_10_emmesi
// .notEmpty().withMessage('Το πεδίο 11 είναι υποχρεωτικό.'),//field_11 must be filled in
// body('field_12_comments').if(body('field_10_emmesi').notEmpty()).if(body('field_10_amesi').notEmpty()).notEmpty().withMessage('Το πεδίο 12 είναι υποχρεωτικό.'),
// body('field_13_comments').if(body('field_10_emmesi').notEmpty()).if(body('field_10_amesi').notEmpty()).notEmpty().withMessage('Το πεδίο 13 είναι υποχρεωτικό.'),
// body('field_34').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 34 είναι υποχρεωτικό.'),
// body('field_35').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 35 είναι υποχρεωτικό.'),
// body('field_36').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 36 είναι υποχρεωτικό.'),
// body('field_37').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 37 είναι υποχρεωτικό.'),
// body('field_38').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 38 είναι υποχρεωτικό.'),
// body('field_39').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 39 είναι υποχρεωτικό.'),
// body('field_40').if(body('field_33').notEmpty()).notEmpty().withMessage('Το πεδίο 40 είναι υποχρεωτικό.'),
// ],
async function (req, res, next) {
let analysis_id = req.params.entry_id;
const errors = validationResult(req);
if (!errors.isEmpty()) {
// if array exists
console.log(errors);
//return res.status(422).json(errors.array());
} else {
try {
const entry = await database.analysis.findOne({
where: {
id: req.params.entry_id,
},
});
const field21 = entry.uploads[0].field21;
const field23 = entry.uploads[0].field23;
const field36 = entry.uploads[0].field36;
const signed_pdf = [];
// const bill = entry.uploads[0].bill;
const bill = [];
try {
const file = req.files;
if (file.field_21_upload) {
for (i in file.field_21_upload) {
field21.push(file.field_21_upload[i].filename);
}
}
if (file.field_23_upload) {
for (i in file.field_23_upload) {
field23.push(file.field_23_upload[i].filename);
}
}
if (file.field_36_upload) {
for (i in file.field_36_upload) {
field36.push(file.field_36_upload[i].filename);
}
}
if (file.signed_pdf_upload) {
console.log(file.signed_pdf_upload);
for (i in file.signed_pdf_upload) {
signed_pdf.push(file.signed_pdf_upload[i].filename);
}
}
if (file.nomosxedio) {
bill.push({
filename: file.nomosxedio[0].filename,
upload_date: req.body.last_updated,
});
}
} catch (e) {
console.log("Error message: " + e.message);
}
const uploads = [
{
field21: field21,
field23: field23,
field36: field36,
bill: bill,
signed_pdf: signed_pdf,
},
];
let analysis;
if (req.body.status === "Κατατέθηκε") {
analysis = await database.analysis.update(
{
uploads: uploads,
author: req.session.user.taxId,
status: req.body.status,
},
{
where: {
id: analysis_id,
},
}
);
await database.audit.create({
authorTaxId: req.session.user.taxId,
data: entry.data,
type: entry.type,
status: req.body.status,
timestamp: new Date().toLocaleString("el-GR", {
timeZone: "Europe/Athens",
}),
action: req.method,
auditId: analysis_id,
});
} else {
analysis = await database.analysis.update(
{
data: req.body,
uploads: uploads,
author: req.session.user.taxId,
status: req.body.status,
},
{
where: {
id: analysis_id,
},
}
);
await database.audit.create({
authorTaxId: req.session.user.taxId,
data: req.body,
type: entry.type,
status: req.body.status,
timestamp: req.body.last_updated,
action: req.method,
auditId: analysis_id,
});
}
if (!analysis) {
res.status(404).send("Error while updating analysis.");
} else {
res.send({ redirect: "../user_views/history" });
}
} catch (e) {
console.log(e);
}
}
}
);
routes.put(
"/:entry_id/accounting",
authUser,
authRole,
authAgency,
upload,
async function (req, res, next) {
let analysis_id = req.params.entry_id;
try {
const signed_accounting_office_pdf = [];
try {
const file = req.files;
if (file.signed_glk_pdf_upload) {
const date = new Date().toLocaleString("el-GR", {
timeZone: "Europe/Athens",
});
signed_accounting_office_pdf.push({
filename: file.signed_glk_pdf_upload[0].filename,
upload_date: date,
});
}
} catch (e) {
console.log("Error message: " + e.message);
}
const entry = await database.analysis.findOne({
where: { id: analysis_id },
});
let analysis = await database.analysis.update(
{
accountingData: req.body,
accountingUploads: signed_accounting_office_pdf,
author: req.session.user.taxId,
status: req.body.status,
},
{
where: {
id: analysis_id,
},
}
);
const data = req.body;
await database.audit.create({
authorTaxId: req.session.user.taxId,
data: entry.data,
type: entry.type,
status: req.body.status,
timestamp: new Date().toLocaleString("el-GR", {
timeZone: "Europe/Athens",
}),
action: req.method,
auditId: analysis_id,
});
if (!analysis) {
res.status(404).send("Error in updating analysis.");
} else {
res.send({ redirect: "../user_views/history" });
}
} catch (e) {
console.log(e);
}
}
);
routes.put(
"/:entry_id/delete_file",
authUser,
authRole,
authAgency,
async (req, res, next) => {
let entry = await database.analysis.findOne({
where: {
id: req.params.entry_id,
},
});
entry = entry.dataValues;
let filePath = `public/uploads/${req.body.deleted_file}`;
try {
if (entry.field_21_upload.includes(req.body.deleted_file)) {
let index21 = entry.field_21_upload.indexOf(req.body.deleted_file); //find index of file to be deleted
entry.field_21_upload.splice(index21, 1); //delete position of index, count 1
await database.analysis.update(
{ field_21_upload: entry.field_21_upload },
{ where: { id: entry.id } }
);
} else if (entry.field_23_upload.includes(req.body.deleted_file)) {
let index23 = entry.field_23_upload.indexOf(req.body.deleted_file); //find index of file to be deleted
entry.field_23_upload.splice(index23, 1); //delete position of index, count 1
await database.analysis.update(
{ field_23_upload: entry.field_23_upload },
{ where: { id: entry.id } }
);
} else if (entry.field_36_upload.includes(req.body.deleted_file)) {
let index36 = entry.field_36_upload.indexOf(req.body.deleted_file); //find index of file to be deleted
entry.field_36_upload.splice(index36, 1); //delete position of index, count 1
await database.analysis.update(
{ field_36_upload: entry.field_36_upload },
{ where: { id: entry.id } }
);
}
fs.unlink(filePath, async function (err) {
if (err && err.code == "ENOENT") {
// file doens't exist
console.info("File doesn't exist, won't remove it.");
res.sendStatus(404);
} else if (err) {
// other errors, e.g. maybe we don't have enough permission
console.error("Error occurred while trying to remove file");
res.sendStatus(403);
} else {
console.info(`removed`);
res.sendStatus(200);
}
});
} catch (err) {
console.log(err);
}
}
);
//TODO: on delete remove uploaded and exported files
routes.delete(
"/:entry_id/delete",
authUser,
authRole,
authAgency,
async function (req, res, next) {
let entry = await database.analysis.findOne({
where: { id: req.params.entry_id },
});
entry ? entry.destroy().then(res.sendStatus(200)) : res.sendStatus(404);
}
);
routes.post(
"/:entry_id/versions",
authUser,
authRole,
authAgency,
async function (req, res, next) {
let entries = await database.audit.findAll({
where: { auditId: req.params.entry_id },
include: [{ model: database.user }],
});
entries
? res.status(200).json({ entries: entries })
: res.sendStatus(404).send("Versions not found.");
}
);
routes.post(
"/:entry_id/diff/",
authUser,
authRole,
authAgency,
async function (req, res, next) {
let target1 = req.body.firstTargetDate;
let target2 = req.body.secondTargetDate;
let entry1 = await database.audit.findOne({
where: { id: target1 },
});
let entry2 = await database.audit.findOne({
where: { id: target2 },
});
let data = {};
for (i in entry1.data) {
if (entry1.data[i] === undefined) entry1.data[i] = "";
if (entry2.data[i] === undefined) entry2.data[i] = "";
let diffchars;
if (Array.isArray(entry1.data[i]) || Array.isArray(entry2.data[i])) {
diffchars = diff.diffWords(entry2.data[i][0], entry2.data[i][1]);
} else {
diffchars = diff.diffWords(entry1.data[i], entry2.data[i]);
}
diffchars.forEach((part) => {
// green for additions, red for deletions
part.added
? data[i]
? data[i].push({ value: part.value, color: "green" })
: (data[i] = [{ value: part.value, color: "green" }])
: part.removed
? data[i]
? data[i].push({ value: part.value, color: "red" })
: (data[i] = [{ value: part.value, color: "red" }])
: data[i]
? data[i].push({ value: part.value, color: "" })
: (data[i] = [{ value: part.value, color: "" }]);
});
}
req.diffData = data;
//TODO: add second loop to handle accountingData
next();
},
diffPdf.exportPDF
);
module.exports = routes;