-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathputCustomFields.js
More file actions
60 lines (58 loc) · 1.63 KB
/
putCustomFields.js
File metadata and controls
60 lines (58 loc) · 1.63 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
const fs = require('fs');
const superagent = require('superagent');
const { getAuthToken } = require('./lib/login');
const fn = process.argv[3];
const mod = process.argv[2];
const ent = 'user';
(async () => {
let added = 0;
let updated = 0;
let errors = 0;
try {
if (!fn) {
throw new Error('Usage: node putCustomFields.js <mod-users version> <file>\nNOTE: Module set to ' + mod);
}
let config = await getAuthToken(superagent);
let url = `${config.okapi}/custom-fields`;
let collStr = fs.readFileSync(fn, 'utf8');
if (!collStr.match(/customFields/)) {
throw new Error('Payload object must contain a "customFields" array property!');
}
let coll = JSON.parse(collStr);
coll.entityType = ent;
delete coll.totalRecords;
console.log(`PUT ${url}...`);
try {
let res = 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)
.set('content-type', 'application/json')
.set('accept', 'text/plain')
.set('x-okapi-module-id', mod)
.send(coll);
updated++;
} catch (e) {
let msg;
let err1 = e;
try {
msg = e.response.res.text;
} catch (e) {
msg = err1.message;
}
if (process.env.DEBUG) {
console.log(e);
} else {
console.log(`ERROR: ${msg}`);
}
errors++;
}
console.log(`Added: ${added}`);
console.log(`Updated: ${updated}`);
console.log(`Errors: ${errors}`);
} catch (e) {
console.log(e.message);
}
})();