diff --git a/CHANGELOG.md b/CHANGELOG.md index 0de2faab..5680d86d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Add more reusable hooks and utilities. Refs UISACQCOMP-228. * Move reusable version history components to the ACQ lib. Refs UISACQCOMP-230. +* Move reusable helper function to support version history functionality. Refs UISACQCOMP-232. ## [6.0.1](https://github.com/folio-org/stripes-acq-components/tree/v6.0.1) (2024-11-14) [Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v6.0.0...v6.0.1) diff --git a/lib/VersionHistory/getVersionMetadata.js b/lib/VersionHistory/getVersionMetadata.js new file mode 100644 index 00000000..7bc57532 --- /dev/null +++ b/lib/VersionHistory/getVersionMetadata.js @@ -0,0 +1,7 @@ +import get from 'lodash/get'; + +export const getVersionMetadata = (version, entity) => ({ + ...get(entity, 'metadata', {}), + updatedByUserId: version?.userId, + updatedDate: version?.actionDate, +}); diff --git a/lib/VersionHistory/getVersionMetadata.test.js b/lib/VersionHistory/getVersionMetadata.test.js new file mode 100644 index 00000000..bceeec74 --- /dev/null +++ b/lib/VersionHistory/getVersionMetadata.test.js @@ -0,0 +1,29 @@ +import { getVersionMetadata } from './getVersionMetadata'; + +const version = { + userId: 'userId', + actionDate: '2024-11-21T05:14:30.510+00:00', + eventDate: '2024-11-21T05:14:30.510+00:00', +}; + +describe('getVersionMetadata', () => { + it('should return metadata from entity and updatedByUserId and updatedDate from version', () => { + const entity = { + metadata: { + metadataKey: 'metadataValue', + createdDate: '2024-11-21T01:55:55.066+00:00', + }, + }; + + expect(getVersionMetadata(version, entity)).toEqual({ + metadataKey: 'metadataValue', + updatedByUserId: 'userId', + updatedDate: version.actionDate, + ...entity.metadata, + }); + }); + + it('should return empty object if version is not provided', () => { + expect(getVersionMetadata(null, {})).toEqual({}); + }); +}); diff --git a/lib/VersionHistory/index.js b/lib/VersionHistory/index.js index 97242923..45805f2f 100644 --- a/lib/VersionHistory/index.js +++ b/lib/VersionHistory/index.js @@ -1,6 +1,7 @@ export * from './components'; export { getFieldLabels } from './getFieldLabels'; export { getHighlightedFields } from './getHighlightedFields'; +export { getVersionMetadata } from './getVersionMetadata'; export * from './hooks'; export { VersionCard } from './VersionCard'; export { VersionHistoryPane } from './VersionHistoryPane'; diff --git a/lib/hooks/useAddresses/useAddresses.js b/lib/hooks/useAddresses/useAddresses.js index a4704b1b..056979d8 100644 --- a/lib/hooks/useAddresses/useAddresses.js +++ b/lib/hooks/useAddresses/useAddresses.js @@ -22,7 +22,7 @@ export const useAddresses = (options = {}) => { ...queryOptions } = options; - const [namespace] = useNamespace({ key: 'acquisitions-units' }); + const [namespace] = useNamespace({ key: 'tenant-addresses' }); const ky = useOkapiKy({ tenant: tenantId }); const searchParams = { diff --git a/lib/utils/api/api.test.js b/lib/utils/api/api.test.js index 4dc5baad..552ad416 100644 --- a/lib/utils/api/api.test.js +++ b/lib/utils/api/api.test.js @@ -3,6 +3,7 @@ import { SEARCH_PARAMETER, } from '../../AcqList/constants'; import { + ACQUISITIONS_UNITS_API, LIMIT_MAX, LINES_API, ORDER_PIECES_API, @@ -11,6 +12,7 @@ import { VENDORS_API, } from '../../constants'; +import { fetchAcqUnitsByIds } from './fetchAcqUnitsByIds'; import { fetchOrderLines } from './fetchOrderLines'; import { fetchOrderLinesByIds } from './fetchOrderLinesByIds'; import { fetchOrders } from './fetchOrders'; @@ -110,4 +112,12 @@ describe('API utils', () => { expect(httpClient.get).toHaveBeenCalledWith(RECEIVING_TITLES_API, { searchParams }); }); }); + + describe('fetchAcqUnitsByIds', () => { + it('should fetch acquisitions units by ids', async () => { + await fetchAcqUnitsByIds(httpClient)(ids); + + expect(httpClient.get).toHaveBeenCalledWith(ACQUISITIONS_UNITS_API, { searchParams }); + }); + }); }); diff --git a/lib/utils/api/fetchAcqUnitsByIds.js b/lib/utils/api/fetchAcqUnitsByIds.js new file mode 100644 index 00000000..c36a5b4c --- /dev/null +++ b/lib/utils/api/fetchAcqUnitsByIds.js @@ -0,0 +1,11 @@ +import { ACQUISITIONS_UNITS_API } from '../../constants'; +import { fetchExportDataByIds } from '../fetchExportDataByIds'; + +export const fetchAcqUnitsByIds = (ky) => async (acquisitionUnitIds) => { + return fetchExportDataByIds({ + api: ACQUISITIONS_UNITS_API, + ids: acquisitionUnitIds, + ky, + records: 'acquisitionsUnits', + }); +}; diff --git a/lib/utils/api/index.js b/lib/utils/api/index.js index 959d968f..517fbc5c 100644 --- a/lib/utils/api/index.js +++ b/lib/utils/api/index.js @@ -1,3 +1,4 @@ +export { fetchAcqUnitsByIds } from './fetchAcqUnitsByIds'; export { fetchOrderLines } from './fetchOrderLines'; export { fetchOrderLinesByIds } from './fetchOrderLinesByIds'; export { fetchOrders } from './fetchOrders';