From 99aaaa4d8141ffd3b8851a20788f0051811f5338 Mon Sep 17 00:00:00 2001 From: Alisher Musurmonov Date: Thu, 21 Nov 2024 11:11:46 +0500 Subject: [PATCH] UISACQCOMP-232: Move reusable helper function to support version history functionality --- CHANGELOG.md | 1 + lib/VersionHistory/getVersionMetadata.js | 7 ++++ lib/VersionHistory/getVersionMetadata.test.js | 38 +++++++++++++++++++ lib/VersionHistory/index.js | 1 + lib/hooks/useAddresses/useAddresses.js | 2 +- lib/utils/getAcqUnitsByIds.js | 11 ++++++ lib/utils/index.js | 1 + 7 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/VersionHistory/getVersionMetadata.js create mode 100644 lib/VersionHistory/getVersionMetadata.test.js create mode 100644 lib/utils/getAcqUnitsByIds.js 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..6a6818ed --- /dev/null +++ b/lib/VersionHistory/getVersionMetadata.test.js @@ -0,0 +1,38 @@ +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: 'actionDate', + }); + }); + + it('should return empty object if entity is not provided', () => { + expect(getVersionMetadata(version)).toEqual({}); + }); + + it('should return empty object if version is not provided', () => { + const entity = { + metadata: { + metadataKey: 'metadataValue', + }, + }; + + expect(getVersionMetadata(null, entity)).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/getAcqUnitsByIds.js b/lib/utils/getAcqUnitsByIds.js new file mode 100644 index 00000000..bc8fd805 --- /dev/null +++ b/lib/utils/getAcqUnitsByIds.js @@ -0,0 +1,11 @@ +import { ACQUISITIONS_UNITS_API } from '../constants'; +import { fetchExportDataByIds } from './fetchExportDataByIds'; + +export const getAcqUnitsByIds = (ky) => async (acquisitionUnitIds) => { + return fetchExportDataByIds({ + api: ACQUISITIONS_UNITS_API, + ids: acquisitionUnitIds, + ky, + records: 'acquisitionsUnits', + }); +}; diff --git a/lib/utils/index.js b/lib/utils/index.js index b29a795f..b346a5f4 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -15,6 +15,7 @@ export * from './filterSelectValues'; export * from './formatDate'; export * from './formatDateTime'; export * from './generateQueryTemplate'; +export * from './getAcqUnitsByIds'; export * from './getAcqUnitsOptions'; export * from './getAddresses'; export * from './getAddressOptions';