-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.js
240 lines (229 loc) · 7.66 KB
/
controllers.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
const bcrypt = require('bcrypt');
const sanitize = require('./genSantizer');
const {User, Organization, Survey} = require('./models');
const {exportToXLSX, filterResponsesByTime, formatDataForExcel, PrepareForDisp} = require('./helpers');
const auth = async (req, res, next) => {
var authheader = req.headers.authorization;
if (!authheader) return res.status(401).json({message:"Authentication Needed"})
var auth = new Buffer.from(authheader.split(' ')[1], 'base64').toString().split(':');
var username = sanitize(auth[0]); var pass = auth[1];
try {
const user = await User.findOne({ email: username });
if (user != null) {
bcrypt.compare(pass, user.password, async function (err, result) {
if (result) {
// Admin Pass
if (user.roleId == '623cc24a8b7ab06011bd1e62' || user.roleId == '623cc24a8b7ab06011bd1e61') {
return next();
}
var org = await Organization.findOne({ _id: user.organizationId });
// Check if the roles are correct
if (org != null && (user.roleId == '623cc24a8b7ab06011bd1e60' || user.roleId == '6362ad70297414bfb79bdf01')) {
// Check if package is eligable
if (org.packageId != '623d73a051e8bcb894b3f7df'){
// Check if package has expired
var expiresAt = new Date(org.expires);
if (expiresAt.getTime() > new Date().getTime())
next();
else return res.status(401).json({message: "Package Expired"})
} else return res.status(401).json({message: "Package not Allowed"})
} else return res.status(401).json({message: "Wrong credentials"})
} else return res.status(401).json({message: "Wrong credentials"})
});
} else return res.status(401).json({message: "Wrong credentials"})
} catch (error) { console.log(error); return res.status(500).send("Bad Input") }
}
const getResponseXL = async (req, res, next) => {
const surveyId = sanitize(req.params.survey);
const shortOrgId = sanitize(req.params.shortOrg);
var surveyFound = false;
const survey_type_check = await Survey.findOne({shortSurveyId: surveyId});
try {
const accounts = await Organization.aggregate([
{
"$match": {
"orgId": shortOrgId
},
},
{
"$lookup": {
"from": "projects",
"localField": "projectsId",
"foreignField": "_id",
"as": "projects",
"pipeline": [
{
"$lookup": {
"from": "surveys",
"localField": "surveysId",
"foreignField": "_id",
"as": "surveys",
},
}
],
}
}
]);
var account = accounts[0];
for (let index = 0; index < account.projects.length; index++) {
const project = account.projects[index];
if (project.surveys.some(survey => survey.shortSurveyId == surveyId)) // The survey belongs to the org
surveyFound = true;
}
}
catch (err) {
console.log(err);
}
// If survey is found
if (surveyFound) {
const survey = await Survey.aggregate([
{
"$match": {
"shortSurveyId": surveyId
}
},
{
"$lookup": {
"from": survey_type_check.type == "REGULAR" ? "questions" : "online-questions",
"localField": "questions",
"foreignField": "_id",
"as": "joined_questions"
}
},
{
"$lookup": {
"from": "responses",
"localField": "responses",
"foreignField": "_id",
"as": "joined_responses"
}
}
]);
var questions = survey[0].joined_questions.sort(function(x, y){return x.createdOn - y.createdOn;});
var responses = survey[0].joined_responses;
exportToXLSX(formatDataForExcel(questions, responses, survey_type_check.type, survey_type_check.name), surveyId)
return res.status(200).sendFile('./temp/'+surveyId+'.xlsx', { root: __dirname });
}
return res.status(403).json({message: "Bad Input"})
}
const getResponse = async (req, res, next) => {
var at = req.query.at;
var from = req.query.from;
var to = req.query.to;
const surveyId = sanitize(req.params.survey);
const shortOrgId = sanitize(req.params.shortOrg);
var surveyFound = false;
const survey_type_check = await Survey.findOne({shortSurveyId: surveyId});
try {
const accounts = await Organization.aggregate([
{
"$match": {
"orgId": shortOrgId
},
},
{
"$lookup": {
"from": "projects",
"localField": "projectsId",
"foreignField": "_id",
"as": "projects",
"pipeline": [
{
"$lookup": {
"from": "surveys",
"localField": "surveysId",
"foreignField": "_id",
"as": "surveys",
},
}
],
}
}
]);
var account = accounts[0];
for (let index = 0; index < account.projects.length; index++) {
const project = account.projects[index];
if (project.surveys.some(survey => survey.shortSurveyId == surveyId)) // The survey belongs to the org
surveyFound = true;
}
}
catch (err) {
console.log(err);
}
// If survey is found
if (surveyFound) {
const survey = await Survey.aggregate([
{
"$match": {
"shortSurveyId": surveyId
}
},
{
"$lookup": {
"from": survey_type_check.type == "REGULAR" ? "questions" : "online-questions",
"localField": "questions",
"foreignField": "_id",
"as": "joined_questions"
}
},
{
"$lookup": {
"from": "responses",
"localField": "responses",
"foreignField": "_id",
"as": "joined_responses"
}
}
]);
var questions = survey[0].joined_questions.sort(function(x, y){return x.createdOn - y.createdOn;});
var responses = survey[0].joined_responses;
}
// /shelf/:shortOrg/:survey
if (at == undefined && from == undefined && to == undefined && surveyFound) {
try {
var ForDisp = PrepareForDisp(survey_type_check, questions, responses);
return res.json(ForDisp)
} catch (error) {
console.log(error);
return res.status(500).json({ message: "Bad Input" });
}
}
// /shelf/:shortOrg/:survey?at=MM-DD-YYYY
else if (at != undefined && from == undefined && to == undefined && surveyFound) {
try {
var ForDisp = PrepareForDisp(survey_type_check, questions, filterResponsesByTime(responses, "AT", at));
return res.json(ForDisp)
} catch (error) {
console.log(error);
return res.status(500).json({ message: "Bad Input" });
}
}
// /shelf/:shortOrg/:survey?from=MM-DD-YYYY
else if (at == undefined && from != undefined && to == undefined && surveyFound) {
try {
var ForDisp = PrepareForDisp(survey_type_check, questions, filterResponsesByTime(responses, "FROM", from));
return res.json(ForDisp)
} catch (error) {
console.log(error);
return res.status(500).json({ message: "Bad Input" });
}
}
// /shelf/:shortOrg/:survey?from=MM-DD-YYYY&to=MM-DD-YYYY
else if (from != undefined && to != undefined && at == undefined && surveyFound) {
try {
var ForDisp = PrepareForDisp(survey_type_check, questions, filterResponsesByTime(responses, "FROM_TO", from, to));
return res.json(ForDisp)
} catch (error) {
console.log(error);
return res.status(500).json({ message: "Bad Input" });
}
}
else {
return res.status(403).json({ message: "Bad Input" });
}
}
module.exports = {
auth,
getResponse,
getResponseXL
}