diff --git a/app.js b/app.js index a279b95..4d5c09e 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,7 @@ const express = require('express') const esClient = require('./lib/elasticsearch/client') const loadConfig = require('./lib/load-config') const { preflightCheck } = require('./lib/preflight_check') +const { loadNyplCoreData } = require('./lib/load_nypl_core') const swaggerDocs = require('./swagger.v1.1.x.json') @@ -20,7 +21,7 @@ app.set('trust proxy', 'loopback') app.init = async () => { await loadConfig.loadConfig() - + await loadNyplCoreData() preflightCheck() // Load logger after running above to ensure we respect LOG_LEVEL if set diff --git a/lib/available_delivery_location_types.js b/lib/available_delivery_location_types.js index da9c4c3..5133f9f 100644 --- a/lib/available_delivery_location_types.js +++ b/lib/available_delivery_location_types.js @@ -1,18 +1,18 @@ const logger = require('./logger') const { makeNyplDataApiClient } = require('./data-api-client') +const { patronTypes } = require('./load_nypl_core') class AvailableDeliveryLocationTypes { static getScholarRoomByPatronId (patronID) { // If patronID is falsy (i.e. patron is not logged in) they're just a Rearcher: if (!patronID) return Promise.resolve(['Research']) - const patronTypeMapping = require('@nypl/nypl-core-objects')('by-patron-type') return this._getPatronTypeOf(patronID) .then((patronType) => { - if (this._isUnfamiliarPatronType(patronTypeMapping, patronType)) { + if (this._isUnfamiliarPatronType(patronTypes(), patronType)) { return } - const patronTypeData = patronTypeMapping[patronType] + const patronTypeData = patronTypes()[patronType] return patronTypeData.scholarRoom && patronTypeData.scholarRoom.code }) } @@ -38,8 +38,8 @@ class AvailableDeliveryLocationTypes { }) } - static _isUnfamiliarPatronType (patronTypeMapping, patronType) { - if (!patronTypeMapping[patronType]) { + static _isUnfamiliarPatronType (patronTypes, patronType) { + if (!patronTypes[patronType]) { logger.info(`Found the Patron Type: ${patronType} is not recognizable.`) return true } else { @@ -48,8 +48,4 @@ class AvailableDeliveryLocationTypes { } } -const patronTypeMapping = require('@nypl/nypl-core-objects')('by-patron-type') - -AvailableDeliveryLocationTypes.patronTypeMapping = patronTypeMapping - module.exports = AvailableDeliveryLocationTypes diff --git a/lib/delivery-locations-resolver.js b/lib/delivery-locations-resolver.js index 86ac983..a3b46cf 100644 --- a/lib/delivery-locations-resolver.js +++ b/lib/delivery-locations-resolver.js @@ -1,14 +1,13 @@ const { itemHasRecapHoldingLocation, barcodeFromItem } = require('./util') const scsbClient = require('./scsb-client') -const recapCustomerCodes = require('@nypl/nypl-core-objects')('by-recap-customer-code') -const sierraLocations = require('@nypl/nypl-core-objects')('by-sierra-location') +const { recapCustomerCodes, sierraLocations, m2CustomerCodes } = require('./load_nypl_core') const logger = require('./logger') const onsiteEddCriteria = require('../data/onsite-edd-criteria.json') const { isItemNyplOwned } = require('./ownership_determination') class DeliveryLocationsResolver { static nyplCoreLocation (locationCode) { - return sierraLocations[locationCode] + return sierraLocations()[locationCode] } static requestableBasedOnHoldingLocation (item) { @@ -57,14 +56,8 @@ class DeliveryLocationsResolver { // Fetch Sierra delivery locations by m2 customer code. Returns undefined if the m2 customer code is not requestable: static deliveryLocationsByM2CustomerCode (customerCode) { - let m2CustomerCodes - try { - m2CustomerCodes = require('@nypl/nypl-core-objects')('by-m2-customer-code') - } catch (e) { - - } - if (m2CustomerCodes && m2CustomerCodes[customerCode] && m2CustomerCodes[customerCode].sierraDeliveryLocations) { - const { sierraDeliveryLocations, requestable } = m2CustomerCodes[customerCode] + if (m2CustomerCodes()?.[customerCode]?.sierraDeliveryLocations) { + const { sierraDeliveryLocations, requestable } = m2CustomerCodes()[customerCode] if (requestable) { return sierraDeliveryLocations } else return undefined diff --git a/lib/jsonld_serializers.js b/lib/jsonld_serializers.js index a4361be..492881c 100644 --- a/lib/jsonld_serializers.js +++ b/lib/jsonld_serializers.js @@ -1,7 +1,6 @@ 'use strict' -const sierraLocations = require('@nypl/nypl-core-objects')('by-sierra-location') -const recordTypes = require('@nypl/nypl-core-objects')('by-record-types') +const { recordTypes, sierraLocations } = require('./load_nypl_core') const NyplSourceMapper = require('research-catalog-indexer/lib/utils/nypl-source-mapper') const util = require('./util.js') @@ -287,7 +286,7 @@ class ResourceSerializer extends JsonLdItemSerializer { } ResourceSerializer.getFormattedRecordType = function (recordTypeId) { - const prefLabel = recordTypes[recordTypeId]?.label + const prefLabel = recordTypes()[recordTypeId]?.label if (!prefLabel) return null return { '@id': recordTypeId, @@ -499,10 +498,10 @@ class AggregationSerializer extends JsonLdItemSerializer { v.label = p[1] } else if (field === 'buildingLocation') { // Build buildingLocation agg labels from nypl-core: - v.label = sierraLocations[v.value]?.label + v.label = sierraLocations()[v.value]?.label } else if (field === 'recordType') { // Build recordType agg labels from nypl-core: - v.label = recordTypes[v.value]?.label + v.label = recordTypes()[v.value]?.label // Unknown recordType? Remove it: if (!v.label) return null } else { diff --git a/lib/loadNyplCore.js b/lib/load_nypl_core.js similarity index 64% rename from lib/loadNyplCore.js rename to lib/load_nypl_core.js index 18d304b..fdaf878 100644 --- a/lib/loadNyplCore.js +++ b/lib/load_nypl_core.js @@ -1,4 +1,4 @@ -let _data +const _data = {} const nyplCoreObjects = require('@nypl/nypl-core-objects') const loadNyplCoreData = async () => { @@ -15,6 +15,11 @@ const loadNyplCoreData = async () => { })) } -const nyplCoreData = () => _data - -module.exports = { loadNyplCoreData, nyplCoreData } +module.exports = { + loadNyplCoreData, + patronTypes: () => _data.patronTypes, + sierraLocations: () => _data.sierraLocations, + recapCustomerCodes: () => _data.recapCustomerCodes, + recordTypes: () => _data.recordTypes, + m2CustomerCodes: () => _data.m2CustomerCodes +} diff --git a/lib/location_label_updater.js b/lib/location_label_updater.js index c23f787..46e6001 100644 --- a/lib/location_label_updater.js +++ b/lib/location_label_updater.js @@ -1,4 +1,4 @@ -const sierraLocations = require('@nypl/nypl-core-objects')('by-sierra-location') +const { sierraLocations } = require('./load_nypl_core') class LocationLabelUpdater { constructor (responseReceived) { @@ -10,20 +10,22 @@ class LocationLabelUpdater { const resp = this.elasticSearchResponse const updatedHits = resp.hits.hits.map((bib) => { // Update locations for items: - ; (bib._source.items || []).forEach((item) => { + const items = bib._source.items || [] + items.forEach((item) => { if (item.holdingLocation && item.holdingLocation.length > 0) { item.holdingLocation = item.holdingLocation.map((loc) => { - const nyplCoreEntry = sierraLocations[loc.id.replace(/^loc:/, '')] + const nyplCoreEntry = sierraLocations()[loc.id.replace(/^loc:/, '')] if (nyplCoreEntry) loc.label = nyplCoreEntry.label return loc }) } }) // Update locations for holdings: - ; (bib._source.holdings || []).forEach((holding) => { + const holdings = bib._source.holdings || [] + holdings.forEach((holding) => { if (holding.location && holding.location.length > 0) { holding.location = holding.location.map((loc) => { - const nyplCoreEntry = sierraLocations[loc.code.replace(/^loc:/, '')] + const nyplCoreEntry = sierraLocations()[loc.code.replace(/^loc:/, '')] if (nyplCoreEntry) loc.label = nyplCoreEntry.label return loc })