Skip to content

Commit

Permalink
UIIN-2818: Populate Acquisitions accordion on item when central order…
Browse files Browse the repository at this point in the history
…ing is active (#2510)

* Make sourcce record as shared when instance has been shared

* UIIN-2818: Use central tenant id to fetch piece from the central order

* Update ViewSource.js
  • Loading branch information
OleksandrHladchenko1 committed Jun 17, 2024
1 parent ead590a commit 483cb8c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* Edit Inventory Items: Display a Save & keep editing button. Refs UIIN-2456.
* Prevent users from editing tags when Instance/Holdings/Item is updated. Fixes UIIN-2941.
* Instance record > Classification accordion > Display a clipboard icon next to classification number. Refs UIIN-2580.
* Populate Acquisitions accordion on item when central ordering is active. Refs UIIN-2818.

## [11.0.4](https://github.com/folio-org/ui-inventory/tree/v11.0.4) (2024-04-30)
[Full Changelog](https://github.com/folio-org/ui-inventory/compare/v11.0.3...v11.0.4)
Expand Down
117 changes: 71 additions & 46 deletions src/Item/ViewItem/ItemAcquisition/useItemAcquisition.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,83 @@
import {
useState,
useEffect,
} from 'react';
import { useQuery } from 'react-query';
import isEmpty from 'lodash/isEmpty';

import { useOkapiKy, useNamespace, useStripes } from '@folio/stripes/core';
import {
useOkapiKy,
useNamespace,
useStripes,
checkIfUserInMemberTenant,
} from '@folio/stripes/core';

const useItemAcquisition = (id) => {
const ky = useOkapiKy();
const namespace = useNamespace();
const stripes = useStripes();

const centralTenant = stripes.user.user?.consortium?.centralTenantId;

const [activeTenant, setActiveTenant] = useState(stripes.okapi.tenant);

const ky = useOkapiKy({ tenant: activeTenant });
const namespace = useNamespace();

const fetchItemAcquisition = async () => {
const { pieces = [] } = await ky.get('orders/pieces', {
searchParams: { query: `itemId=${id}` },
}).json();

if (!pieces.length) return {};

const [piece] = pieces;
let orderLine;
let order;
let vendor;

try {
orderLine = await ky.get(`orders/order-lines/${piece.poLineId}`).json();
} catch {
orderLine = {};
}

try {
order = await ky.get(`orders/composite-orders/${orderLine.purchaseOrderId}`).json();
} catch {
order = {};
}

try {
vendor = await ky.get(`organizations/organizations/${order.vendor}`).json();
} catch {
vendor = {};
}

return {
order,
orderLine,
piece,
vendor,
};
};

const { data = {}, isLoading } = useQuery(
[namespace, 'item-acquisition', id],
async () => {
const { pieces = [] } = await ky.get('orders/pieces', {
searchParams: { query: `itemId=${id}` },
}).json();

if (!pieces.length) return {};

const [piece] = pieces;
let orderLine;
let order;
let vendor;

try {
orderLine = await ky.get(`orders/order-lines/${piece.poLineId}`).json();
} catch {
orderLine = {};
}

try {
order = await ky.get(`orders/composite-orders/${orderLine.purchaseOrderId}`).json();
} catch {
order = {};
}

try {
vendor = await ky.get(`organizations/organizations/${order.vendor}`).json();
} catch {
vendor = {};
}

return {
order,
orderLine,
piece,
vendor,
};
},
{ enabled: stripes.hasInterface('pieces') &&
stripes.hasInterface('order-lines') &&
stripes.hasInterface('orders') &&
stripes.hasInterface('organizations.organizations') &&
Boolean(id) },
[namespace, 'item-acquisition', id, activeTenant],
fetchItemAcquisition,
{
enabled: stripes.hasInterface('pieces') &&
stripes.hasInterface('order-lines') &&
stripes.hasInterface('orders') &&
stripes.hasInterface('organizations.organizations') &&
Boolean(id),
}
);

useEffect(() => {
if (!isLoading && isEmpty(data) && checkIfUserInMemberTenant(stripes)) {
setActiveTenant(centralTenant);
}
}, [isLoading, data]);

return {
isLoading,
itemAcquisition: data,
Expand Down
46 changes: 37 additions & 9 deletions src/Item/ViewItem/ItemAcquisition/useItemAcquisition.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import React from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';
import { renderHook, act } from '@folio/jest-config-stripes/testing-library/react';

import '../../../../test/jest/__mock__';
import React, { act } from 'react';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';

import { renderHook } from '@folio/jest-config-stripes/testing-library/react';
import { useOkapiKy } from '@folio/stripes/core';

import { order, orderLine, vendor, piece, resultData } from './fixtures';
import {
order,
orderLine,
vendor,
piece,
resultData,
} from './fixtures';

import useItemAcquisition from './useItemAcquisition';

const queryClient = new QueryClient();
Expand All @@ -25,17 +33,15 @@ const wrapper = ({ children }) => (
);

describe('useItemAcquisition', () => {
beforeEach(() => {
it('should fetch item acquisition data', async () => {
useOkapiKy
.mockClear()
.mockReturnValue({
get: (path) => ({
json: () => kyResponseMap[path],
}),
});
});

it('should fetch item acquisition data', async () => {
const { result } = renderHook(() => useItemAcquisition('itemId'), { wrapper });

await act(() => {
Expand All @@ -44,4 +50,26 @@ describe('useItemAcquisition', () => {

expect(result.current.itemAcquisition).toEqual(resultData);
});

describe('when acquisition data is empty', () => {
describe('and user is non-central tenant', () => {
it('should fetch acquisition data with central tenant id', async () => {
useOkapiKy
.mockClear()
.mockReturnValue({
get: () => ({
json: () => ({}),
}),
});

const { result } = renderHook(() => useItemAcquisition('itemId'), { wrapper });

await act(() => {
return !result.current.isLoading;
});

expect(useOkapiKy).toHaveBeenCalledWith({ tenant: 'consortia' });
});
});
});
});

0 comments on commit 483cb8c

Please sign in to comment.