Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIDATIMP-1540: Fetch Job summary data with for a particular tenant id #1478

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './useLocationsQuery';
export * from './useOrderByIdQuery';
export * from './usePOLinesByIdQuery';
export * from './useSRSRecordQuery';
export * from './useTenantKy';
44 changes: 44 additions & 0 deletions src/hooks/tests/useTenantKy.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
13 changes: 6 additions & 7 deletions src/hooks/useAuthoritiesByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useInventoryHoldingsByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useInventoryInstancesByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useInventoryItemsByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useInvoiceLineByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useInvoicesByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useLocationsQuery.js
Original file line number Diff line number Diff line change
@@ -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(),
});

Expand Down
12 changes: 5 additions & 7 deletions src/hooks/useOrderByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/usePOLinesByIdQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
13 changes: 6 additions & 7 deletions src/hooks/useSRSRecordQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useTenantKy.js
Original file line number Diff line number Diff line change
@@ -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;
};
20 changes: 10 additions & 10 deletions src/routes/ViewJobLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,5 @@ export const STATUS_CODES = {
CONFLICT: 409,
INTERNAL_SERVER_ERROR: 500,
};

export const OKAPI_TENANT_HEADER = 'X-Okapi-Tenant';
Loading