Skip to content

Commit

Permalink
Merge pull request #429 from NYPL/noref-nypl-core-objects-async
Browse files Browse the repository at this point in the history
NYPL core objects async refactor
  • Loading branch information
charmingduchess authored Dec 20, 2024
2 parents 9805396 + d953e6e commit c257110
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 148 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
2 changes: 1 addition & 1 deletion config/production.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NYPL_OAUTH_URL=https://isso.nypl.org/
ENCRYPTED_NYPL_OAUTH_ID=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAGswaQYJKoZIhvcNAQcGoFwwWgIBADBVBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDMLKVUQA58B6vprNcAIBEIAoaz0lI9EL2M9NyTuEwT8JDmPBt6aXfMiFs027DEuwsCN0wS0qWeFL1g==
ENCRYPTED_NYPL_OAUTH_SECRET=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAIcwgYQGCSqGSIb3DQEHBqB3MHUCAQAwcAYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAyWz91LOP2YP5fg0q0CARCAQ9inO9SV1M8R0Pkkx84r7UdwlU1FxfXvIjk/z6Qs81KBAVELhby2iD5LawQyDrR9tjhuMbotS6QnydwwMR/p8+qJXHI=

NYPL_CORE_VERSION=v2.22
NYPL_CORE_VERSION=v2.23

LOG_LEVEL=info
FEATURES=on-site-edd
Expand Down
2 changes: 1 addition & 1 deletion config/qa.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NYPL_OAUTH_URL=https://isso.nypl.org/
ENCRYPTED_NYPL_OAUTH_ID=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAGswaQYJKoZIhvcNAQcGoFwwWgIBADBVBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDMLKVUQA58B6vprNcAIBEIAoaz0lI9EL2M9NyTuEwT8JDmPBt6aXfMiFs027DEuwsCN0wS0qWeFL1g==
ENCRYPTED_NYPL_OAUTH_SECRET=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAIcwgYQGCSqGSIb3DQEHBqB3MHUCAQAwcAYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAyWz91LOP2YP5fg0q0CARCAQ9inO9SV1M8R0Pkkx84r7UdwlU1FxfXvIjk/z6Qs81KBAVELhby2iD5LawQyDrR9tjhuMbotS6QnydwwMR/p8+qJXHI=

NYPL_CORE_VERSION=v2.22
NYPL_CORE_VERSION=v2.23

LOG_LEVEL=info
FEATURES=on-site-edd
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 nyplCore = 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(patronType)) {
return
}
const patronTypeData = patronTypeMapping[patronType]
const patronTypeData = nyplCore.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 (patronType) {
if (!nyplCore.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
22 changes: 8 additions & 14 deletions lib/delivery-locations-resolver.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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 nyplCore = 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 nyplCore.sierraLocations()[locationCode]
}

static requestableBasedOnHoldingLocation (item) {
Expand Down Expand Up @@ -50,21 +50,15 @@ class DeliveryLocationsResolver {

// Fetch Sierra delivery locations by recap code
static deliveryLocationsByRecapCustomerCode (customerCode) {
if (recapCustomerCodes[customerCode] && recapCustomerCodes[customerCode].sierraDeliveryLocations) {
return recapCustomerCodes[customerCode].sierraDeliveryLocations
if (nyplCore.recapCustomerCodes()[customerCode] && nyplCore.recapCustomerCodes()[customerCode].sierraDeliveryLocations) {
return nyplCore.recapCustomerCodes()[customerCode].sierraDeliveryLocations
}
}

// 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 (nyplCore.m2CustomerCodes()?.[customerCode]?.sierraDeliveryLocations) {
const { sierraDeliveryLocations, requestable } = nyplCore.m2CustomerCodes()[customerCode]
if (requestable) {
return sierraDeliveryLocations
} else return undefined
Expand All @@ -73,7 +67,7 @@ class DeliveryLocationsResolver {

// Determine eddRequestable by recap customer code:
static __eddRequestableByCustomerCode (customerCode) {
if (recapCustomerCodes[customerCode]) return Boolean(recapCustomerCodes[customerCode].eddRequestable)
if (nyplCore.recapCustomerCodes()[customerCode]) return Boolean(nyplCore.recapCustomerCodes()[customerCode].eddRequestable)
}

// Determine eddRequestable by on-site EDD requestability criteria (presumed on-site):
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 locations = require('@nypl/nypl-core-objects')('by-sierra-location')
const recordTypes = require('@nypl/nypl-core-objects')('by-record-types')
const nyplCore = 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 = nyplCore.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 = locations[v.value]?.label
v.label = nyplCore.sierraLocations()[v.value]?.label
} else if (field === 'recordType') {
// Build recordType agg labels from nypl-core:
v.label = recordTypes[v.value]?.label
v.label = nyplCore.recordTypes()[v.value]?.label
// Unknown recordType? Remove it:
if (!v.label) return null
} else {
Expand Down
25 changes: 25 additions & 0 deletions lib/load_nypl_core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const _data = {}
const nyplCoreObjects = require('@nypl/nypl-core-objects')

const loadNyplCoreData = () => {
const vocabularies = {
sierraLocations: 'by-sierra-location',
recordTypes: 'by-record-types',
recapCustomerCodes: 'by-recap-customer-code',
m2CustomerCodes: 'by-m2-customer-code',
patronTypes: 'by-patron-type'
}
return Promise.all(Object.keys(vocabularies).map(async (vocab) => {
const nyplCoreValues = await nyplCoreObjects(vocabularies[vocab])
_data[vocab] = nyplCoreValues
}))
}

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 nyplCore = 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 = nyplCore.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 = nyplCore.sierraLocations()[loc.code.replace(/^loc:/, '')]
if (nyplCoreEntry) loc.label = nyplCoreEntry.label
return loc
})
Expand Down
Loading

0 comments on commit c257110

Please sign in to comment.