Lines 175-188
const insertSampleHold = async ({
Link Here
|
175 |
); |
175 |
); |
176 |
} |
176 |
} |
177 |
|
177 |
|
178 |
const generatedPatron = await buildSampleObject({ |
178 |
const { patron, patron_category } = await insertSamplePatron({ |
179 |
object: "patron", |
179 |
library: { library_id }, |
180 |
values: { library_id, incorrect_address: null, patron_card_lost: null }, |
|
|
181 |
}); |
182 |
|
183 |
const patron = await insertObject({ |
184 |
type: "patron", |
185 |
object: generatedPatron, |
186 |
baseUrl, |
180 |
baseUrl, |
187 |
authHeader, |
181 |
authHeader, |
188 |
}); |
182 |
}); |
Lines 202-208
const insertSampleHold = async ({
Link Here
|
202 |
baseUrl, |
196 |
baseUrl, |
203 |
authHeader, |
197 |
authHeader, |
204 |
}); |
198 |
}); |
205 |
return { hold, patron }; |
199 |
return { hold, patron, patron_category }; |
206 |
}; |
200 |
}; |
207 |
|
201 |
|
208 |
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => { |
202 |
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => { |
Lines 213-234
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => {
Link Here
|
213 |
}); |
207 |
}); |
214 |
|
208 |
|
215 |
let generatedPatron; |
209 |
let generatedPatron; |
|
|
210 |
let patronCategory; |
216 |
if (!patron) { |
211 |
if (!patron) { |
217 |
generatedPatron = await buildSampleObject({ |
212 |
generatedPatron = true; |
218 |
object: "patron", |
213 |
const patron_objects = await insertSamplePatron({ |
219 |
values: { |
214 |
library: { library_id: libraries[0].library_id }, |
220 |
library_id: libraries[0].library_id, |
|
|
221 |
incorrect_address: null, |
222 |
patron_card_lost: null, |
223 |
}, |
224 |
}); |
225 |
|
226 |
patron = await insertObject({ |
227 |
type: "patron", |
228 |
object: generatedPatron, |
229 |
baseUrl, |
215 |
baseUrl, |
230 |
authHeader, |
216 |
authHeader, |
231 |
}); |
217 |
}); |
|
|
218 |
generatedCategory = patron_objects.category; |
219 |
patron = patron_objects.patron; |
232 |
} |
220 |
} |
233 |
|
221 |
|
234 |
const generatedCheckout = buildSampleObject({ |
222 |
const generatedCheckout = buildSampleObject({ |
Lines 250-258
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => {
Link Here
|
250 |
items, |
238 |
items, |
251 |
libraries, |
239 |
libraries, |
252 |
item_type, |
240 |
item_type, |
253 |
patron, |
|
|
254 |
checkout, |
241 |
checkout, |
255 |
...(generatedPatron ? { patron: generatedPatron } : {}), |
242 |
...(generatedPatron |
|
|
243 |
? { |
244 |
patron, |
245 |
patron_category: generatedCategory, |
246 |
} |
247 |
: {}), |
248 |
}; |
249 |
}; |
250 |
|
251 |
const insertSamplePatron = async ({ |
252 |
library, |
253 |
patron_category, |
254 |
baseUrl, |
255 |
authHeader, |
256 |
}) => { |
257 |
let generatedLibrary; |
258 |
let generatedCategory; |
259 |
if (!library) { |
260 |
generatedLibrary = await buildSampleObject({ object: "library" }); |
261 |
library = await insertLibrary({ |
262 |
library: generatedLibrary, |
263 |
baseUrl, |
264 |
authHeader, |
265 |
}); |
266 |
} |
267 |
if (!patron_category) { |
268 |
generatedCategory = await buildSampleObject({ |
269 |
object: "patron_category", |
270 |
}); |
271 |
query({ |
272 |
sql: "INSERT INTO categories(categorycode, description) VALUES (?, ?)", |
273 |
values: [ |
274 |
generatedCategory.patron_category_id, |
275 |
`description for ${generatedCategory.patron_category_id}`, |
276 |
], |
277 |
}); |
278 |
// FIXME We need /patron_categories/:patron_category_id |
279 |
await apiGet({ |
280 |
endpoint: `/api/v1/patron_categories?q={"me.patron_category_id":"${generatedCategory.patron_category_id}"}`, |
281 |
baseUrl, |
282 |
authHeader, |
283 |
}).then(categories => (patron_category = categories[0])); |
284 |
} |
285 |
|
286 |
let generatedPatron = await buildSampleObject({ |
287 |
object: "patron", |
288 |
values: { |
289 |
library_id: library.library_id, |
290 |
category_id: patron_category.patron_category_id, |
291 |
incorrect_address: null, |
292 |
patron_card_lost: null, |
293 |
}, |
294 |
}); |
295 |
|
296 |
let { |
297 |
patron_id, |
298 |
_strings, |
299 |
anonymized, |
300 |
restricted, |
301 |
expired, |
302 |
extended_attributes, |
303 |
checkouts_count, |
304 |
overdues_count, |
305 |
account_balance, |
306 |
lang, |
307 |
login_attempts, |
308 |
sms_provider_id, |
309 |
...patron |
310 |
} = generatedPatron; |
311 |
delete patron.library; |
312 |
|
313 |
patron = await apiPost({ |
314 |
endpoint: `/api/v1/patrons`, |
315 |
body: patron, |
316 |
baseUrl, |
317 |
authHeader, |
318 |
}); |
319 |
|
320 |
return { |
321 |
patron, |
322 |
...(generatedLibrary ? { library } : {}), |
323 |
...(generatedCategory ? { patron_category } : {}), |
256 |
}; |
324 |
}; |
257 |
}; |
325 |
}; |
258 |
|
326 |
|
Lines 363-369
const deleteSampleObjects = async allObjects => {
Link Here
|
363 |
return true; |
431 |
return true; |
364 |
}; |
432 |
}; |
365 |
|
433 |
|
366 |
const insertLibrary = async (library, baseUrl, authHeader) => { |
434 |
const insertLibrary = async ({ library, baseUrl, authHeader }) => { |
367 |
const { |
435 |
const { |
368 |
pickup_items, |
436 |
pickup_items, |
369 |
smtp_server, |
437 |
smtp_server, |
Lines 382-433
const insertLibrary = async (library, baseUrl, authHeader) => {
Link Here
|
382 |
}; |
450 |
}; |
383 |
|
451 |
|
384 |
const insertObject = async ({ type, object, baseUrl, authHeader }) => { |
452 |
const insertObject = async ({ type, object, baseUrl, authHeader }) => { |
385 |
if (type == "patron") { |
453 |
if (type == "library") { |
386 |
await query({ |
|
|
387 |
sql: "SELECT COUNT(*) AS count FROM branches WHERE branchcode = ?", |
388 |
values: [object.library_id], |
389 |
}).then(result => { |
390 |
if (!result[0].count) { |
391 |
insertLibrary(object.library, baseUrl, authHeader); |
392 |
} |
393 |
}); |
394 |
await query({ |
395 |
sql: "SELECT COUNT(*) AS count FROM categories WHERE categorycode= ?", |
396 |
values: [object.category_id], |
397 |
}).then(result => { |
398 |
if (!result[0].count) { |
399 |
query({ |
400 |
sql: "INSERT INTO categories(categorycode, description) VALUES (?, ?)", |
401 |
values: [ |
402 |
object.category_id, |
403 |
`description for ${object.category_id}`, |
404 |
], |
405 |
}); |
406 |
} |
407 |
}); |
408 |
const { |
409 |
_strings, |
410 |
anonymized, |
411 |
restricted, |
412 |
expired, |
413 |
extended_attributes, |
414 |
library, |
415 |
checkouts_count, |
416 |
overdues_count, |
417 |
account_balance, |
418 |
lang, |
419 |
login_attempts, |
420 |
sms_provider_id, |
421 |
...patron |
422 |
} = object; |
423 |
|
424 |
return apiPost({ |
425 |
endpoint: `/api/v1/patrons`, |
426 |
body: patron, |
427 |
baseUrl, |
428 |
authHeader, |
429 |
}); |
430 |
} else if (type == "library") { |
431 |
const keysToKeep = ["library_id", "name"]; |
454 |
const keysToKeep = ["library_id", "name"]; |
432 |
const library = Object.fromEntries( |
455 |
const library = Object.fromEntries( |
433 |
Object.entries(object).filter(([key]) => keysToKeep.includes(key)) |
456 |
Object.entries(object).filter(([key]) => keysToKeep.includes(key)) |
Lines 511-516
module.exports = {
Link Here
|
511 |
insertSampleBiblio, |
534 |
insertSampleBiblio, |
512 |
insertSampleHold, |
535 |
insertSampleHold, |
513 |
insertSampleCheckout, |
536 |
insertSampleCheckout, |
|
|
537 |
insertSamplePatron, |
514 |
insertObject, |
538 |
insertObject, |
515 |
deleteSampleObjects, |
539 |
deleteSampleObjects, |
516 |
}; |
540 |
}; |
517 |
- |
|
|