Skip to content

Commit

Permalink
UIQM-588 Use Authority search to get Authority linked records count. (#…
Browse files Browse the repository at this point in the history
…625)

* UIQM-588 Use Authority search to get Authority linked records count.

* UIQM-588 Updated tests

* UIQM-588 Added search interface

* Update CHANGELOG.md
  • Loading branch information
BogdanDenis authored Dec 7, 2023
1 parent adddfc1 commit 1f0140f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [UIQM-594](https://issues.folio.org/browse/UIQM-594) *BREAKING* Add authority-source-files interface.
* [UIQM-522](https://issues.folio.org/browse/UIQM-522) Create/Derive a new MARC bib record & Create a MARC holdings | Default state of Save & close button should be disabled.
* [UIQM-534](https://issues.folio.org/browse/UIQM-534) Remove fields that have no MARC tag and no subfield value.
* [UIQM-588](https://issues.folio.org/browse/UIQM-588) *BREAKING* Use Authority search to get Authority linked records count.

## [7.0.4](https://github.com/folio-org/ui-quick-marc/tree/v7.0.4) (2023-11-09)

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"inventory": "13.0",
"marc-records-editor": "5.0",
"permissions": "5.6",
"search": "1.2",
"marc-specifications": "1.0",
"authority-source-files": "2.0",
"instance-authority-links": "2.1"
Expand Down Expand Up @@ -126,7 +127,8 @@
"marc-records-editor.item.get",
"marc-records-editor.item.put",
"inventory-storage.locations.collection.get",
"marc-specifications.item.get"
"marc-specifications.item.get",
"search.authorities.collection.get"
],
"visible": true
},
Expand Down
14 changes: 1 addition & 13 deletions src/QuickMarcEditor/QuickMarcEditorContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ const QuickMarcEditorContainer = ({
const [marcRecord, setMarcRecord] = useState();
const [locations, setLocations] = useState();
const [isLoading, setIsLoading] = useState(true);
const [linksCount, setLinksCount] = useState(0);
const [fixedFieldSpec, setFixedFieldSpec] = useState();

const searchParams = new URLSearchParams(location.search);
Expand All @@ -96,7 +95,7 @@ const QuickMarcEditorContainer = ({
&& action !== QUICK_MARC_ACTIONS.CREATE;

const showCallout = useShowCallout();
const { fetchLinksCount } = useAuthorityLinksCount();
const { linksCount } = useAuthorityLinksCount({ id: marcType === MARC_TYPES.AUTHORITY && externalId });

const closeEditor = useCallback((id) => {
if (marcType === MARC_TYPES.HOLDINGS && action !== QUICK_MARC_ACTIONS.CREATE) {
Expand Down Expand Up @@ -147,10 +146,6 @@ const QuickMarcEditorContainer = ({
// must be with the central tenant id when user derives shared record
const linkingRulesPromise = mutator.linkingRules.GET(headers);

const linksCountPromise = marcType === MARC_TYPES.AUTHORITY
? fetchLinksCount([externalId])
: Promise.resolve();

const fixedFieldSpecPromise = mutator.fixedFieldSpec.GET({
path: `${MARC_SPEC_API}/${marcType}/008`,
...headers,
Expand All @@ -160,22 +155,16 @@ const QuickMarcEditorContainer = ({
instancePromise,
marcRecordPromise,
locationsPromise,
linksCountPromise,
linkingRulesPromise,
fixedFieldSpecPromise,
])
.then(([
instanceResponse,
marcRecordResponse,
locationsResponse,
linksCountResponse,
linkingRulesResponse,
fixedFieldSpecResponse,
]) => {
if (marcType === MARC_TYPES.AUTHORITY) {
setLinksCount(linksCountResponse.links[0].totalLinks);
}

if (action !== QUICK_MARC_ACTIONS.CREATE) {
searchParams.set('relatedRecordVersion', instanceResponse._version);

Expand Down Expand Up @@ -211,7 +200,6 @@ const QuickMarcEditorContainer = ({
externalId,
history,
marcType,
fetchLinksCount,
centralTenantId,
token,
locale,
Expand Down
8 changes: 1 addition & 7 deletions src/QuickMarcEditor/QuickMarcEditorContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import buildStripes from '../../test/jest/__mock__/stripesCore.mock';
import { useAuthorityLinksCount } from '../queries';
import { applyCentralTenantInHeaders } from './utils';

const mockFetchLinksCount = jest.fn().mockResolvedValue();

const match = {
path: '/marc-authorities/quick-marc/edit-authority/:externalId',
url: '/marc-authorities/quick-marc/edit-authority/external-id',
Expand Down Expand Up @@ -160,10 +158,6 @@ describe('Given Quick Marc Editor Container', () => {

describe('when the marc type is authority', () => {
it('should make a request to get the number of links', async () => {
useAuthorityLinksCount.mockClear().mockReturnValue({
fetchLinksCount: mockFetchLinksCount,
});

await act(async () => {
await renderQuickMarcEditorContainer({
mutator,
Expand All @@ -174,7 +168,7 @@ describe('Given Quick Marc Editor Container', () => {
});
});

expect(mockFetchLinksCount).toHaveBeenCalledWith([match.params.externalId]);
expect(useAuthorityLinksCount).toHaveBeenCalledWith({ id: match.params.externalId });
});
});

Expand Down
30 changes: 22 additions & 8 deletions src/queries/useAuthorityLinksCount/useAuthorityLinksCount.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { useMutation } from 'react-query';
import { useTenantKy } from '../../temp';
import { useQuery } from 'react-query';

const useAuthorityLinksCount = ({ tenantId } = {}) => {
const ky = useTenantKy({ tenantId });
import {
useNamespace,
useOkapiKy,
} from '@folio/stripes/core';

const { mutateAsync, isLoading } = useMutation({
mutationFn: ids => ky.post('links/authorities/bulk/count', { json: { ids } }).json(),
});
const useAuthorityLinksCount = ({ id } = {}) => {
const ky = useOkapiKy();
const [namespace] = useNamespace();

const searchParams = {
query: `(id==${id} and authRefType==("Authorized"))`, // only Authorized records have links counts, so we need to get them
};

const { data = {}, isLoading } = useQuery(
[namespace, 'authority', id],
() => ky.get('search/authorities', { searchParams }).json(),
{
keepPreviousData: true,
enabled: Boolean(id),
},
);

return {
fetchLinksCount: mutateAsync,
linksCount: data.authorities?.[0]?.numberOfTitles || 0,
isLoading,
};
};
Expand Down
29 changes: 16 additions & 13 deletions src/queries/useAuthorityLinksCount/useAuthorityLinksCount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import {
} from 'react-query';
import { renderHook } from '@folio/jest-config-stripes/testing-library/react';

import '../../../test/jest/__mock__';

import { useOkapiKy } from '@folio/stripes/core';

import useAuthorityLinksCount from './useAuthorityLinksCount';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(),
}));
import '../../../test/jest/__mock__';

const queryClient = new QueryClient();

Expand All @@ -23,22 +18,30 @@ const wrapper = ({ children }) => (
</QueryClientProvider>
);

const mockPost = jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ links: [] }),
const mockGet = jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ authorities: [{ numberOfTitles: 2 }] }),
});

describe('Given useAuthorityLinksCount', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
post: mockPost,
get: mockGet,
});
});

it('should fetch links count', async () => {
const { result } = renderHook(() => useAuthorityLinksCount(), { wrapper });
it('should fetch authorities', async () => {
renderHook(() => useAuthorityLinksCount({ id: 'fakeId' }), { wrapper });

expect(mockGet).toHaveBeenCalledWith('search/authorities', {
searchParams: {
query: '(id==fakeId and authRefType==("Authorized"))',
},
});
});

await result.current.fetchLinksCount(['fakeId']);
it('should return links count', async () => {
const { result } = renderHook(() => useAuthorityLinksCount({ id: 'fakeId' }), { wrapper });

expect(mockPost).toHaveBeenCalledWith('links/authorities/bulk/count', { json: { ids: ['fakeId'] } });
expect(result.current.linksCount).toEqual(2);
});
});

0 comments on commit 1f0140f

Please sign in to comment.