-
Notifications
You must be signed in to change notification settings - Fork 1
/
requests.js
57 lines (49 loc) · 1.61 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
require('dotenv').config();
const testFolder = './.sessions/';
const fs = require('fs');
const { linkUserToLabelByName } = require('./app/utils/labels');
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) {
res.status(400); res.send('Params user_id, label_name and security_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 = await linkUserToLabelByName(userID, labelName, pageToken, true);
res.status(200); res.send(response);
}
}
}
module.exports = {
getNameFBID, addLabel,
};