diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..a89f2d98 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,60 @@ +name: Deploy + +on: + push: + branches: + - qa + - production + - qa2 + +# Global environment variable based on the branch name +env: + ENV_TAG: ${{ github.ref_name }} + ECR_REPOSITORY: discovery-api + +jobs: + tests: + name: Run Tests + uses: ./.github/workflows/test.yml + + deploy: + permissions: + id-token: write + contents: read + runs-on: ubuntu-latest + needs: tests + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + role-to-assume: arn:aws:iam::946183545209:role/GithubActionsDeployerRole + aws-region: us-east-1 + + - name: Log in to ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Back up previous image for rollback + run: | + MANIFEST=$(aws ecr batch-get-image --repository-name ${{ env.ECR_REPOSITORY }} --image-ids imageTag="${{ env.ENV_TAG }}-latest" --output json | jq --raw-output --join-output '.images[0].imageManifest') + PREVIOUS_MANIFEST=$(aws ecr batch-get-image --repository-name ${{ env.ECR_REPOSITORY }} --image-ids imageTag="${{ env.ENV_TAG }}-previous" --output json | jq --raw-output --join-output '.images[0].imageManifest') + if [ "$MANIFEST" != "$PREVIOUS_MANIFEST" ]; then + aws ecr put-image --repository-name ${{ env.ECR_REPOSITORY }} --image-tag "${{ env.ENV_TAG }}-previous" --image-manifest "$MANIFEST" + fi + + - name: Build, tag, and push image to Amazon ECR + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG . + docker push $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG + docker tag $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:${{ env.ENV_TAG }}-latest + docker push $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:${{ env.ENV_TAG }}-latest + + - name: Force ECS Update + run: | + aws ecs update-service --cluster discovery-api-${{ env.ENV_TAG }} --service discovery-api-${{ env.ENV_TAG }} --force-new-deployment \ No newline at end of file diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index e1f2a8ee..d0634cb7 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -1,25 +1,44 @@ +name: Run Smoke Tests + +on: + workflow_run: + workflows: ["Deploy"] + types: [completed] + branches: [production, qa, qa2] + +permissions: + id-token: write + contents: read + +env: + ENV: ${{ github.event.workflow_run.head_branch }} + jobs: - integration-test-qa: + integration-test: permissions: id-token: write contents: read runs-on: ubuntu-latest - needs: tests - if: github.ref == 'refs/heads/qa2' - steps: + steps: - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: arn:aws:iam::946183545209:role/GithubActionsDeployerRole aws-region: us-east-1 - - uses: actions/checkout@v4 + - name: Wait for stable service + uses: nick-fields/retry@v3 + with: + max_attempts: 3 + timeout_minutes: 10 + command: aws ecs wait services-stable --cluster discovery-api-${{env.ENV}} --services discovery-api-${{env.ENV}} + - uses: actions/checkout@v3 - name: Set Node version - uses: actions/setup-node@v4 + uses: actions/setup-node@v3 with: - node-version-file: '.nvmrc' - - name: Install dependencies - run: npm ci - - name: Start service - run: ENV=qa npm start & - - name: Run tests - run: node test/integration/delivery-locations-by-barcode.test.js \ No newline at end of file + node-version-file: ".nvmrc" + - name: npm install + run: npm i + - name: Run integration tests + env: + ENV: "qa" + run: node test/integration/delivery-locations-by-barcode.test.js diff --git a/.github/workflows/rollback-qa2.yml b/.github/workflows/rollback-qa2.yml index 925d8b4f..6d54f636 100644 --- a/.github/workflows/rollback-qa2.yml +++ b/.github/workflows/rollback-qa2.yml @@ -1,7 +1,12 @@ name: Rollback qa2 -on: - workflow_dispatch: +on: + workflow_run: + workflows: ["Run Smoke Test"] + types: [completed] + +env: + ENV: ${{ github.event.workflow_run.head_branch }} jobs: # Rollback job in case of failure (Revert qa to the previous task definition) @@ -11,6 +16,7 @@ jobs: contents: read name: Rollback to Previous Version runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' && env.ENV == 'qa2'}} steps: - name: Checkout code uses: actions/checkout@v4 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..786584b5 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,25 @@ +name: Unit Tests + +on: + workflow_call: + push: + +permissions: + contents: read + +jobs: + tests: + permissions: + id-token: write + contents: read + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set Node version + uses: actions/setup-node@v3 + with: + node-version-file: '.nvmrc' + - name: npm install + run: npm ci + - name: Unit Tests + run: npm test \ No newline at end of file diff --git a/config/production.env b/config/production.env index 2635bc6a..43da8308 100644 --- a/config/production.env +++ b/config/production.env @@ -1,5 +1,5 @@ ENCRYPTED_ELASTICSEARCH_URI=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAJYwgZMGCSqGSIb3DQEHBqCBhTCBggIBADB9BgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDFWw8ECX9Pz81z0kvAIBEIBQGec9PCpwuvEgLH6imhqP6tx1fj8Vlf2ZipnUy06jzmpE262Qvk9LPAq7sIYPVkTCZctwilwcU9oC6yxasVoUlK87la77v03CeZsPIDwciFY= -ENCRYPTED_RESOURCES_INDEX=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAHcwdQYJKoZIhvcNAQcGoGgwZgIBADBhBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDLHxxMobbmmkVc76dQIBEIA0aZV7enzAKvaDMt7lJik1Ps8J6wikj4dJTL6YzjIlpRh8Dsl7tYgpEgsTNVqA7JRhErUT9w== +ENCRYPTED_RESOURCES_INDEX=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAHcwdQYJKoZIhvcNAQcGoGgwZgIBADBhBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDAk2mOM+zTrPU2rOagIBEIA0E3OuraTI5i3rqAiNrAj4RKV/c6DqotQ5nqSP8TzKk0qZYlHz+1jua11ZtKHIJ9LhszaE4Q== ENCRYPTED_ELASTICSEARCH_API_KEY=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAJ4wgZsGCSqGSIb3DQEHBqCBjTCBigIBADCBhAYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAyPOPaQCBbvKQhJoPQCARCAV2TlWlRh+xKnCegpprEQgfldZGcVW48RND0LVd/pQpVTJnRTtbCpP7damT7k8ziJVdWZ3jsfs5fw5YnKc/EIQ1M//DRUzOJL98ir5LTTxE7QhflKDtUY+Q== ENCRYPTED_SCSB_URL=AQECAHh7ea2tyZ6phZgT4B9BDKwguhlFtRC6hgt+7HbmeFsrsgAAAHwwegYJKoZIhvcNAQcGoG0wawIBADBmBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDKPFC8wFkVM5CyT6VQIBEIA5m4eLBkpChRA//ZNEWsRqIDGZmevb/thzI03a0NiAW6VfybSAYpFthh+bj/yAk1VEEBF6r1T4A2GP @@ -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.35 +NYPL_CORE_VERSION=v2.39 LOG_LEVEL=info FEATURES=on-site-edd diff --git a/config/qa.env b/config/qa.env index 627e25b8..05a98812 100644 --- a/config/qa.env +++ b/config/qa.env @@ -12,7 +12,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.35 +NYPL_CORE_VERSION=v2.39 LOG_LEVEL=debug FEATURES=on-site-edd diff --git a/config/test.env b/config/test.env index 3883094a..9267f131 100644 --- a/config/test.env +++ b/config/test.env @@ -10,7 +10,7 @@ NYPL_OAUTH_URL=http://oauth.example.com NYPL_OAUTH_ID=encrypted-nypl-oauth-id NYPL_OAUTH_SECRET=encrypted-nypl-oauth-id -NYPL_CORE_VERSION=v2.37 +NYPL_CORE_VERSION=v2.39 LOG_LEVEL=error FEATURES=on-site-edd diff --git a/lib/available_delivery_location_types.js b/lib/available_delivery_location_types.js index 16cf7bd6..1f3835ec 100644 --- a/lib/available_delivery_location_types.js +++ b/lib/available_delivery_location_types.js @@ -8,7 +8,6 @@ class AvailableDeliveryLocationTypes { if (!patronID) return null const patronType = await this._getPatronTypeOf(patronID) - if (this._isUnfamiliarPatronType(patronType)) { return null } diff --git a/lib/contributors.js b/lib/contributors.js index 0e7321de..90cdeb15 100644 --- a/lib/contributors.js +++ b/lib/contributors.js @@ -100,21 +100,33 @@ module.exports = function (app, _private = null) { } /** - * Builds an aggregation query that checks the resource index for counts on the contributorRoleLiteral field for a list of contributors. + * Builds an aggregation query that checks the resource index for counts on the browseableContributorRole_packed field for a list of contributors. */ const buildElasticRoleCountQuery = function (contributorList) { return { size: 0, query: { - terms: { - contributorRoleLiteral: contributorList + bool: { + should: [ + { + terms: { + 'creatorLiteral.keywordLowercased': contributorList + } + }, + { + terms: { + 'contributorLiteral.keywordLowercased': contributorList + } + } + ], + minimum_should_match: 1 } }, aggs: { contributor_role: { terms: { script: { - source: 'def results = []; for (val in doc["contributorRoleLiteral"]) { int pos = val.indexOf("||"); if (pos != -1) { String name = val.substring(0, pos); if (params.targets.contains(name)) { results.add(val); } } } return results;', + source: 'def results = []; for (val in doc["browseableContributorRole_packed"]) { int pos = val.indexOf("||"); if (pos != -1) { String name = val.substring(0, pos); if (params.targets.contains(name)) { results.add(val); } } } return results;', params: { targets: contributorList } diff --git a/lib/display-field-unpacker.js b/lib/display-field-unpacker.js new file mode 100644 index 00000000..5ae5b26e --- /dev/null +++ b/lib/display-field-unpacker.js @@ -0,0 +1,25 @@ +const parseValueAndLabel = (delimitedString) => { + if (!delimitedString.includes('||')) { + return { value: delimitedString, display: null } + } + const [value, display] = delimitedString.split('||') + return { value, display } +} + +module.exports = (elasticSearchResponse) => { + elasticSearchResponse.hits.hits.forEach((bib) => { + // Contributors and creators are packed like so || where + // can have prefix, title, and roles. We'd like to unpack them in a friendly format for the frontend + // to display the full label and use the isolated name for link-building + Object.entries(bib._source).forEach(([key, value]) => { + if (key.endsWith('_displayPacked')) { + const fieldName = key.replace('_displayPacked', '') + bib._source[fieldName + 'Display'] = value.map((packedValue) => parseValueAndLabel(packedValue)) + delete bib._source[key] + } + }) + + return bib + }) + return elasticSearchResponse +} diff --git a/lib/elasticsearch/config.js b/lib/elasticsearch/config.js index 4a64591c..bb74f353 100644 --- a/lib/elasticsearch/config.js +++ b/lib/elasticsearch/config.js @@ -90,7 +90,7 @@ const FILTER_CONFIG = { mediaType: { operator: 'match', field: ['mediaType.id', 'mediaType.label'], repeatable: true }, carrierType: { operator: 'match', field: ['carrierType.id', 'carrierType.label'], repeatable: true }, publisher: { operator: 'match', field: ['publisherLiteral.raw'], repeatable: true }, - contributorLiteral: { operator: 'match', field: ['contributorLiteral.keywordLowercased', 'parallelContributor.raw', 'creatorLiteral.keywordLowercased', 'parallelCreatorLiteral.raw'], repeatable: true }, + contributorLiteral: { operator: 'match', field: ['contributorLiteral.keywordLowercased', 'parallelContributorLiteral.raw', 'creatorLiteral.keywordLowercased', 'parallelCreatorLiteral.raw'], repeatable: true }, creatorLiteral: { operator: 'match', field: ['creatorLiteral.raw', 'parallelCreatorLiteral.raw'], repeatable: true }, issuance: { operator: 'match', field: ['issuance.id', 'issuance.label'], repeatable: true }, createdYear: { operator: 'match', field: ['createdYear'], repeatable: true }, diff --git a/lib/elasticsearch/elastic-query-builder.js b/lib/elasticsearch/elastic-query-builder.js index 3e2ec309..d5e0d1b0 100644 --- a/lib/elasticsearch/elastic-query-builder.js +++ b/lib/elasticsearch/elastic-query-builder.js @@ -183,7 +183,7 @@ class ElasticQueryBuilder { * Concat contributor + role if role param is provided */ applyContributorRole () { - this.query.addMust(termMatch('contributorRoleLiteral', this.request.params.filters.contributorLiteral + '||' + this.request.params.role)) + this.query.addMust(termMatch('browseableContributorRole_packed', this.request.params.filters.contributorLiteral + '||' + this.request.params.role)) } /** diff --git a/lib/jsonld_serializers.js b/lib/jsonld_serializers.js index 31081f7e..b3b1ca36 100644 --- a/lib/jsonld_serializers.js +++ b/lib/jsonld_serializers.js @@ -301,12 +301,14 @@ ResourceSerializer.getFormattedFormat = function (formatId) { ResourceSerializer.formatCollection = function (collectionId) { const prefLabel = nyplCore.collections()[`nyplCollection:${collectionId}`]?.label + const locationsPath = nyplCore.collections()[`nyplCollection:${collectionId}`]?.locationsPath const buildingLocationLabel = buildingLocations.find((loc) => loc.value === collectionId.slice(0, 2))?.label if (!prefLabel) return null return { '@id': collectionId, prefLabel, - buildingLocationLabel + buildingLocationLabel, + locationsPath } } @@ -374,10 +376,15 @@ class ItemResourceSerializer extends JsonLdItemSerializer { }) } + if (this.body.holdingLocation) { + stmts.holdingLocation = ItemResourceSerializer.getFormattedHoldingLocation(this.body.holdingLocation) + } + // Override default serialization of item.electronicLocator statements (full digital surrogates): if (this.body.electronicLocator) { stmts.electronicLocator = this.body.electronicLocator.map((link) => ResourceSerializer.formatElectronicResourceBlankNode(link, 'nypl:ElectronicLocation')) } + return stmts } @@ -386,6 +393,19 @@ class ItemResourceSerializer extends JsonLdItemSerializer { return (new ItemResourceSerializer(resp, options)).format() } + static getFormattedHoldingLocation (location) { + const loc = Array.isArray(location) ? location[0] : null + if (!loc) return [] + const locationId = loc['@id']?.split(':')[1] + const sierraLocations = nyplCore.sierraLocations() + const collectionAccessType = sierraLocations?.[locationId]?.collectionAccessType + return [{ + '@id': loc['@id'], + prefLabel: loc?.prefLabel, + collectionAccessType + }] + } + // Given an item, returns item with an added `identifier` // of form 'urn:[sourceIdentifierPrefix]:[sourceIdentifier]' // e.g. diff --git a/lib/models/Item.js b/lib/models/Item.js index 2a868f29..f1e990ec 100644 --- a/lib/models/Item.js +++ b/lib/models/Item.js @@ -114,7 +114,6 @@ Item.withDeliveryLocationsByBarcode = async function (item, scholarRoom) { const model = new Item(item) item.eddRequestable = !!model.eddRequestable const filteredDeliveryLocationsWithScholarRoom = locationUtils.filterLocations(model.deliveryLocation, scholarRoom) - item.deliveryLocation = locationUtils.formatLocations(filteredDeliveryLocationsWithScholarRoom) return item } diff --git a/lib/models/Location.js b/lib/models/Location.js index b73ab0f0..56a50be3 100644 --- a/lib/models/Location.js +++ b/lib/models/Location.js @@ -24,18 +24,7 @@ class Location { case 'm2-customer-code': return this.deliveryLocationsByM2CustomerCode default: - return this.deliveryLocationByHoldingLocation - } - } - - get deliveryLocationByHoldingLocation () { - if (this.nyplCoreLocation?.sierraDeliveryLocations?.length) { - // It's mapped, but the sierraDeliveryLocation entities only have `code` and `label` - // Do a second lookup to populate `deliveryLocationTypes` - return this.nyplCoreLocation.sierraDeliveryLocations.map((deliveryLocation) => { - deliveryLocation.deliveryLocationTypes = this.nyplCoreLocation.deliveryLocationTypes - return deliveryLocation - }) + return this.nyplCoreLocation?.sierraDeliveryLocations } } diff --git a/lib/resources.js b/lib/resources.js index f294f638..c4d2f516 100644 --- a/lib/resources.js +++ b/lib/resources.js @@ -48,6 +48,10 @@ const SORT_FIELDS = { initialDirection: 'asc', field: 'creator_sort' }, + callnumber: { + initialDirection: 'asc', + field: 'shelfMark.keywordLowercased' + }, relevance: {} } @@ -334,7 +338,6 @@ module.exports = function (app, _private = null) { // The resolved values of Promise.all are strictly ordered based on original array of promises const items = resp[0] const scholarRoom = resp[1] - // Use HTC API and nypl-core mappings to ammend ES response with deliveryLocations: return Promise.all(items.map(async (item) => Item.withDeliveryLocationsByBarcode(item, scholarRoom))) .catch((e) => { diff --git a/lib/response_massager.js b/lib/response_massager.js index a1eb17ec..034580a5 100644 --- a/lib/response_massager.js +++ b/lib/response_massager.js @@ -1,6 +1,7 @@ const LocationLabelUpdater = require('./location_label_updater') const AvailabilityResolver = require('./availability_resolver.js') const parallelFieldsExtractor = require('./parallel-fields-extractor') +const displayFieldUnpacker = require('./display-field-unpacker') const { isAeonUrl, sortOnPropWithUndefinedLast } = require('../lib/util') const FulfillmentResolver = require('./fulfillment_resolver') const fixItemRequestability = require('./requestability_resolver') @@ -73,6 +74,9 @@ class ResponseMassager { // Rename parallel fields: response = parallelFieldsExtractor(response) + // Extract display values and labels from packed fields + response = displayFieldUnpacker(response) + // Update ES response with updated availability from SCSB: const updatedWithAvailability = (new AvailabilityResolver(response)) .responseWithUpdatedAvailability(options) diff --git a/lib/vocabularies.js b/lib/vocabularies.js index b7627e48..451f1e81 100644 --- a/lib/vocabularies.js +++ b/lib/vocabularies.js @@ -17,7 +17,8 @@ module.exports = function (app, _private = null) { (val) => ({ value: val.code, label: val.label, - holdingLocations: val.holdingLocations + holdingLocations: val.holdingLocations, + locationsPath: val.locationsPath }) ) return ({ formats, collections, languages: languages.values, buildingLocations }) diff --git a/package-lock.json b/package-lock.json index d253d9a4..0ba9982a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@aws-sdk/client-kms": "^3.948.0", "@elastic/elasticsearch": "~8.12.0", - "@nypl/nypl-core-objects": "3.0.4", + "@nypl/nypl-core-objects": "3.0.5", "@nypl/nypl-data-api-client": "^2.0.0", "@nypl/scsb-rest-client": "3.0.0", "dotenv": "^16.4.5", @@ -3589,9 +3589,9 @@ } }, "node_modules/@nypl/nypl-core-objects": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/@nypl/nypl-core-objects/-/nypl-core-objects-3.0.4.tgz", - "integrity": "sha512-xFo14urEMO2VUL2/YcjG3io1deU/UCKtYlI2ADCOTEwOcCpwzzvkvOvTI2E2CvgstkHplVejBjdvd4l6ak0inQ==", + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/@nypl/nypl-core-objects/-/nypl-core-objects-3.0.5.tgz", + "integrity": "sha512-XXQ15ekqBgn/XX9eNmU40EE+OpIYSyTkq/TdAEhmTRiHWC+tohT8cfDNABz8lH+xnuqiwam6Fmvla3g5KI2DHQ==", "dependencies": { "axios": "^1.6.8", "csv": "^5.3.2", @@ -7255,9 +7255,9 @@ "license": "MIT" }, "node_modules/follow-redirects": { - "version": "1.15.6", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", - "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "version": "1.15.11", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz", + "integrity": "sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ==", "funding": [ { "type": "individual", @@ -10389,6 +10389,27 @@ "node": ">=10" } }, + "node_modules/research-catalog-indexer/node_modules/@nypl/nypl-core-objects": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@nypl/nypl-core-objects/-/nypl-core-objects-3.0.4.tgz", + "integrity": "sha512-xFo14urEMO2VUL2/YcjG3io1deU/UCKtYlI2ADCOTEwOcCpwzzvkvOvTI2E2CvgstkHplVejBjdvd4l6ak0inQ==", + "dependencies": { + "axios": "^1.6.8", + "csv": "^5.3.2", + "csv-stringify": "^5.6.0", + "just-flatten": "^1.0.0" + } + }, + "node_modules/research-catalog-indexer/node_modules/@nypl/nypl-core-objects/node_modules/axios": { + "version": "1.13.6", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.13.6.tgz", + "integrity": "sha512-ChTCHMouEe2kn713WHbQGcuYrr6fXTBiu460OTwWrWob16g1bXn4vtz07Ope7ewMozJAnEquLk5lWQWtBig9DQ==", + "dependencies": { + "follow-redirects": "^1.15.11", + "form-data": "^4.0.5", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/research-catalog-indexer/node_modules/@nypl/scsb-rest-client": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@nypl/scsb-rest-client/-/scsb-rest-client-2.0.0.tgz", @@ -10407,6 +10428,11 @@ "form-data": "^4.0.0" } }, + "node_modules/research-catalog-indexer/node_modules/csv-stringify": { + "version": "5.6.5", + "resolved": "https://registry.npmjs.org/csv-stringify/-/csv-stringify-5.6.5.tgz", + "integrity": "sha512-PjiQ659aQ+fUTQqSrd1XEDnOr52jh30RBurfzkscaE2tPaFsDH5wOAHJiw8XAHphRknCwMUE9KRayc4K/NbO8A==" + }, "node_modules/research-catalog-indexer/node_modules/debug": { "version": "4.3.7", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz", diff --git a/package.json b/package.json index 8443ec2b..d58cc938 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "dependencies": { "@aws-sdk/client-kms": "^3.948.0", "@elastic/elasticsearch": "~8.12.0", - "@nypl/nypl-core-objects": "3.0.4", + "@nypl/nypl-core-objects": "3.0.5", "@nypl/nypl-data-api-client": "^2.0.0", "@nypl/scsb-rest-client": "3.0.0", "dotenv": "^16.4.5", diff --git a/routes/resources.js b/routes/resources.js index 9b7e394f..171393ab 100644 --- a/routes/resources.js +++ b/routes/resources.js @@ -90,7 +90,7 @@ module.exports = function (app) { * * e.g. discovery/resources/b1234-i9876 */ - app.get(`/api/v${VER}/discovery/resources/:uri-:itemUri([a-z]?i[0-9]+)`, function (req, res, next) { + app.get(`/api/v${VER}/discovery/resources/:uri-:itemUri([a-z]?i[a-z0-9]+)`, function (req, res, next) { const params = { uri: req.params.uri, itemUri: req.params.itemUri } return app.resources.findByUri(params, { baseUrl: app.baseUrl }, req) diff --git a/test/display-field-unpacker.test.js b/test/display-field-unpacker.test.js new file mode 100644 index 00000000..3f768393 --- /dev/null +++ b/test/display-field-unpacker.test.js @@ -0,0 +1,22 @@ +const { expect } = require('chai') +const displayFieldsUnpacker = require('../lib/display-field-unpacker') +const packedDisplayBib = require('./fixtures/packed-display-response.json') + +describe('Display field unpacker', () => { + describe('When a bib has a packed display property', () => { + it('adds each of the items in that array as unpacked objects', () => { + const displayFieldsUnpacked = displayFieldsUnpacker(packedDisplayBib).hits.hits[0]._source + expect(Object.keys(displayFieldsUnpacked).length).to.equal(2) + expect(displayFieldsUnpacked).to.deep.equal({ + testDisplay: [ + { value: 'someValue', display: 'someDisplay' }, + { value: 'someValueB', display: 'someDisplayB' }, + { value: 'someValueC', display: null } + ], + testOtherDisplay: [ + { value: 'otherValue', display: 'otherDisplay' } + ] + }) + }) + }) +}) diff --git a/test/elastic-query-builder.test.js b/test/elastic-query-builder.test.js index 48c08440..062a9ec0 100644 --- a/test/elastic-query-builder.test.js +++ b/test/elastic-query-builder.test.js @@ -272,7 +272,7 @@ describe('ElasticQueryBuilder', () => { const inst = ElasticQueryBuilder.forApiRequest(request) expect(inst.query.toJson()).to.nested - .include({ 'bool.must[0].term.contributorRoleLiteral.value': 'Patinkin, Mandy||performer.' }) + .include({ 'bool.must[0].term.browseableContributorRole_packed.value': 'Patinkin, Mandy||performer.' }) }) }) diff --git a/test/fixtures/packed-display-response.json b/test/fixtures/packed-display-response.json new file mode 100644 index 00000000..3a304c50 --- /dev/null +++ b/test/fixtures/packed-display-response.json @@ -0,0 +1,18 @@ +{ + "hits": { + "hits": [ + { + "_source": { + "test_displayPacked": [ + "someValue||someDisplay", + "someValueB||someDisplayB", + "someValueC" + ], + "testOther_displayPacked": [ + "otherValue||otherDisplay" + ] + } + } + ] + } +} diff --git a/test/integration/delivery-locations-by-barcode.test.js b/test/integration/delivery-locations-by-barcode.test.js index 2302b12b..c50f1d0b 100644 --- a/test/integration/delivery-locations-by-barcode.test.js +++ b/test/integration/delivery-locations-by-barcode.test.js @@ -1,46 +1,69 @@ -require('dotenv').config('config/qa.env') -const axios = require('axios') -const { expectations, ptypes } = require('./delivery-locations-constants') +// const { loadConfig } = require('../../lib/load-config') +// const { expectations, ptypes } = require('./delivery-locations-constants') +// const { makeNyplDataApiClient } = require('../../lib/data-api-client') -const checkLocationsForPtype = async (ptype) => { - const problems = [] - const match = [] - await Promise.all(Object.values(expectations).map(async (expectation) => { - const deliveryLocationsFromApi = await getDeliveryLocations(expectation.barcode, ptypes[ptype]) - let totalMatch = true - const registerProblem = (problem) => { - problems.push({ barcode: expectation.barcode, deliveryLocationsFromApi, ...problem }) - totalMatch = false - } - const checkForValue = (expectedValue, action) => { - const includedValueIncluded = deliveryLocationsFromApi.some((label) => label.includes(expectedValue)) - const match = action === 'include' ? includedValueIncluded : !includedValueIncluded - if (!match) { - registerProblem({ [`expectedTo${action}`]: expectedValue }) - } - } - expectation[ptype].includes.forEach((expectedValue) => checkForValue(expectedValue, 'include')) - expectation[ptype].excludes.forEach((expectedValue) => checkForValue(expectedValue, 'exclude')) - if (totalMatch) match.push({ barcode: expectation.barcode, deliveryLocationsFromApi, expectedToInclude: expectation[ptype].includes, expectedToExclude: expectation[ptype].excludes }) - })) - return { match, problems } -} +// const checkLocationsForPtype = async (ptype) => { +// const problems = [] +// const match = [] -const getDeliveryLocations = async (barcode, patronId) => { - const { data: { itemListElement: deliveryLocationsPerRecord } } = await axios.get(`http://localhost:8082/api/v0.1/request/deliveryLocationsByBarcode?barcodes[]=${barcode}&patronId=${patronId}`) - // per record - return deliveryLocationsPerRecord[0] - .deliveryLocation.map(loc => loc.prefLabel.toLowerCase()) -} +// await Promise.all(Object.entries(expectations).map(async ([holdingLocation, expectation], i) => { +// let deliveryLocationsFromApi +// let totalMatch = true +// const registerProblem = (problem) => { +// problems.push({ holdingLocation, barcode: expectation.barcode, deliveryLocationsFromApi, ...problem }) +// totalMatch = false +// } +// try { +// deliveryLocationsFromApi = await getDeliveryLocations(expectation.barcode, ptypes[ptype]) +// } catch (e) { +// registerProblem({ lookUpFailed: true }) +// return +// } + +// const checkForValue = (expectedValue, action) => { +// const includedValueIncluded = deliveryLocationsFromApi.some((label) => label.includes(expectedValue)) +// const match = action === 'include' ? includedValueIncluded : !includedValueIncluded +// if (!match) { +// registerProblem({ [`expectedTo${action}`]: expectedValue }) +// } +// } +// expectation[ptype].includes.forEach((expectedValue) => checkForValue(expectedValue, 'include')) +// expectation[ptype].excludes.forEach((expectedValue) => checkForValue(expectedValue, 'exclude')) +// if (totalMatch) match.push({ barcode: expectation.barcode, deliveryLocationsFromApi, expectedToInclude: expectation[ptype].includes, expectedToExclude: expectation[ptype].excludes }) +// })) +// return { match, problems } +// } + +// const getDeliveryLocations = async (barcode, patronId) => { +// try { +// const { itemListElement: itemData } = await makeNyplDataApiClient().get(`request/deliveryLocationsByBarcode?barcodes[]=${barcode}&patronId=${patronId}`) +// // per record +// return itemData[0] +// .deliveryLocation.map(loc => loc.prefLabel.toLowerCase()) +// } catch (e) { +// console.error(e) +// } +// } + +// const theThing = async () => { +// throw new Error('spaghetti') +// await loadConfig() +// const results = await Promise.all(Object.keys(ptypes).map((checkLocationsForPtype))) +// const resultsHaveProblems = Object.keys(ptypes).some((ptype, i) => { +// const resultsForPtype = results[i] +// if (resultsForPtype.problems.length) { +// console.error(`Error with ${ptype} ptype delivery results, `, resultsForPtype.problems) +// return true +// } else { +// console.log(`All delivery location checks for ${ptype} patron type successful`) +// return false +// } +// }) +// if (resultsHaveProblems) throw new Error('Delivery location checks failed.') +// } const theThing = async () => { - const results = await Promise.all(Object.keys(ptypes).map((checkLocationsForPtype))) - Object.keys(ptypes).forEach((ptype, i) => { - const resultsForPtype = results[i] - if (resultsForPtype.problems.length) { - console.error(`Error with ${ptype} ptype delivery results, `, resultsForPtype.problems) - } else console.log(`All delivery location checks for ${ptype} patron type successful`) - }) + throw new Error('spaghetti') } theThing() diff --git a/test/integration/delivery-locations-constants.js b/test/integration/delivery-locations-constants.js index b46d812c..6b70cfd7 100644 --- a/test/integration/delivery-locations-constants.js +++ b/test/integration/delivery-locations-constants.js @@ -38,11 +38,11 @@ const expectations = { scholar: { includes: [schomburg], excludes: [scholar, sasb, lpa] }, general: { includes: [schomburg], excludes: [scholar, sasb, lpa] } }, - // nyplM1: { - // barcode: null, - // scholar: { includes: [sasb], excludes: [scholar, lpa, schomburg] }, - // general: { includes: [sasb], excludes: [scholar, lpa, schomburg] } - // }, + nyplM1: { + barcode: '33433084847221', + scholar: { includes: [sasb, scholar], excludes: [lpa, schomburg] }, + general: { includes: [sasb], excludes: [scholar, lpa, schomburg] } + }, nyplM2: { barcode: '33333069027734', scholar: { includes: [sasb, scholar], excludes: [lpa, schomburg] }, diff --git a/test/item-resource-serializer.test.js b/test/item-resource-serializer.test.js index 57429231..3387c8c8 100644 --- a/test/item-resource-serializer.test.js +++ b/test/item-resource-serializer.test.js @@ -60,7 +60,19 @@ describe('ItemResourceSerializer', () => { expect(doc.idNyplSourceId['@value']).to.eq('9876543210') }) }) - + describe('getFormattedHoldingLocation', () => { + it('should return holding location with id, label, and collection access type', () => { + const locationEntity = ItemResourceSerializer.getFormattedHoldingLocation([ + { + '@id': 'loc:maff1', + prefLabel: 'Schwarzman Building - Dorot Jewish Division Reference Room 111' + } + ]) + expect(locationEntity[0].prefLabel).to.equal('Schwarzman Building - Dorot Jewish Division Reference Room 111') + expect(locationEntity[0]['@id']).to.equal('loc:maff1') + expect(locationEntity[0].collectionAccessType).to.equal('shelf') + }) + }) describe('addSourceIdentifier', () => { it('adds source identifier for NYPL', async () => { const item = { uri: 'i1234' } diff --git a/test/resource_serializer.test.js b/test/resource_serializer.test.js index be0427e6..133671ae 100644 --- a/test/resource_serializer.test.js +++ b/test/resource_serializer.test.js @@ -9,11 +9,13 @@ describe('Resource Serializer', () => { { '@id': 'mal', buildingLocationLabel: 'Stephen A. Schwarzman Building (SASB)', + locationsPath: 'locations/schwarzman/general-research-division', prefLabel: 'General Research Division' }, { '@id': 'bur', buildingLocationLabel: 'Stavros Niarchos Foundation Library (SNFL)', + locationsPath: 'locations/snfl/yoseloff-business', prefLabel: 'Yoseloff Business Center' } ]) @@ -30,6 +32,7 @@ describe('Resource Serializer', () => { expect(collectionEntity.prefLabel).to.equal('Art & Architecture Collection') expect(collectionEntity['@id']).to.equal('mab') expect(collectionEntity.buildingLocationLabel).to.equal('Stephen A. Schwarzman Building (SASB)') + expect(collectionEntity.locationsPath).to.equal('locations/schwarzman/wallach-division/art-architecture-collection') }) }) describe('.formatItemFilterAggregations()', () => { diff --git a/test/vocabularies.test.js b/test/vocabularies.test.js index d3bdb48d..81c87a6e 100644 --- a/test/vocabularies.test.js +++ b/test/vocabularies.test.js @@ -57,6 +57,6 @@ describe('Vocabularies', function () { const results = await app.vocabularies({}, { baseUrl: app.baseUrl }) - expect(results.collections[0]).to.have.keys(['value', 'label', 'holdingLocations']) + expect(results.collections[0]).to.have.keys(['value', 'label', 'holdingLocations', 'locationsPath']) }) })