-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
292 lines (266 loc) · 7.18 KB
/
server.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
//node packages
const axios = require("axios");
const fs = require("fs").promises;
const cron = require("node-cron");
require("dotenv").config();
//globals
const FREEPBX_TOKEN_URL = process.env.FREEPBX_TOKEN_URL;
const FREEPBX_API_URL = process.env.FREEPBX_API_URL;
const FREEPBX_AUTH_URL = process.env.FREEPBX_AUTH_URL;
const FREEPBX_GQL_URL = process.env.FREEPBX_GQL_URL;
const FREEPBX_CLIENT_ID = process.env.FREEPBX_CLIENT_ID;
const FREEPBX_CLIENT_SECRET = process.env.FREEPBX_CLIENT_SECRET;
const FREEPBX_SCOPE = process.env.FREEPBX_SCOPE;
const DS_RINGGROUP = process.env.DS_RINGGROUP;
const BACKUP_DS_RINGGROUP = process.env.BACKUP_DS_RINGGROUP;
const DS_CALLER_ID = process.env.DS_CALLER_ID;
const NIGHT_CRON_STRING = process.env.NIGHT_CRON_STRING;
const DAY_CRON_STRING = process.env.DAY_CRON_STRING;
const DS_URL = process.env.DS_URL;
const DS_URL_TOKEN = process.env.DS_URL_TOKEN;
const SUPERVISOR_INFO_FILE = process.env.SUPERVISOR_INFO_FILE;
const TZ = process.env.TZ;
//oauth stuff
const config = {
client: {
id: FREEPBX_CLIENT_ID,
secret: FREEPBX_CLIENT_SECRET,
},
auth: {
tokenHost: FREEPBX_API_URL,
tokenPath: "token",
},
};
const { ClientCredentials } = require("simple-oauth2");
const client = new ClientCredentials(config);
const tokenParams = {
scope: FREEPBX_SCOPE.split(" "),
};
//helper functions
const getNightSupId = async () => {
const { data: nightSup } = await axios.get(`${DS_URL}?token=${DS_URL_TOKEN}`);
return nightSup;
};
const getSups = async () => {
const sups = await fs.readFile(`./${SUPERVISOR_INFO_FILE}`, "utf-8");
return JSON.parse(sups);
};
const generateDsNumberList = async (nightSup) => {
const { supervisors: sups } = await getSups();
let dsNumberList = "";
for (const sup of sups) {
if (sup.id == nightSup) {
for (num of sup.external_phone_numbers) {
dsNumberList += `${num}#-`;
}
for (num of sup.internal_extensions) {
dsNumberList += `${num}-`;
}
}
}
dsNumberList = dsNumberList.slice(0, -1);
return dsNumberList;
};
const generateBackupDsNumberList = async () => {
const { supervisors: sups } = await getSups();
let backupDsNumberList = "";
for (const sup of sups) {
if (sup.backup_night) {
for (num of sup.external_phone_numbers) {
backupDsNumberList += `${num}#-`;
}
for (num of sup.internal_extensions) {
backupDsNumberList += `${num}-`;
}
}
}
backupDsNumberList = backupDsNumberList.slice(0, -1);
return backupDsNumberList;
};
const generateDayDsNumberList = async () => {
const { supervisors: sups } = await getSups();
let dayDsNumberList = "";
for (const sup of sups) {
if (sup.day) {
for (num of sup.external_phone_numbers) {
dayDsNumberList += `${num}#-`;
}
for (num of sup.internal_extensions) {
dayDsNumberList += `${num}-`;
}
}
}
dayDsNumberList = dayDsNumberList.slice(0, -1);
return dayDsNumberList;
};
const handleDay = async () => {
const dsNumberList = await generateDayDsNumberList();
let accessToken;
try {
accessToken = await client.getToken(tokenParams, { json: true });
// console.log(accessToken.token.access_token);
} catch (error) {
console.log("Access token error: ", error.message);
}
const res = await axios.post(
FREEPBX_GQL_URL,
{
query: `mutation{
updateRingGroup(input:{
groupNumber:${DS_RINGGROUP}
description:"Main DS"
extensionList:"${dsNumberList}"
strategy:"ringall"
ringTime: "60"
changecid: "fixed"
fixedcid: "${DS_CALLER_ID}"
}) {
message status
}
}`,
},
{ headers: { Authorization: `Bearer ${accessToken.token.access_token}` } }
);
const backupRes = await axios.post(
FREEPBX_GQL_URL,
{
query: `mutation{
updateRingGroup(input:{
groupNumber:${BACKUP_DS_RINGGROUP}
description:"Backup DS"
extensionList:"${dsNumberList}"
strategy:"ringall"
ringTime: "60"
callRecording: "force"
changecid: "fixed"
fixedcid: "${DS_CALLER_ID}"
}) {
message status
}
}`,
},
{ headers: { Authorization: `Bearer ${accessToken.token.access_token}` } }
);
const reloadRes = await axios.post(
FREEPBX_GQL_URL,
{
query: `mutation{
doreload(input:{}) {
message
status
transaction_id
}
}`,
},
{ headers: { Authorization: `Bearer ${accessToken.token.access_token}` } }
);
return {
main: res.status,
backup: backupRes.status,
reload: reloadRes.status,
};
};
const handleNight = async (nightSup) => {
let dsNumberList;
let backupDsNumberList;
if (nightSup <= 0) {
dsNumberList = await generateBackupDsNumberList();
} else {
dsNumberList = await generateDsNumberList(nightSup);
backupDsNumberList = await generateBackupDsNumberList();
}
let accessToken;
try {
accessToken = await client.getToken(tokenParams, { json: true });
// console.log(accessToken.token.access_token);
} catch (error) {
console.log("Access token error: ", error.message);
}
const res = await axios.post(
FREEPBX_GQL_URL,
{
query: `mutation{
updateRingGroup(input:{
groupNumber:${DS_RINGGROUP}
description:"Main DS"
extensionList:"${dsNumberList}"
strategy:"ringall"
ringTime: "15"
callRecording: "force"
changecid: "fixed"
fixedcid: "${DS_CALLER_ID}"
}) {
message status
}
}`,
},
{ headers: { Authorization: `Bearer ${accessToken.token.access_token}` } }
);
const backupRes = await axios.post(
FREEPBX_GQL_URL,
{
query: `mutation{
updateRingGroup(input:{
groupNumber:${BACKUP_DS_RINGGROUP}
description:"Backup DS"
extensionList:"${backupDsNumberList}"
strategy:"ringall"
ringTime: "60"
callRecording: "force"
changecid: "fixed"
fixedcid: "${DS_CALLER_ID}"
}) {
message status
}
}`,
},
{ headers: { Authorization: `Bearer ${accessToken.token.access_token}` } }
);
const reloadRes = await axios.post(
FREEPBX_GQL_URL,
{
query: `mutation{
doreload(input:{}) {
message
status
transaction_id
}
}`,
},
{ headers: { Authorization: `Bearer ${accessToken.token.access_token}` } }
);
return {
main: res.status,
backup: backupRes.status,
reload: reloadRes.status,
};
};
//cron scheduling
cron.schedule(
NIGHT_CRON_STRING,
async () => {
handleNight(await getNightSupId());
},
{ timezone: TZ }
);
cron.schedule(
DAY_CRON_STRING,
() => {
handleDay();
},
{ timezone: TZ }
);
(async () => {
const dayArr = DAY_CRON_STRING.split(" ");
const nightArr = NIGHT_CRON_STRING.split(" ");
const h = new Date().getHours();
//check on application start (i.e. the
//cronjob hasn't run yet)
if (h >= dayArr[1] && h < nightArr[1]) {
//it's daytime
handleDay();
} else {
//it's nighttime
handleNight(await getNightSupId());
}
})();