diff --git a/src/Item/ItemVersionHistory/ItemVersionHistory.js b/src/Item/ItemVersionHistory/ItemVersionHistory.js index 99fd0b6c8..11e3cd47e 100644 --- a/src/Item/ItemVersionHistory/ItemVersionHistory.js +++ b/src/Item/ItemVersionHistory/ItemVersionHistory.js @@ -6,7 +6,33 @@ import { AuditLogPane } from '@folio/stripes-acq-components'; import { useItemAuditDataQuery } from '../../hooks'; import { DataContext } from '../../contexts'; -import { createVersionHistoryFieldFormatter } from '../../utils'; +import { getDateWithTime } from '../../utils'; + +export const createFieldFormatter = (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}`, +}); const ItemVersionHistory = ({ onClose, @@ -63,7 +89,7 @@ const ItemVersionHistory = ({ date: formatMessage({ id: 'ui-inventory.date' }), }; - const fieldFormatter = createVersionHistoryFieldFormatter(referenceData, circulationHistory); + const fieldFormatter = createFieldFormatter(referenceData, circulationHistory); return ( ({ ...jest.requireActual('@folio/stripes-acq-components'), @@ -30,6 +30,21 @@ const queryClient = new QueryClient(); const onCloseMock = jest.fn(); const itemId = 'itemId'; +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 renderItemVersionHistory = () => { const component = ( @@ -54,3 +69,76 @@ describe('ItemVersionHistory', () => { expect(screen.getByText('Version history')).toBeInTheDocument(); }); }); + +describe('createFieldFormatter', () => { + const fieldFormatter = createFieldFormatter(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'); + }); +}); diff --git a/src/utils.js b/src/utils.js index a756c22e6..47430e20f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1128,34 +1128,3 @@ export const omitCurrentAndCentralTenants = (stripes) => { export const getIsVersionHistoryEnabled = settings => { return settings?.find(setting => setting.key === VERSION_HISTORY_ENABLED_SETTING)?.value; }; - -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 f570a070c..cbf26ff8f 100644 --- a/src/utils.test.js +++ b/src/utils.test.js @@ -23,7 +23,6 @@ import { omitCurrentAndCentralTenants, marshalInstance, getIsVersionHistoryEnabled, - createVersionHistoryFieldFormatter, } from './utils'; import { CONTENT_TYPE_HEADER, @@ -507,92 +506,3 @@ describe('getIsVersionHistoryEnabled', () => { expect(getIsVersionHistoryEnabled(settings)).toBe(false); }); }); - -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'); - }); -});