Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
charmingduchess committed Dec 19, 2024
1 parent 9851592 commit 95e2b82
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand Down
14 changes: 5 additions & 9 deletions lib/available_delivery_location_types.js
Original file line number Diff line number Diff line change
@@ -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
})
}
Expand All @@ -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 {
Expand All @@ -48,8 +48,4 @@ class AvailableDeliveryLocationTypes {
}
}

const patronTypeMapping = require('@nypl/nypl-core-objects')('by-patron-type')

AvailableDeliveryLocationTypes.patronTypeMapping = patronTypeMapping

module.exports = AvailableDeliveryLocationTypes
15 changes: 4 additions & 11 deletions lib/delivery-locations-resolver.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/jsonld_serializers.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions lib/loadNyplCore.js → lib/load_nypl_core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let _data
const _data = {}
const nyplCoreObjects = require('@nypl/nypl-core-objects')

const loadNyplCoreData = async () => {
Expand All @@ -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
}
12 changes: 7 additions & 5 deletions lib/location_label_updater.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sierraLocations = require('@nypl/nypl-core-objects')('by-sierra-location')
const { sierraLocations } = require('./load_nypl_core')

class LocationLabelUpdater {
constructor (responseReceived) {
Expand All @@ -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
})
Expand Down

0 comments on commit 95e2b82

Please sign in to comment.