Skip to content

Commit

Permalink
UIQM-568 Remove second call to links-suggestion endpoint. Now only ne…
Browse files Browse the repository at this point in the history
…ed to send one call to member tenant. (#606)

* UIQM-568 Remove second call to links-suggestion endpoint. Now only need to send one call to member tenant.

* UIQM-568 Remove not needed mapping of suggested fields
  • Loading branch information
BogdanDenis authored Oct 12, 2023
1 parent da099ba commit 5f784f3
Show file tree
Hide file tree
Showing 4 changed files with 806 additions and 1,971 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* [UIQM-562](https://issues.folio.org/browse/UIQM-562) Fix optimistic locking error doesn't appear when edit "MARC authority" record.
* [UIQM-564](https://issues.folio.org/browse/UIQM-564) Hide permission `quickMARC: Create a new MARC authority record
* [UIQM-431](https://issues.folio.org/browse/UIQM-431) Marc record fixed field 008 with proper order.
* [UIQM-568](https://issues.folio.org/browse/UIQM-568) Remove second call to links-suggestion endpoint. Now only need to send one call to member tenant.

## [6.0.2](https://github.com/folio-org/ui-quick-marc/tree/v6.0.2) (2023-03-30)

Expand Down
72 changes: 8 additions & 64 deletions src/hooks/useAuthorityLinking/useAuthorityLinking.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import get from 'lodash/get';
import omit from 'lodash/omit';
import pick from 'lodash/pick';

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

import {
useAuthoritySourceFiles,
Expand All @@ -30,12 +27,10 @@ import {
} from '../../QuickMarcEditor/utils';
import {
AUTOLINKING_STATUSES,
AUTOLINKING_ERROR_CODES,
UNCONTROLLED_ALPHA,
UNCONTROLLED_NUMBER,
QUICK_MARC_ACTIONS,
} from '../../QuickMarcEditor/constants';
import { MARC_TYPES } from '../../common/constants';

const joinSubfields = (subfields) => Object.keys(subfields).reduce((content, key) => {
const subfield = subfields[key].join(` ${key} `);
Expand All @@ -48,10 +43,8 @@ const formatSubfieldCode = (code) => { return code.startsWith('$') ? code : `$${
const useAuthorityLinking = ({ tenantId, marcType, action } = {}) => {
const stripes = useStripes();
const location = useLocation();
const { search } = location;

const centralTenantId = stripes.user.user.consortium?.centralTenantId;
const isMemberTenant = checkIfUserInMemberTenant(stripes);
const isCentralTenantInHeaders = applyCentralTenantInHeaders(location, stripes, marcType)
&& action === QUICK_MARC_ACTIONS.EDIT;

Expand All @@ -62,13 +55,9 @@ const useAuthorityLinking = ({ tenantId, marcType, action } = {}) => {
const { linkingRules } = useAuthorityLinkingRules({ tenantId: _tenantId });

const {
fetchLinkSuggestions: fetchMemberLinkSuggestions,
isLoading: isLoadingMemberLinkSuggestions,
fetchLinkSuggestions,
isLoading: isLoadingLinkSuggestions,
} = useLinkSuggestions({ tenantId: _tenantId });
const {
fetchLinkSuggestions: fetchCentralLinkSuggestions,
isLoading: isLoadingCentralLinkSuggestions,
} = useLinkSuggestions({ tenantId: centralTenantId });

const linkableBibFields = useMemo(() => linkingRules.map(rule => rule.bibField), [linkingRules]);
const autoLinkableBibFields = useMemo(() => {
Expand Down Expand Up @@ -221,61 +210,17 @@ const useAuthorityLinking = ({ tenantId, marcType, action } = {}) => {
}, [getSubfieldGroups]);

const getSuggestedFields = useCallback(async (formValues, fieldsToHydrate, extraRequestArgs = {}) => {
const searchParams = new URLSearchParams(search);
const isSharedRecord = searchParams.get('shared') === 'true';
const payload = hydrateForLinkSuggestions(formValues, fieldsToHydrate);

const requestArgs = {
body: payload,
...extraRequestArgs,
};

const isRecordWithCentralAndMemberSuggestions = (
marcType === MARC_TYPES.BIB
&& isMemberTenant
&& (!isSharedRecord || (isSharedRecord && action === QUICK_MARC_ACTIONS.DERIVE))
);

const memberLinkSuggestionsPromise = fetchMemberLinkSuggestions(requestArgs);
const centralLinkSuggestionsPromise = isRecordWithCentralAndMemberSuggestions
? fetchCentralLinkSuggestions(requestArgs)
: Promise.resolve({ fields: [] });

const [
{ fields: memberSuggestedFields },
{ fields: suggestedFieldsFromCentralTenant },
] = await Promise.all([
memberLinkSuggestionsPromise,
centralLinkSuggestionsPromise,
]);

return memberSuggestedFields.map((suggestedField, index) => {
if (suggestedField.linkDetails?.status !== AUTOLINKING_STATUSES.ERROR) {
return suggestedField;
}

const suggestedFieldFromCentralTenant = suggestedFieldsFromCentralTenant[index];

const shouldTakeSuggestedFieldFromCentralTenant = (
isRecordWithCentralAndMemberSuggestions
&& suggestedField.linkDetails.errorCause === AUTOLINKING_ERROR_CODES.AUTHORITY_NOT_FOUND
&& suggestedFieldFromCentralTenant?.linkDetails.status !== AUTOLINKING_STATUSES.ERROR
);
const { fields: memberSuggestedFields } = await fetchLinkSuggestions(requestArgs);

if (shouldTakeSuggestedFieldFromCentralTenant) {
return suggestedFieldFromCentralTenant;
}

return suggestedField;
});
}, [
fetchMemberLinkSuggestions,
fetchCentralLinkSuggestions,
search,
marcType,
isMemberTenant,
action,
]);
return memberSuggestedFields;
}, [fetchLinkSuggestions]);

const autoLinkAuthority = useCallback(async (formValues) => {
const fieldsToLink = formValues.records.filter(record => isRecordForAutoLinking(record, autoLinkableBibFields));
Expand Down Expand Up @@ -401,9 +346,8 @@ const useAuthorityLinking = ({ tenantId, marcType, action } = {}) => {
autoLinkAuthority,
actualizeLinks,
linkingRules,
fetchMemberLinkSuggestions,
fetchCentralLinkSuggestions,
isLoadingLinkSuggestions: isLoadingMemberLinkSuggestions || isLoadingCentralLinkSuggestions,
fetchLinkSuggestions,
isLoadingLinkSuggestions,
};
};

Expand Down
Loading

0 comments on commit 5f784f3

Please sign in to comment.