From 12dfbb471dc740dc000ebee0a921a61a4a05635e Mon Sep 17 00:00:00 2001 From: Mariia_Aloshyna Date: Wed, 11 Oct 2023 16:25:45 +0300 Subject: [PATCH] UIDATIMP-1540: Fetch Job summary data with for a particular tenant id --- CHANGELOG.md | 1 + src/hooks/index.js | 1 + src/hooks/tests/useTenantKy.test.js | 42 +++++++++++++++++++++ src/hooks/useAuthoritiesByIdQuery.js | 13 +++---- src/hooks/useInventoryHoldingsByIdQuery.js | 13 +++---- src/hooks/useInventoryInstancesByIdQuery.js | 13 +++---- src/hooks/useInventoryItemsByIdQuery.js | 13 +++---- src/hooks/useInvoiceLineByIdQuery.js | 13 +++---- src/hooks/useInvoicesByIdQuery.js | 13 +++---- src/hooks/useLocationsQuery.js | 13 +++---- src/hooks/useOrderByIdQuery.js | 12 +++--- src/hooks/usePOLinesByIdQuery.js | 13 +++---- src/hooks/useSRSRecordQuery.js | 13 +++---- src/hooks/useTenantKy.js | 19 ++++++++++ src/routes/ViewJobLog.js | 20 +++++----- src/utils/constants.js | 2 + 16 files changed, 134 insertions(+), 80 deletions(-) create mode 100644 src/hooks/tests/useTenantKy.test.js create mode 100644 src/hooks/useTenantKy.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 96d6f9c3f..eff3b9121 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ * leverage jest-config-stripes for all jest and testing-library packages (UIDATIMP-1508) * *BREAKING* bump `react-intl` to `v6.4.4` (UIDATIMP-1520) * Bump the major versions of @folio/plugin-find-organization optionalDependencies (UIDATIMP-1532) +* Fetch Job summary data with for a particular tenant id (UIDATIMP-1540) ### Bugs fixed: * Fix all the failed accessibility tests in ui-data-import (UIDATIMP-1393) diff --git a/src/hooks/index.js b/src/hooks/index.js index 8fc5c1650..4fa12a4c1 100644 --- a/src/hooks/index.js +++ b/src/hooks/index.js @@ -9,3 +9,4 @@ export * from './useLocationsQuery'; export * from './useOrderByIdQuery'; export * from './usePOLinesByIdQuery'; export * from './useSRSRecordQuery'; +export * from './useTenantKy'; diff --git a/src/hooks/tests/useTenantKy.test.js b/src/hooks/tests/useTenantKy.test.js new file mode 100644 index 000000000..ecd716f0c --- /dev/null +++ b/src/hooks/tests/useTenantKy.test.js @@ -0,0 +1,42 @@ +import { renderHook } from '@folio/jest-config-stripes/testing-library/react'; + +import { useOkapiKy } from '@folio/stripes/core'; + +import { useTenantKy } from '../useTenantKy'; +import { OKAPI_TENANT_HEADER } from '../../utils'; + +const reqMock = { + headers: { + set: jest.fn(), + }, +}; +const kyMock = { + extend: jest.fn(({ hooks: { beforeRequest } }) => { + beforeRequest[0](reqMock); + + return kyMock; + }), +}; + +describe('useTenantKy', () => { + beforeEach(() => { + reqMock.headers.set.mockClear(); + kyMock.extend.mockClear(); + useOkapiKy.mockClear().mockReturnValue(kyMock); + }); + + it('should set provided okapi tenant header and return \'ky\' client', async () => { + const tenantId = 'college'; + const { result } = renderHook(() => useTenantKy({ tenantId })); + + expect(result.current).toBe(kyMock); + expect(reqMock.headers.set).toHaveBeenCalledWith(OKAPI_TENANT_HEADER, tenantId); + }); + + it('should use current tenant in the headers if there is no provided tenant ID', async () => { + const { result } = renderHook(() => useTenantKy()); + + expect(result.current).toBe(kyMock); + expect(reqMock.headers.set).not.toHaveBeenCalled(); + }); +}); diff --git a/src/hooks/useAuthoritiesByIdQuery.js b/src/hooks/useAuthoritiesByIdQuery.js index 10a1ec253..cd64cae77 100644 --- a/src/hooks/useAuthoritiesByIdQuery.js +++ b/src/hooks/useAuthoritiesByIdQuery.js @@ -1,19 +1,18 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useAuthoritiesByIdQuery = (authoritiesIds = []) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useAuthoritiesByIdQuery = (authoritiesIds = [], { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'authoritiesById' }); const queryIds = authoritiesIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenantId], queryFn: () => ky.get(`authority-storage/authorities?query=id==(${queryIds})`).json(), enabled: !!queryIds, } diff --git a/src/hooks/useInventoryHoldingsByIdQuery.js b/src/hooks/useInventoryHoldingsByIdQuery.js index ed3d6a767..2557cbf60 100644 --- a/src/hooks/useInventoryHoldingsByIdQuery.js +++ b/src/hooks/useInventoryHoldingsByIdQuery.js @@ -1,19 +1,18 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useInventoryHoldingsByIdQuery = (holdingsIds = []) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useInventoryHoldingsByIdQuery = (holdingsIds = [], { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'holdingsById' }); const queryIds = holdingsIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenantId], queryFn: () => ky.get(`holdings-storage/holdings?query=id==(${queryIds})`).json(), enabled: !!queryIds, } diff --git a/src/hooks/useInventoryInstancesByIdQuery.js b/src/hooks/useInventoryInstancesByIdQuery.js index ce5baba37..691b03f74 100644 --- a/src/hooks/useInventoryInstancesByIdQuery.js +++ b/src/hooks/useInventoryInstancesByIdQuery.js @@ -1,19 +1,18 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useInventoryInstancesByIdQuery = (instancesIds = []) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useInventoryInstancesByIdQuery = (instancesIds = [], { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'instancesById' }); const queryIds = instancesIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenantId], queryFn: () => ky.get(`inventory/instances?query=id==(${queryIds})`).json(), enabled: !!queryIds, } diff --git a/src/hooks/useInventoryItemsByIdQuery.js b/src/hooks/useInventoryItemsByIdQuery.js index e19f57063..1393d0a00 100644 --- a/src/hooks/useInventoryItemsByIdQuery.js +++ b/src/hooks/useInventoryItemsByIdQuery.js @@ -1,19 +1,18 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useInventoryItemsByIdQuery = (itemsIds = []) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useInventoryItemsByIdQuery = (itemsIds = [], { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'itemsById' }); const queryIds = itemsIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenantId], queryFn: () => ky.get(`inventory/items?query=id==(${queryIds})`).json(), enabled: !!queryIds, } diff --git a/src/hooks/useInvoiceLineByIdQuery.js b/src/hooks/useInvoiceLineByIdQuery.js index 297c87ace..0112abc80 100644 --- a/src/hooks/useInvoiceLineByIdQuery.js +++ b/src/hooks/useInvoiceLineByIdQuery.js @@ -1,17 +1,16 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useInvoiceLineByIdQuery = (invoiceLineId = null) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useInvoiceLineByIdQuery = (invoiceLineId = null, { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'invoiceLineById' }); return useQuery( { - queryKey: [namespace, invoiceLineId], + queryKey: [namespace, invoiceLineId, tenantId], queryFn: () => ky.get(`invoice-storage/invoice-lines/${invoiceLineId}`).json(), enabled: !!invoiceLineId, } diff --git a/src/hooks/useInvoicesByIdQuery.js b/src/hooks/useInvoicesByIdQuery.js index 65e07b4f0..b883d4098 100644 --- a/src/hooks/useInvoicesByIdQuery.js +++ b/src/hooks/useInvoicesByIdQuery.js @@ -1,19 +1,18 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useInvoicesByIdQuery = (invoicesIds = []) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useInvoicesByIdQuery = (invoicesIds = [], { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'invoicesById' }); const queryIds = invoicesIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenantId], queryFn: () => ky.get(`invoice-storage/invoices?query=id==(${queryIds})`).json(), enabled: !!queryIds, } diff --git a/src/hooks/useLocationsQuery.js b/src/hooks/useLocationsQuery.js index d4bc5e508..4ec5a7c15 100644 --- a/src/hooks/useLocationsQuery.js +++ b/src/hooks/useLocationsQuery.js @@ -1,16 +1,15 @@ import { useQuery } from 'react-query'; -import { - useOkapiKy, - useNamespace, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useLocationsQuery = () => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const useLocationsQuery = ({ tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'locations' }); const query = useQuery({ - queryKey: [namespace], + queryKey: [namespace, tenantId], queryFn: () => ky.get('locations').json(), }); diff --git a/src/hooks/useOrderByIdQuery.js b/src/hooks/useOrderByIdQuery.js index c76455326..54ee66466 100644 --- a/src/hooks/useOrderByIdQuery.js +++ b/src/hooks/useOrderByIdQuery.js @@ -1,17 +1,15 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; +import { useTenantKy } from './useTenantKy'; -export const useOrderByIdQuery = (orderId = null) => { - const ky = useOkapiKy(); +export const useOrderByIdQuery = (orderId = null, { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'orderById' }); return useQuery( { - queryKey: [namespace, orderId], + queryKey: [namespace, orderId, tenantId], queryFn: () => ky.get(`orders/composite-orders/${orderId}`).json(), enabled: !!orderId, } diff --git a/src/hooks/usePOLinesByIdQuery.js b/src/hooks/usePOLinesByIdQuery.js index 6f5bb204d..2fb0489f5 100644 --- a/src/hooks/usePOLinesByIdQuery.js +++ b/src/hooks/usePOLinesByIdQuery.js @@ -1,19 +1,18 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const usePOLinesByIdQuery = (poLineIds = []) => { - const ky = useOkapiKy(); +import { useTenantKy } from './useTenantKy'; + +export const usePOLinesByIdQuery = (poLineIds = [], { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'poLinesByIds' }); const queryIds = poLineIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenantId], queryFn: () => ky.get(`orders/order-lines?query=id==(${queryIds})`).json(), enabled: !!queryIds, } diff --git a/src/hooks/useSRSRecordQuery.js b/src/hooks/useSRSRecordQuery.js index 6d139fa99..a42f2f080 100644 --- a/src/hooks/useSRSRecordQuery.js +++ b/src/hooks/useSRSRecordQuery.js @@ -1,17 +1,16 @@ import { useQuery } from 'react-query'; -import { - useNamespace, - useOkapiKy, -} from '@folio/stripes/core'; +import { useNamespace } from '@folio/stripes/core'; -export const useSRSRecordQuery = recordId => { - const ky = useOkapiKy(); +import { useTenantKy } from '../hooks'; + +export const useSRSRecordQuery = (recordId, { tenantId } = {}) => { + const ky = useTenantKy({ tenantId }); const [namespace] = useNamespace({ key: 'srsRecord' }); return useQuery( { - queryKey: [namespace, recordId], + queryKey: [namespace, recordId, tenantId], queryFn: () => ky.get(`source-storage/records/${recordId}`).json(), enabled: !!recordId, } diff --git a/src/hooks/useTenantKy.js b/src/hooks/useTenantKy.js new file mode 100644 index 000000000..d1154d635 --- /dev/null +++ b/src/hooks/useTenantKy.js @@ -0,0 +1,19 @@ +import { useOkapiKy } from '@folio/stripes/core'; + +import { OKAPI_TENANT_HEADER } from '../utils'; + +export const useTenantKy = ({ tenantId } = {}) => { + const ky = useOkapiKy(); + + return tenantId + ? ky.extend({ + hooks: { + beforeRequest: [ + request => { + request.headers.set(OKAPI_TENANT_HEADER, tenantId); + }, + ], + }, + }) + : ky; +}; diff --git a/src/routes/ViewJobLog.js b/src/routes/ViewJobLog.js index 3d8dadc25..4ef4bec21 100644 --- a/src/routes/ViewJobLog.js +++ b/src/routes/ViewJobLog.js @@ -58,16 +58,16 @@ export const ViewJobLog = () => { isLoading: isJobLogLoading, data: jobLogData = {}, } = useJobLogRecordsQuery(logId, instanceLineIdParam || recordId); - const { data: srsRecordData } = useSRSRecordQuery(recordId); - const { data: instancesData } = useInventoryInstancesByIdQuery(instancesIds); - const { data: holdingsData } = useInventoryHoldingsByIdQuery(holdingsIds); - const { data: itemsData } = useInventoryItemsByIdQuery(itemsIds); - const { data: orderData } = useOrderByIdQuery(orderId); - const { data: poLinesData } = usePOLinesByIdQuery(poLinesIds); - const { data: invoicesData } = useInvoicesByIdQuery(invoicesIds); - const { data: invoiceLineData } = useInvoiceLineByIdQuery(invoiceLineId); - const { data: authoritiesData } = useAuthoritiesByIdQuery(authoritiesIds); - const { data: locationsData = [] } = useLocationsQuery(); + const { data: srsRecordData } = useSRSRecordQuery(recordId, { tenantId: jobLogData?.sourceRecordTenantId}); + const { data: instancesData } = useInventoryInstancesByIdQuery(instancesIds, { tenantId: jobLogData?.relatedInstanceInfo?.tenantId }); + const { data: holdingsData } = useInventoryHoldingsByIdQuery(holdingsIds, { tenantId: jobLogData?.relatedHoldingsInfo?.tenantId }); + const { data: itemsData } = useInventoryItemsByIdQuery(itemsIds, { tenantId: jobLogData?.relatedItemInfo?.tenantId }); + const { data: orderData } = useOrderByIdQuery(orderId, { tenantId: jobLogData?.relatedPoLineInfo?.tenantId }); + const { data: poLinesData } = usePOLinesByIdQuery(poLinesIds, { tenantId: jobLogData?.relatedPoLineInfo?.tenantId }); + const { data: invoicesData } = useInvoicesByIdQuery(invoicesIds, { tenantId: jobLogData?.relatedInvoiceInfo?.tenantId }); + const { data: invoiceLineData } = useInvoiceLineByIdQuery(invoiceLineId, { tenantId: jobLogData?.relatedInvoiceInfo?.tenantId }); + const { data: authoritiesData } = useAuthoritiesByIdQuery(authoritiesIds, { tenantId: jobLogData?.relatedAuthorityInfo?.tenantId }); + const { data: locationsData = [] } = useLocationsQuery({ tenantId: jobLogData?.relatedHoldingsInfo?.tenantId }); useEffect(() => { if (!isJobLogLoading && !isJobLogError) { diff --git a/src/utils/constants.js b/src/utils/constants.js index fb09573f3..9a4e6c67f 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -868,3 +868,5 @@ export const STATUS_CODES = { CONFLICT: 409, INTERNAL_SERVER_ERROR: 500, }; + +export const OKAPI_TENANT_HEADER = 'X-Okapi-Tenant';