-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.js
102 lines (100 loc) · 3.29 KB
/
history.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
const routes = require("express").Router();
let database = require("../services/database");
const { Op } = require("sequelize");
const { authUser, authRole, authAgency } = require("../middleware/auth");
const Enums = require("../lib/enums/analysis");
routes.get(
"/",
authUser,
authRole,
authAgency,
async function (req, res, next) {
try {
let user = await database.user.findOne({
where: {
taxId: req.session.user.taxId,
},
include: [{ model: database.analysis }],
});
let entries, status;
if (user && user.dataValues) {
if (user.role === Enums.Roles.Composer) {
status = Enums.Status.Composing;
entries = await database.analysis.findAll({
where: {
status: status,
"$user.agency$": user.agency, //has to be from same agency
},
include: [{ model: database.user }],
});
} else if (user.role === Enums.Roles.GeneralAccountingOffice) {
status = Enums.Status.Pending;
entries = await database.analysis.findAll({
where: {
status: {
[Op.or]: [Enums.Status.Pending, Enums.Status.Checked],
},
},
include: [{ model: database.user }],
});
} else if (user.role === Enums.Roles.Parliament) {
status = Enums.Status.Uploaded;
entries = await database.analysis.findAll({
where: {
status: status,
},
include: [{ model: database.user }],
});
} else {
entries = await database.analysis.findAll({ include: database.user });
}
const userEntries = [];
for (i in entries) {
let entry = await database.audit.findAll({
limit: 1,
where: {
authorTaxId: user.taxId,
auditId: entries[i].dataValues.id,
},
include: [{ model: database.user }],
order: [["createdAt", "DESC"]],
});
userEntries.push(entry[0]);
}
const latestEntries = [];
if (userEntries.length > 0) {
for (i in userEntries) {
if (userEntries[i] && "auditId" in userEntries[i]) {
let firstAuditEntry = await database.audit.findAll({
limit: 1,
where: { auditId: userEntries[i].auditId },
include: [{ model: database.user }],
});
let latestAuditEntry = await database.audit.findAll({
limit: 1,
where: { auditId: userEntries[i].auditId },
order: [["createdAt", "DESC"]],
include: [{ model: database.user }],
});
latestEntries.push({
firstAuditEntry: firstAuditEntry[0].dataValues,
userEntry: userEntries[i].dataValues,
latestAuditEntry: latestAuditEntry[0].dataValues,
});
}
}
}
res.render("user_views/history", {
entries: entries,
user: user,
latestEntries: latestEntries,
});
} else {
res.status(404).send("Not found");
}
} catch (err) {
console.log(err);
}
}
);
module.exports = routes;