-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteServicePoint.js
More file actions
107 lines (92 loc) · 2.78 KB
/
deleteServicePoint.js
File metadata and controls
107 lines (92 loc) · 2.78 KB
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
/*
Delete service points and its service points users by service point id.
*/
const superagent = require('superagent');
const { getAuthToken } = require('./lib/login');
const sid = process.argv[2];
const debug = process.env.DEBUG;
const wait = (ms) => {
console.log(`(Waiting ${ms} ms...)`);
return new Promise((resolve) => setTimeout(resolve, ms));
};
const delSpId = async (config, sid) => {
let url = `${config.okapi}/service-points-users?query=servicePointsIds=${sid}%20OR%20defaultServicePointId=${sid}&limit=5000`;
console.log(`GET ${url}`);
try {
res = await superagent
.get(url)
.set('User-Agent', config.agent)
.set('cookie', config.cookie)
.set('x-okapi-tenant', config.tenant)
.set('x-okapi-token', config.token)
console.log(`INFO service-point-users found: ${res.body.totalRecords}`);
if (res.body.totalRecords > 0) {
console.log(`INFO ${res.body.totalRecords} users found with service-point id ${sid}`)
for (let x = 0; x < res.body.servicePointsUsers.length; x++) {
let r = res.body.servicePointsUsers[x];
for (let y = 0; y < r.servicePointsIds.length; y++) {
let spid = r.servicePointsIds[y];
if (spid === sid) {
r.servicePointsIds.splice(y, 1);
y++;
}
}
if (r.defaultServicePointId === sid) delete r.defaultServicePointId;
await putSpu(config, r);
}
} else {
}
} catch (e) {
console.log(`${e}`);
}
}
const putSpu = async (config, spuRec) => {
let id = spuRec.id;
let url = `${config.okapi}/service-points-users/${id}`
console.log(`INFO PUT to ${url}`);
try {
await superagent
.put(url)
.set('User-Agent', config.agent)
.set('cookie', config.cookie)
.set('x-okapi-tenant', config.tenant)
.set('x-okapi-token', config.token)
.send(spuRec);
} catch (e) {
console.log(e);
}
}
const delId = async (config, id, ep) => {
let url = `${config.okapi}/${ep}/${id}`;
console.log(`DELETE ${url}`);
let out;
try {
res = await superagent
.delete(url)
.set('User-Agent', config.agent)
.set('cookie', config.cookie)
.set('x-okapi-tenant', config.tenant)
.set('x-okapi-token', config.token)
out = res.body
} catch (e) {
console.log(`${e}`);
}
}
(async () => {
try {
if (!sid) {
throw ('Usage: node deleteServicePoints.js <service-points_id>');
}
let start = new Date().valueOf();
let config = await getAuthToken(superagent);
await delSpId(config, sid);
await delId(config, sid, 'service-points');
let end = new Date().valueOf();
let tt = (end - start)/1000;
console.log('Done!');
console.log('Time:', tt);
} catch (e) {
let msg = (debug) ? e : `${e}`;
console.log(msg);
}
})();