Skip to content

Commit

Permalink
UIDATIMP-1540: Fetch Job summary data with for a particular tenant id
Browse files Browse the repository at this point in the history
  • Loading branch information
mariia-aloshyna committed Oct 11, 2023
1 parent c672199 commit 12dfbb4
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 80 deletions.
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';
42 changes: 42 additions & 0 deletions src/hooks/tests/useTenantKy.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
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 = [], { 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,
}
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 = [], { 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,
}
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 = [], { 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,
}
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 = [], { 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,
}
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, { 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,
}
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 = [], { 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,
}
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 = ({ tenantId } = {}) => {
const ky = useTenantKy({ tenantId });
const [namespace] = useNamespace({ key: 'locations' });

const query = useQuery({
queryKey: [namespace],
queryKey: [namespace, tenantId],
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, { 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,
}
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 = [], { 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,
}
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 '../hooks';

Check failure on line 5 in src/hooks/useSRSRecordQuery.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Useless path segments for "../hooks", should be "./"

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,
}
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 = ({ tenantId } = {}) => {
const ky = useOkapiKy();

return tenantId
? ky.extend({
hooks: {
beforeRequest: [
request => {
request.headers.set(OKAPI_TENANT_HEADER, tenantId);
},
],
},
})
: 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, { tenantId: jobLogData?.sourceRecordTenantId});

Check failure on line 61 in src/routes/ViewJobLog.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

A space is required before '}'
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) {
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';

0 comments on commit 12dfbb4

Please sign in to comment.