-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdrupal_access.js
150 lines (136 loc) · 4.51 KB
/
drupal_access.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
require('dotenv').config()
const request = require('superagent');
const logger = require('../libs/logger')(module);
const drupalConfig = require('config').get('profiles_api');
const NodeCache = require("node-cache");
const drupalCache = new NodeCache({ stdTTL: drupalConfig.cacheTTL, checkperiod: 600 });
function parseFromAnchorTag(uid_string) {
let re = /<a.*>(.+)<\/a>/g;
let m = re.exec(uid_string);
re.lastIndex = 0;
if (m.length === 2) {
return m[1];
} else {
return undefined;
}
}
function parseTicketsCount(tickets_str) {
let re = /tickets string:(.*)/g;
let m = re.exec(tickets_str);
re.lastIndex = 0;
if (m.length === 2) {
let ticketsArr = JSON.parse(m[1]);
return ticketsArr.length;
}
else return 0;
}
function parseUser(res_body) {
if (res_body.length === 0 || !res_body[0]) {
return undefined
}
return {
last_name: res_body[0]['Last name'],
first_name: res_body[0]['First name'],
uid: res_body[0]['uid'],
email: parseFromAnchorTag(res_body[0]['E-mail']),
phone: res_body[0]['Phone number'],
has_ticket: res_body[0]['PHP'] !== "tickets string:[]",
num_of_tickets: parseTicketsCount(res_body[0]['PHP'])
};
}
var login = (function() {
let request_agent = false;
logger.info('DRUPAL_PROFILE_API_URL: ' + drupalConfig.url +
", DRUPAL_PROFILE_API_USER: " + drupalConfig.username)
return function(forceLogin) {
return new Promise((resolve, reject) => {
if (request_agent && !forceLogin) {
resolve({ request_agent })
} else {
request_agent = request.agent();
request_agent
.post(drupalConfig.url + '/api/user/login')
.accept('application/json')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ 'username': drupalConfig.username, 'password': drupalConfig.password })
.then((res) => {
resolve({ request_agent });
}).catch((err) => {
request_agent = false;
reject(err)
});
}
});
};
})();
var search_multiple_uids = function(login_data, uid_arr) {
var tasks = uid_arr.map((uid) => {
return search(login_data, null, uid);
});
return Promise.all(tasks);
}
var search_multiple_emails = function(login_data, email_arr) {
var tasks = email_arr.map((mail) => {
return search(login_data, mail);
});
return Promise.all(tasks);
}
var search = function(login_data, email, uid, is_retry) {
return new Promise((resolve, reject) => {
var qs = {};
var key;
if (email) {
qs.mail = email;
key = email;
}
if (uid) {
qs.uid = uid;
key = uid;
}
var user_data_ = drupalCache.get(key)
if (user_data_ && drupalConfig.useCache) {
return resolve({ email: qs.mail, uid: qs.uid, user_data: user_data_ });
}
login_data.request_agent
.get(drupalConfig.url + '/en/api/usersearch')
.query(qs)
.then((res) => {
user_data_ = parseUser(res.body)
drupalCache.set(key, user_data_);
resolve({ email: qs.mail, uid: qs.uid, user_data: user_data_ });
}).catch((err) => {
if (err.status === 403 && !is_retry) {
logger.error(err);
return login(true).then(login_data =>
search(login_data, email, uid, true)
).catch(err => {
reject(err);
});
} else {
reject(err);
}
});
});
};
var DrupalAccess = {
//This is a temporary stub as long as we dont have connection to Drupal.
get_user_info: function(uid_arr, is_retry) {
return login().then(login_data =>
search_multiple_uids(login_data, uid_arr)
).catch(err => {
logger.error(err)
//login(true);
});
},
get_user_by_email: function(email_arr, is_retry) {
return login().then(login_data =>
search_multiple_emails(login_data, email_arr)
).catch(err => {
logger.error(err)
//login(true);
});
}
}
module.exports = {
DrupalAccess: DrupalAccess
};