forked from zabartcc/vatusa-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
98 lines (77 loc) · 3.66 KB
/
app.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
import axios from 'axios';
import dotenv from 'dotenv';
import schedule from 'node-schedule';
import FormData from 'form-data';
import { performance } from 'perf_hooks';
dotenv.config();
const zabApi = axios.create({
baseURL: process.env.ZAB_API_URL,
headers: {
'Authorization': `Bearer ${process.env.ZAB_API_KEY}`
}
});
const syncRoster = async() => {
const start = performance.now();
console.log(`Syncing Roster...`);
const { data: vatusaData } = await axios.get(`https://api.vatusa.net/v2/facility/zme/roster/both?apikey=${process.env.VATUSA_API_KEY}`).catch(console.error);
const { data: zabData } = await zabApi.get('/controller');
const allZabControllers = [...zabData.data.home, ...zabData.data.visiting];
const { data: zabRoles } = await zabApi.get('/controller/role');
const availableRoles = zabRoles.data.map(role => role.code);
const zabControllers = allZabControllers.map(c => c.cid); // everyone in db
const zabMembers = allZabControllers.filter(c => c.member).map(c => c.cid); // only member: true
const zabNonMembers = allZabControllers.filter(c => !c.member).map(c => c.cid); // only member: false
const zabHomeControllers = zabData.data.home.map(c => c.cid); // only vis: false
const zabVisitingControllers = zabData.data.visiting.map(c => c.cid); // only: vis: true
const vatusaControllers = vatusaData.data.map(c => c.cid); // all controllers returned by VATUSA
const vatusaHomeControllers = vatusaData.data.filter(c => c.membership === 'home').map(c => c.cid); // only membership: home
const vatusaVisitingControllers = vatusaData.data.filter(c => c.membership !== 'home').map(c => c.cid); // only membership: !home
const toBeAdded = vatusaControllers.filter(cid => !zabControllers.includes(cid));
const makeNonMember = zabMembers.filter(cid => !vatusaControllers.includes(cid));
const makeMember = zabNonMembers.filter(cid => vatusaControllers.includes(cid));
const makeVisitor = zabHomeControllers.filter(cid => vatusaVisitingControllers.includes(cid));
const makeHome = zabVisitingControllers.filter(cid => vatusaHomeControllers.includes(cid));
console.log(`Members to be added: ${toBeAdded.join(', ')}`);
console.log(`Members to be removed: ${makeNonMember.join(', ')}`);
console.log(`Controllers to be made member: ${makeMember.join(', ')}`);
console.log(`Controllers to be made visitor: ${makeVisitor.join(', ')}`);
console.log(`Controllers to be made home controller: ${makeHome.join(', ')}`);
const vatusaObject = {};
for(const user of vatusaData.data) {
vatusaObject[user.cid] = user;
}
for (const cid of toBeAdded) {
const user = vatusaObject[cid];
const assignableRoles = user.roles.filter(role => availableRoles.includes(role.role.toLowerCase())).map(role => role.role.toLowerCase());
const userData = {
fname: user.fname,
lname: user.lname,
cid: user.cid,
rating: user.rating,
home: user.facility,
email: user.email,
broadcast: user.flag_broadcastOptedIn,
member: true,
vis: (user.membership === 'home') ? false : true,
roleCodes: (user.membership === 'home') ? assignableRoles : []
}
await zabApi.post(`/controller/${user.cid}`, userData);
}
for (const cid of makeMember) {
await zabApi.put(`/controller/${cid}/member`, {member: true});
}
for (const cid of makeNonMember) {
await zabApi.put(`/controller/${cid}/member`, {member: false});
}
for (const cid of makeVisitor) {
await zabApi.put(`/controller/${cid}/visit`, {vis: true});
}
for (const cid of makeHome) {
await zabApi.put(`/controller/${cid}/visit`, {vis: false});
}
console.log(`...Done!\nFinished in ${Math.round(performance.now() - start)/1000}s\n---`);
}
(() => {
syncRoster();
schedule.scheduleJob('*/10 * * * *', syncRoster);
})();