-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckinItems.js
More file actions
79 lines (71 loc) · 2.24 KB
/
checkinItems.js
File metadata and controls
79 lines (71 loc) · 2.24 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
const fs = require('fs');
const superagent = require('superagent');
const readline = require('readline');
const path = require('path');
const { getAuthToken } = require('./lib/login');
let spId = process.argv[2];
let inFile = process.argv[3];
let ep = 'circulation/check-in-by-barcode';
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
try {
const start = new Date().valueOf();
let today = new Date().toISOString();
if (!inFile) {
throw 'Usage: node checkinItems <service_point_id> <jsonl_file>';
} else if (!fs.existsSync(inFile)) {
throw new Error('Can\'t find input file');
}
let config = await getAuthToken(superagent);
const actionUrl = `${config.okapi}/${ep}`;
let success = 0;
let fail = 0;
const fileStream = fs.createReadStream(inFile);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
let x = 0;
for await (const line of rl) {
x++;
let rec = JSON.parse(line);
let lDate = new Date();
if (config.expiry && config.expiry <= lDate.valueOf()) {
config = await getAuthToken(superagent);
}
let crec = {
itemBarcode: rec.barcode,
servicePointId: spId,
checkInDate: today
}
console.log(`[${x}] POST ${rec.barcode} to ${actionUrl}`);
try {
let res = await superagent
.post(actionUrl)
.send(crec)
.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', 'application/json');
success++;
} catch (e) {
let errMsg = (e.response && e.response.text) ? e.response.text : e;
console.log(errMsg);
fail++
}
if (config.delay) {
await wait(config.delay);
}
}
const end = new Date().valueOf();
const ms = end - start;
const time = Math.floor(ms / 1000);
console.log(`\nTime: ${time} sec`);
console.log(`Successes: ${success}`);
console.log(`Failures: ${fail}\n`);
} catch (e) {
console.error(e);
}
})();