-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequests.js
executable file
·345 lines (312 loc) · 11.3 KB
/
requests.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
require('dotenv').config();
const testFolder = './.sessions/';
const fs = require('fs');
const { linkUserToLabelByName } = require('./app/utils/labels');
const { changeAdminStatus } = require('./app/utils/DB_helper');
const { getTurmaInCompany } = require('./app/utils/DB_helper');
const addQueue = require('./app/utils/notificationAddQueue');
const send = require('./app/utils/notificationSendQueue');
const { seeDataQueue } = require('./app/utils/notificationAddQueue');
const { sendDonnaMail } = require('./app/utils/surveys/questionario_followUp');
const { sendMatricula } = require('./app/utils/surveys/questionario_followUp');
const { saveAnswer } = require('./app/utils/surveys/questionario_callback');
const { syncRespostas } = require('./app/utils/surveys/questionario_sync');
const rules = require('./app/utils/notificationRules');
const { dateNoTimezone } = require('./app/utils/helper');
async function getFBIDJson() { // eslint-disable-line
const result = {};
await fs.readdirSync(testFolder).forEach(async (file) => {
const obj = JSON.parse(await fs.readFileSync(testFolder + file, 'utf8'));
result[obj.user.name] = obj.user.id;
});
return result;
}
async function getNameFBID(req, res) {
const body = JSON.parse(req.body || '{}');
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else {
const result = await getFBIDJson();
if (result) {
res.status(200); res.send(result);
} else {
res.status(500); res.send('Failure');
}
}
}
}
async function addLabel(req, res) {
if (!req.body || !req.body.user_id || !req.body.label_name || !req.body.security_token || !req.body.fb_access_token) {
res.status(400); res.send('Params user_id, label_name, security_token and fb_access_token are required!');
} else {
const userID = req.body.user_id;
const labelName = req.body.label_name;
const securityToken = req.body.security_token;
const pageToken = req.body.fb_access_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else {
const response = {};
response.facebook_label = await linkUserToLabelByName(userID, labelName, pageToken, true);
if (labelName === 'admin') {
response.database_label = await changeAdminStatus(userID, true);
}
res.status(200); res.send(response);
}
}
}
async function addMissingNotification(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.notification_type || !body.turma_id) {
res.status(401); res.send('Missing aluno_id or turma_id!');
} else {
addQueue.addMissingAlunoNotification(body.turma_id, body.notification_type);
res.status(200); res.send('Processando');
}
}
}
async function addFamiliarQueue(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.aluno_id || !body.turma_id) {
res.status(401); res.send('Missing aluno_id or turma_id!');
} else {
const notificationRules = await rules.loadTabNotificationRules(await getTurmaInCompany(body.turma_id));
console.log('notificationRules', notificationRules);
const response = await addQueue.addQueueForFamiliar(body.aluno_id, notificationRules);
res.status(200);
res.send(response);
}
}
}
async function addNewQueue(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.aluno_id || !body.turma_id) {
res.status(401); res.send('Missing aluno_id or turma_id!');
} else {
addQueue.addNewNotificationAlunas(body.aluno_id, body.turma_id);
res.status(200); res.send('Processando');
}
}
}
async function sendNotificationQueue(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else {
const turmaID = body.turma_id;
const alunoID = body.aluno_id;
const indicadoID = body.indicado_id;
const notificationType = body.notification_type;
const { moment } = body;
let dataComparacao = null;
if (moment) {
dataComparacao = new Date(moment);
if (!Object.prototype.toString.call(dataComparacao) === '[object Date]' || isNaN(dataComparacao.getTime())) { // eslint-disable-line
res.status(401); res.send('Data inválida, utilize uma string ISO como essa: 2020-02-15T17:30:00.000Z');
}
}
if (dataComparacao && !Object.prototype.toString.call(dataComparacao) === '[object Date]') dataComparacao = null;
const queue = await send.getQueue(turmaID, alunoID, indicadoID, notificationType);
const result = await send.sendNotificationFromQueue(queue, dataComparacao, false);
res.status(200); res.send(result);
}
}
}
async function dataQueue(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.turma_id) {
res.status(401); res.send('body.turma_id missing!');
} else {
const result = await seeDataQueue(body.turma_id);
res.status(200); res.send(result);
}
}
}
async function seeQueue(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.turma_id) {
res.status(401); res.send('body.turma_id missing!');
} else {
const result = await addQueue.seeNotifications(body.turma_id);
res.status(200); res.send(result);
}
}
}
async function donnaMail(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.nome || !body.email) {
res.status(401); res.send('body.nome or body.email missing');
} else {
const result = await sendDonnaMail(body.nome, body.email);
res.status(200); res.send(result);
}
}
}
async function matriculaMail(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else {
const turmaName = body.turma_name;
const pagamentoID = body.pagamento_id;
const buyerEmail = body.email;
const { cpf } = body;
const inCompany = body.in_company;
if (!turmaName || !buyerEmail) {
res.status(200); res.send('turma_name or email missing');
} else {
const result = await sendMatricula(turmaName, pagamentoID, buyerEmail, cpf, inCompany);
res.status(200); res.send(result);
}
}
}
}
async function saveNewAnswer(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (!body.questionarioID || !body.answerID || (!body.alunoID && !body.indicadoID)) {
res.status(401); res.send('questionarioID or answerIDor or alunoID or indicadoID missing');
} else if (body.alunoID && body.indicadoID) {
res.status(401); res.send('Dont send both alunoID and indicadoID together');
} else {
const result = await saveAnswer(body.questionarioID, body.answerID, body.alunoID, body.indicadoID);
res.status(200); res.send(result);
}
}
}
async function syncAnswers(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else if (body.syncID && typeof body.syncID !== 'number') {
res.status(401); res.send('syncID invalid');
} else {
const result = await syncRespostas(body.syncID);
res.status(200); res.send(result);
}
}
}
async function logMail(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else {
const turmaID = body.turma_id;
const alunoID = body.aluno_id;
const notificationType = body.notification_type;
const { moment } = body;
let dataComparacao = null;
if (moment) {
dataComparacao = new Date(moment);
if (!Object.prototype.toString.call(dataComparacao) === '[object Date]' || isNaN(dataComparacao.getTime())) { // eslint-disable-line
res.status(401); res.send('Data inválida, utilize uma string ISO como essa: 2020-02-15T17:30:00.000Z');
}
} else {
dataComparacao = await dateNoTimezone();
}
if (dataComparacao && !Object.prototype.toString.call(dataComparacao) === '[object Date]') dataComparacao = null;
const queue = await send.getQueue(turmaID, alunoID, notificationType);
const result = await send.sendNotificationFromQueue(queue, dataComparacao, true);
res.status(200); res.send(result);
}
}
}
async function addQueueDetails(req, res) {
const { body } = req;
if (!body || !body.security_token) {
res.status(400); res.send('Param security_token is required!');
} else {
const securityToken = body.security_token;
if (securityToken !== process.env.SECURITY_TOKEN_MA) {
res.status(401); res.send('Unauthorized!');
} else {
const turmaID = body.turma_id;
const type = body.notification_type;
const details = body.additional_details || null;
if (!turmaID || !type) {
res.status(401); res.send('Falta turma_id ou notification_type!');
} else {
const result = await addQueue.addQueueProvisorio(turmaID, type, details);
console.log('result', result);
res.status(200); res.send(result);
}
}
}
}
module.exports = {
getNameFBID,
addLabel,
addMissingNotification,
sendNotificationQueue,
dataQueue,
seeQueue,
addNewQueue,
donnaMail,
saveNewAnswer,
syncAnswers,
logMail,
addQueueDetails,
matriculaMail,
addFamiliarQueue,
};