Lines 1-10
Link Here
|
1 |
const { buildSampleObject, buildSampleObjects } = require("./mockData.js"); |
1 |
const { buildSampleObject, buildSampleObjects } = require("./mockData.js"); |
2 |
const { query } = require("./db.js"); |
2 |
const { query } = require("./db.js"); |
3 |
|
3 |
|
4 |
const { APIClient } = require("./dist/api-client.cjs.js"); |
4 |
const { apiGet, apiPost } = require("./api-client.js"); |
5 |
|
5 |
|
6 |
const insertSampleBiblio = async (item_count, baseUrl, authHeader) => { |
6 |
const insertSampleBiblio = async (item_count, baseUrl, authHeader) => { |
7 |
let client = APIClient.default; |
|
|
8 |
let generated_objects = {}; |
7 |
let generated_objects = {}; |
9 |
const objects = [{ object: "library" }, { object: "item_type" }]; |
8 |
const objects = [{ object: "library" }, { object: "item_type" }]; |
10 |
for (const { object } of objects) { |
9 |
for (const { object } of objects) { |
Lines 52-65
const insertSampleBiblio = async (item_count, baseUrl, authHeader) => {
Link Here
|
52 |
}, |
51 |
}, |
53 |
], |
52 |
], |
54 |
}; |
53 |
}; |
55 |
let result = await client.koha.post({ |
54 |
let result = await apiPost({ |
56 |
endpoint: `${baseUrl}/api/v1/biblios`, |
55 |
endpoint: "/api/v1/biblios", |
57 |
headers: { |
56 |
headers: { |
58 |
"Content-Type": "application/marc-in-json", |
57 |
"Content-Type": "application/marc-in-json", |
59 |
"x-confirm-not-duplicate": 1, |
58 |
"x-confirm-not-duplicate": 1, |
60 |
Authorization: authHeader, |
|
|
61 |
}, |
59 |
}, |
62 |
body: biblio, |
60 |
body: biblio, |
|
|
61 |
baseUrl, |
62 |
authHeader, |
63 |
}); |
63 |
}); |
64 |
const biblio_id = result.id; |
64 |
const biblio_id = result.id; |
65 |
// We do not have a route to get a biblio as it is stored in DB |
65 |
// We do not have a route to get a biblio as it is stored in DB |
Lines 124-139
const insertSampleBiblio = async (item_count, baseUrl, authHeader) => {
Link Here
|
124 |
); |
124 |
); |
125 |
let createdItems = []; |
125 |
let createdItems = []; |
126 |
for (const item of items) { |
126 |
for (const item of items) { |
127 |
await client.koha |
127 |
await apiPost({ |
128 |
.post({ |
128 |
endpoint: `/api/v1/biblios/${biblio_id}/items`, |
129 |
endpoint: `${baseUrl}/api/v1/biblios/${biblio_id}/items`, |
129 |
body: item, |
130 |
body: item, |
130 |
baseUrl, |
131 |
headers: { |
131 |
authHeader, |
132 |
"Content-Type": "application/json", |
132 |
}).then(i => createdItems.push(i)); |
133 |
Authorization: authHeader, |
|
|
134 |
}, |
135 |
}) |
136 |
.then(i => createdItems.push(i)); |
137 |
} |
133 |
} |
138 |
return { biblio, items: createdItems, library, item_type }; |
134 |
return { biblio, items: createdItems, library, item_type }; |
139 |
}; |
135 |
}; |
Lines 195-213
const insertLibrary = async (library, baseUrl, authHeader) => {
Link Here
|
195 |
needs_override, |
191 |
needs_override, |
196 |
...new_library |
192 |
...new_library |
197 |
} = library; |
193 |
} = library; |
198 |
let client = APIClient.default; |
194 |
return apiPost({ |
199 |
return client.koha.post({ |
195 |
endpoint: "/api/v1/libraries", |
200 |
endpoint: `${baseUrl}/api/v1/libraries`, |
|
|
201 |
body: new_library, |
196 |
body: new_library, |
202 |
headers: { |
197 |
baseUrl, |
203 |
"Content-Type": "application/json", |
198 |
authHeader, |
204 |
Authorization: authHeader, |
|
|
205 |
}, |
206 |
}); |
199 |
}); |
207 |
}; |
200 |
}; |
208 |
|
201 |
|
209 |
const insertObject = async (type, object, baseUrl, authHeader) => { |
202 |
const insertObject = async (type, object, baseUrl, authHeader) => { |
210 |
let client = APIClient.default; |
|
|
211 |
if (type == "patron") { |
203 |
if (type == "patron") { |
212 |
await query({ |
204 |
await query({ |
213 |
sql: "SELECT COUNT(*) AS count FROM branches WHERE branchcode = ?", |
205 |
sql: "SELECT COUNT(*) AS count FROM branches WHERE branchcode = ?", |
Lines 247-272
const insertObject = async (type, object, baseUrl, authHeader) => {
Link Here
|
247 |
...patron |
239 |
...patron |
248 |
} = object; |
240 |
} = object; |
249 |
|
241 |
|
250 |
return client.koha.post({ |
242 |
return apiPost({ |
251 |
endpoint: `${baseUrl}/api/v1/patrons`, |
243 |
endpoint: `/api/v1/patrons`, |
252 |
body: patron, |
244 |
body: patron, |
253 |
headers: { |
245 |
baseUrl, |
254 |
"Content-Type": "application/json", |
246 |
authHeader, |
255 |
Authorization: authHeader, |
|
|
256 |
}, |
257 |
}); |
247 |
}); |
258 |
} else if (type == "library") { |
248 |
} else if (type == "library") { |
259 |
const keysToKeep = ["library_id", "name"]; |
249 |
const keysToKeep = ["library_id", "name"]; |
260 |
const library = Object.fromEntries( |
250 |
const library = Object.fromEntries( |
261 |
Object.entries(object).filter(([key]) => keysToKeep.includes(key)) |
251 |
Object.entries(object).filter(([key]) => keysToKeep.includes(key)) |
262 |
); |
252 |
); |
263 |
return client.koha.post({ |
253 |
return apiPost({ |
264 |
endpoint: `${baseUrl}/api/v1/libraries`, |
254 |
endpoint: "/api/v1/libraries", |
265 |
headers: { |
|
|
266 |
"Content-Type": "application/json", |
267 |
Authorization: authHeader, |
268 |
}, |
269 |
body: library, |
255 |
body: library, |
|
|
256 |
baseUrl, |
257 |
authHeader, |
270 |
}); |
258 |
}); |
271 |
} else if (type == "item_type") { |
259 |
} else if (type == "item_type") { |
272 |
const keysToKeep = ["item_type_id", "description"]; |
260 |
const keysToKeep = ["item_type_id", "description"]; |
Lines 279-290
const insertObject = async (type, object, baseUrl, authHeader) => {
Link Here
|
279 |
}) |
267 |
}) |
280 |
.then(result => { |
268 |
.then(result => { |
281 |
// FIXME We need /item_types/:item_type_id |
269 |
// FIXME We need /item_types/:item_type_id |
282 |
return client.koha.get({ |
270 |
return apiGet({ |
283 |
endpoint: `${baseUrl}/api/v1/item_types?q={"item_type_id":"${item_type.item_type_id}"}`, |
271 |
endpoint: `/api/v1/item_types?q={"item_type_id":"${item_type.item_type_id}"}`, |
284 |
headers: { |
272 |
baseUrl, |
285 |
"Content-Type": "application/json", |
273 |
authHeader, |
286 |
Authorization: authHeader, |
|
|
287 |
}, |
288 |
}); |
274 |
}); |
289 |
}) |
275 |
}) |
290 |
.then(item_types => item_types[0]); |
276 |
.then(item_types => item_types[0]); |
291 |
- |
|
|