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..07f76c5b5 --- /dev/null +++ b/src/hooks/tests/useTenantKy.test.js @@ -0,0 +1,44 @@ +import { renderHook } from '@folio/jest-config-stripes/testing-library/react'; + +import '../../../test/jest/__mock__'; + +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 tenant = 'college'; + const { result } = renderHook(() => useTenantKy({ tenant })); + + expect(result.current).toBe(kyMock); + expect(reqMock.headers.set).toHaveBeenCalledWith(OKAPI_TENANT_HEADER, tenant); + }); + + 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..6bd018cb0 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 = [], { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'authoritiesById' }); const queryIds = authoritiesIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenant], 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..623ae80ba 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 = [], { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'holdingsById' }); const queryIds = holdingsIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenant], 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..766df0075 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 = [], { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'instancesById' }); const queryIds = instancesIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenant], 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..7516bbb94 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 = [], { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'itemsById' }); const queryIds = itemsIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenant], 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..8788d9e1b 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, { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'invoiceLineById' }); return useQuery( { - queryKey: [namespace, invoiceLineId], + queryKey: [namespace, invoiceLineId, tenant], 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..3f8d1df9e 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 = [], { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'invoicesById' }); const queryIds = invoicesIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenant], 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..032922f75 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 = ({ tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'locations' }); const query = useQuery({ - queryKey: [namespace], + queryKey: [namespace, tenant], queryFn: () => ky.get('locations').json(), }); diff --git a/src/hooks/useOrderByIdQuery.js b/src/hooks/useOrderByIdQuery.js index c76455326..673e02165 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, { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'orderById' }); return useQuery( { - queryKey: [namespace, orderId], + queryKey: [namespace, orderId, tenant], queryFn: () => ky.get(`orders/composite-orders/${orderId}`).json(), enabled: !!orderId, } diff --git a/src/hooks/usePOLinesByIdQuery.js b/src/hooks/usePOLinesByIdQuery.js index 6f5bb204d..38bcbeb15 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 = [], { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'poLinesByIds' }); const queryIds = poLineIds.join(' or '); const query = useQuery( { - queryKey: [namespace, queryIds], + queryKey: [namespace, queryIds, tenant], 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..68658fc91 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 './useTenantKy'; + +export const useSRSRecordQuery = (recordId, { tenant } = {}) => { + const ky = useTenantKy({ tenant }); const [namespace] = useNamespace({ key: 'srsRecord' }); return useQuery( { - queryKey: [namespace, recordId], + queryKey: [namespace, recordId, tenant], 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..0d582877e --- /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 = ({ tenant } = {}) => { + const ky = useOkapiKy(); + + return tenant + ? ky.extend({ + hooks: { + beforeRequest: [ + request => { + request.headers.set(OKAPI_TENANT_HEADER, tenant); + }, + ], + }, + }) + : ky; +}; diff --git a/src/routes/ViewJobLog.js b/src/routes/ViewJobLog.js index 3d8dadc25..45d063ac1 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, { tenant: jobLogData?.sourceRecordTenantId }); + const { data: instancesData } = useInventoryInstancesByIdQuery(instancesIds, { tenant: jobLogData?.relatedInstanceInfo?.tenantId }); + const { data: holdingsData } = useInventoryHoldingsByIdQuery(holdingsIds, { tenant: jobLogData?.relatedHoldingsInfo?.tenantId }); + const { data: itemsData } = useInventoryItemsByIdQuery(itemsIds, { tenant: jobLogData?.relatedItemInfo?.tenantId }); + const { data: orderData } = useOrderByIdQuery(orderId, { tenant: jobLogData?.relatedPoLineInfo?.tenantId }); + const { data: poLinesData } = usePOLinesByIdQuery(poLinesIds, { tenant: jobLogData?.relatedPoLineInfo?.tenantId }); + const { data: invoicesData } = useInvoicesByIdQuery(invoicesIds, { tenant: jobLogData?.relatedInvoiceInfo?.tenantId }); + const { data: invoiceLineData } = useInvoiceLineByIdQuery(invoiceLineId, { tenant: jobLogData?.relatedInvoiceInfo?.tenantId }); + const { data: authoritiesData } = useAuthoritiesByIdQuery(authoritiesIds, { tenant: jobLogData?.relatedAuthorityInfo?.tenantId }); + const { data: locationsData = [] } = useLocationsQuery({ tenant: 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';