-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks-create
executable file
·107 lines (79 loc) · 2.91 KB
/
blocks-create
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
auth 2>/dev/null || authn 2>/dev/null
#0cf-e4aa278110b7 expects fine amounts in column 1, patron uuid in column 2
tenant=$(cat tenant)
okapi_url=$(cat okapi.url)
okapi_token=$(cat okapi.token)
blocks_file="blocks.tsv"
legacySystem="Sierra"
logfile="block.log"
num_retrieved=1000
rm -f ${logfile}
sed -i 's/"//g' "${blocks_file}"
function fetch_batch() {
local offset=$1
curl -s -w '\n' -X GET -H "Accept: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" "${okapi_url}/users?query=cql.allRecords=1&limit=${num_retrieved}&offset=$offset" |jq -c ".users[]" > "tmp_users.${t}"
}
declare -A user_dict
create_block() {
patron_barcode=${patron_barcode//[^0-9A-Za-z\-]/}
blockId=$(uuid)
IFS='' read -r -d '' block << EndOfJSON
{
"staffInformation": "${notes}",
"patronMessage": "",
"expirationDate": "",
"borrowing": true,
"renewals": true,
"requests": true,
"desc": "Migrated block from ${legacySystem}",
"type": "Manual",
"userId": "${userId}",
"id": "${blockId}"
}
EndOfJSON
}
echo "Retrieving user records"
all_users=""
num_records=$(curl -s -w '\n' -X GET -H "Accept: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" "${okapi_url}/users?limit=0" |jq -r .totalRecords)
for (( offset=0; offset<=$num_records; ));do
for ((t=0; t<=4; t=$(($t + 1)) )); do
if [[ $offset -le $num_records ]];then
fetch_batch $offset &
offset=$(($offset + $num_retrieved))
fi
done
wait
cat tmp_users.* >> tmp_users
rm tmp_users.*
if [[ $offset -ne 0 ]];then printf "%s records retrieved of %s\r" $offset $num_records;fi
done
# load user uuids into scalar array keyed by barcode
echo "Loading ids. Please be patient "
cat tmp_users | jq -r .barcode > tmp_barcodes
cat tmp_users | jq -r .id > tmp_ids
counter=0
while IFS=$'\n' read -r barcode && IFS=$'\n' read -r id <&3; do
counter=$(($counter+ 1))
if [[ $((counter % 1000)) == 0 ]];then echo -en "Records loaded: $counter \r";fi
user_dict["${barcode}"]="${id}"
done < tmp_barcodes 3< tmp_ids
echo "$counter records loaded "
counter=1
badbarcodes=0
numblocks=$(cat ${blocks_file} |wc -l)
while IFS=$'\t' read -r patron_barcode notes; do
echo -en "Processing record $counter of ${numblocks}. Bad barcodes found: ${badbarcodes}\r"
if [[ ! -z ${user_dict[${patron_barcode}]} ]];then
userId=${user_dict[${patron_barcode}]}
create_block
apicall=$(curl --http1.1 -s -w '\n' -X POST -H "Content-type: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" -d "${block}" "${okapi_url}/manualblocks")
echo -e "${apicall//$'\n'/ }" >> ${logfile}
else
badbarcodes=$(($badbarcodes + 1))
echo -e "Barcode not found\t${patron_barcode}" >> ${logfile}
fi
counter=$(($counter + 1))
done < ${blocks_file}
echo
echo "Processing completed"
echo "$badbarcodes barcodes were not found. Server responses are output to ${logfile}"