From 509ca6943cd2eccabcc9e67d70a6613d5e97c93e Mon Sep 17 00:00:00 2001 From: Oleksandr Hladchenko1 Date: Fri, 28 Feb 2025 13:55:54 +0100 Subject: [PATCH] UIIN-3174: Use createVersionHistoryFieldFormatter for holdings and items --- .../HoldingVersionHistory.js | 20 +---- src/utils.js | 31 +++++++ src/utils.test.js | 90 +++++++++++++++++++ 3 files changed, 123 insertions(+), 18 deletions(-) diff --git a/src/Holding/HoldingVersionHistory/HoldingVersionHistory.js b/src/Holding/HoldingVersionHistory/HoldingVersionHistory.js index 81376d888..19881c141 100644 --- a/src/Holding/HoldingVersionHistory/HoldingVersionHistory.js +++ b/src/Holding/HoldingVersionHistory/HoldingVersionHistory.js @@ -6,6 +6,7 @@ import { AuditLogPane } from '@folio/stripes-acq-components'; import { useHoldingAuditDataQuery } from '../../hooks'; import { DataContext } from '../../contexts'; +import { createVersionHistoryFieldFormatter } from '../../utils'; const HoldingVersionHistory = ({ onClose, holdingId }) => { const { formatMessage } = useIntl(); @@ -46,24 +47,7 @@ const HoldingVersionHistory = ({ onClose, holdingId }) => { entries: formatMessage({ id: 'ui-inventory.receivingHistory' }), }; - const fieldFormatter = { - discoverySuppress: value => value.toString(), - holdingsTypeId: value => referenceData.holdingsTypes?.find(type => type.id === value)?.name, - statisticalCodeIds: value => { - const statisticalCode = referenceData.statisticalCodes?.find(code => code.id === value); - - return `${statisticalCode.statisticalCodeType.name}: ${statisticalCode.code} - ${statisticalCode.name}`; - }, - callNumberTypeId: value => referenceData.callNumberTypes?.find(type => type.id === value)?.name, - permanentLocationId: value => referenceData.locationsById[value]?.name, - temporaryLocationId: value => referenceData.locationsById[value]?.name, - effectiveLocationId: value => referenceData.locationsById[value]?.name, - illPolicyId: value => referenceData.illPolicies.find(policy => policy.id === value)?.name, - staffOnly: value => value.toString(), - holdingsNoteTypeId: value => referenceData.holdingsNoteTypes?.find(noteType => noteType.id === value)?.name, - relationshipId: value => referenceData.electronicAccessRelationships?.find(noteType => noteType.id === value)?.name, - publicDisplay: value => value.toString(), - }; + const fieldFormatter = createVersionHistoryFieldFormatter(referenceData); return ( { return tenants?.filter(tenant => tenant.id !== currentTenantId && tenant.id !== centralTenantId); }; + +export const createVersionHistoryFieldFormatter = (referenceData, circulationHistory = {}) => ({ + discoverySuppress: value => value.toString(), + typeId: value => referenceData.callNumberTypes?.find(type => type.id === value)?.name, + itemLevelCallNumberTypeId: value => referenceData.callNumberTypes?.find(type => type.id === value)?.name, + itemDamagedStatusId: value => referenceData.itemDamagedStatuses?.find(type => type.id === value)?.name, + permanentLocationId: value => referenceData.locationsById[value]?.name, + temporaryLocationId: value => referenceData.locationsById[value]?.name, + effectiveLocationId: value => referenceData.locationsById[value]?.name, + permanentLoanTypeId: value => referenceData.loanTypes?.find(type => type.id === value)?.name, + temporaryLoanTypeId: value => referenceData.loanTypes?.find(type => type.id === value)?.name, + materialTypeId: value => referenceData.materialTypes?.find(type => type.id === value)?.name, + statisticalCodeIds: value => { + const statisticalCode = referenceData.statisticalCodes?.find(code => code.id === value); + + return `${statisticalCode.statisticalCodeType.name}: ${statisticalCode.code} - ${statisticalCode.name}`; + }, + relationshipId: value => referenceData.electronicAccessRelationships?.find(type => type.id === value)?.name, + staffOnly: value => value.toString(), + itemNoteTypeId: value => referenceData.itemNoteTypes?.find(type => type.id === value)?.name, + date: value => getDateWithTime(value), + servicePointId: () => circulationHistory.servicePointName, + staffMemberId: () => circulationHistory.source, + dateTime: value => getDateWithTime(value), + source: value => `${value.personal.lastName}, ${value.personal.firstName}`, + holdingsTypeId: value => referenceData.holdingsTypes?.find(type => type.id === value)?.name, + callNumberTypeId: value => referenceData.callNumberTypes?.find(type => type.id === value)?.name, + illPolicyId: value => referenceData.illPolicies.find(policy => policy.id === value)?.name, + holdingsNoteTypeId: value => referenceData.holdingsNoteTypes?.find(noteType => noteType.id === value)?.name, + publicDisplay: value => value.toString(), +}); diff --git a/src/utils.test.js b/src/utils.test.js index ea49de9cc..a31305751 100644 --- a/src/utils.test.js +++ b/src/utils.test.js @@ -22,6 +22,7 @@ import { checkIfCentralOrderingIsActive, omitCurrentAndCentralTenants, marshalInstance, + createVersionHistoryFieldFormatter, } from './utils'; import { CONTENT_TYPE_HEADER, @@ -490,3 +491,92 @@ describe('hasMemberTenantPermission', () => { }); }); }); + +describe('createVersionHistoryFieldFormatter', () => { + const date = '2024-02-26T12:00:00Z'; + const mockReferenceData = { + callNumberTypes: [{ id: '123', name: 'Test Call Number Type' }], + itemDamagedStatuses: [{ id: 'damaged-1', name: 'Damaged' }], + locationsById: { 'location-1': { name: 'Main Library' } }, + loanTypes: [{ id: 'loan-1', name: 'Short Term' }], + materialTypes: [{ id: 'material-1', name: 'Book' }], + statisticalCodes: [{ id: 'stat-1', statisticalCodeType: { name: 'Category' }, code: '001', name: 'Stat Code' }], + electronicAccessRelationships: [{ id: 'rel-1', name: 'Online Access' }], + itemNoteTypes: [{ id: 'note-1', name: 'Public Note' }], + }; + const mockCirculationHistory = { + servicePointName: 'Main Desk', + source: 'Librarian User', + }; + + const fieldFormatter = createVersionHistoryFieldFormatter(mockReferenceData, mockCirculationHistory); + + it('should format discoverySuppress field correctly', () => { + expect(fieldFormatter.discoverySuppress(true)).toBe('true'); + expect(fieldFormatter.discoverySuppress(false)).toBe('false'); + }); + + it('should format typeId field correctly', () => { + expect(fieldFormatter.typeId('123')).toBe('Test Call Number Type'); + }); + + it('should format typeId itemLevelCallNumberTypeId correctly', () => { + expect(fieldFormatter.itemLevelCallNumberTypeId('123')).toBe('Test Call Number Type'); + }); + + it('should format itemDamagedStatusId field correctly', () => { + expect(fieldFormatter.itemDamagedStatusId('damaged-1')).toBe('Damaged'); + }); + + it('should format location IDs field correctly', () => { + expect(fieldFormatter.permanentLocationId('location-1')).toBe('Main Library'); + expect(fieldFormatter.effectiveLocationId('location-1')).toBe('Main Library'); + expect(fieldFormatter.temporaryLocationId('location-1')).toBe('Main Library'); + }); + + it('should format loan types field correctly', () => { + expect(fieldFormatter.permanentLoanTypeId('loan-1')).toBe('Short Term'); + expect(fieldFormatter.temporaryLoanTypeId('loan-1')).toBe('Short Term'); + }); + + it('should format material types field correctly', () => { + expect(fieldFormatter.materialTypeId('material-1')).toBe('Book'); + }); + + it('should format statistical codes field correctly', () => { + expect(fieldFormatter.statisticalCodeIds('stat-1')).toBe('Category: 001 - Stat Code'); + }); + + it('should format electronic access relationships field correctly', () => { + expect(fieldFormatter.relationshipId('rel-1')).toBe('Online Access'); + }); + + it('should format item note types field correctly', () => { + expect(fieldFormatter.itemNoteTypeId('note-1')).toBe('Public Note'); + }); + + it('should format staffOnly field correctly', () => { + expect(fieldFormatter.staffOnly(true)).toBe('true'); + expect(fieldFormatter.staffOnly(false)).toBe('false'); + }); + + it('should format date field correctly', () => { + expect(fieldFormatter.date(date)).toBe(`Formatted Date: ${date}`); + }); + + it('should format dateTime field correctly', () => { + expect(fieldFormatter.dateTime(date)).toBe(`Formatted Date: ${date}`); + }); + + it('should format servicePointId field correctly', () => { + expect(fieldFormatter.servicePointId()).toBe('Main Desk'); + }); + + it('should format staffMemberId field correctly', () => { + expect(fieldFormatter.staffMemberId()).toBe('Librarian User'); + }); + + it('should format source field correctly', () => { + expect(fieldFormatter.source({ personal: { firstName: 'John', lastName: 'Doe' } })).toBe('Doe, John'); + }); +});