Skip to content

Commit

Permalink
UIQM-649 allow deletion of 010 for not linked records (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanDenis authored and Dmytro-Melnyshyn committed Apr 18, 2024
1 parent 4fbcb05 commit c194430
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/QuickMarcEditor/QuickMarcEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const QuickMarcEditor = ({

if (
is010$aUpdated(initialValues.records, records) &&
is010LinkedToBibRecord(initialValues.records, instance.naturalId)
is010LinkedToBibRecord(initialValues.records, instance.naturalId, linksCount)
) {
setIsUpdate0101xxfieldsAuthRecModalOpen(true);

Expand Down
8 changes: 6 additions & 2 deletions src/QuickMarcEditor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export const getContentSubfieldValue = (content = '') => {
}, {});
};

export const is010LinkedToBibRecord = (initialRecords, naturalId) => {
export const is010LinkedToBibRecord = (initialRecords, naturalId, linksCount) => {
if (!linksCount) {
return false;
}

const initial010Field = initialRecords.find(record => record.tag === '010');

if (!initial010Field) {
Expand Down Expand Up @@ -1074,7 +1078,7 @@ export const hasDeleteException = (recordRow, marcType = MARC_TYPES.BIB, authori
if (marcType === MARC_TYPES.AUTHORITY) {
if (
is1XXField(recordRow.tag) ||
(recordRow.tag === '010' && linksCount && is010LinkedToBibRecord(initialValues.records, authority.naturalId))
(recordRow.tag === '010' && is010LinkedToBibRecord(initialValues.records, authority.naturalId, linksCount))
) {
return true;
}
Expand Down
54 changes: 54 additions & 0 deletions src/QuickMarcEditor/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1563,4 +1563,58 @@ describe('QuickMarcEditor utils', () => {
});
});
});

describe('is010LinkedToBibRecord', () => {
describe('when links count is 0', () => {
it('should return false', () => {
const initialRecords = [{
tag: '010',
content: '$a n 123456',
}];
const naturalId = 'n123456';
const linksCount = 0;

expect(utils.is010LinkedToBibRecord(initialRecords, naturalId, linksCount)).toEqual(false);
});
});

describe('when 010 field is missing', () => {
it('should return false', () => {
const initialRecords = [{
tag: '011',
content: '$a n 123456',
}];
const naturalId = 'n123456';
const linksCount = 1;

expect(utils.is010LinkedToBibRecord(initialRecords, naturalId, linksCount)).toEqual(false);
});
});

describe('when 010$a does not match naturalId', () => {
it('should return false', () => {
const initialRecords = [{
tag: '010',
content: '$a n 123456',
}];
const naturalId = 'n123456789';
const linksCount = 1;

expect(utils.is010LinkedToBibRecord(initialRecords, naturalId, linksCount)).toEqual(false);
});
});

describe('when 010$a matches naturalId', () => {
it('should return true', () => {
const initialRecords = [{
tag: '010',
content: '$a n 123456',
}];
const naturalId = 'n123456';
const linksCount = 1;

expect(utils.is010LinkedToBibRecord(initialRecords, naturalId, linksCount)).toEqual(true);
});
});
});
});
8 changes: 6 additions & 2 deletions src/hooks/useValidation/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,17 @@ const BASE_AUTHORITY_VALIDATORS = [
{
tag: '010',
subfield: '$a',
ignore: ({ initialValues, naturalId }) => !is010LinkedToBibRecord(initialValues.records, naturalId),
ignore: ({ linksCount, initialValues, naturalId }) => {
return !is010LinkedToBibRecord(initialValues.records, naturalId, linksCount);
},
validator: RULES.SUBFIELD_VALUE_EXISTS,
message: () => <FormattedMessage id="ui-quick-marc.record.error.010.$aRemoved" />,
},
{
tag: '010',
ignore: ({ initialValues, naturalId }) => !is010LinkedToBibRecord(initialValues.records, naturalId),
ignore: ({ linksCount, initialValues, naturalId }) => {
return !is010LinkedToBibRecord(initialValues.records, naturalId, linksCount);
},
validator: RULES.EXISTS,
message: () => <FormattedMessage id="ui-quick-marc.record.error.010.removed" />,
},
Expand Down
64 changes: 64 additions & 0 deletions src/hooks/useValidation/useValidation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,70 @@ describe('useValidation', () => {
});
});
});

describe('when authority is not linked to bib record', () => {
describe('when 010 $a was removed', () => {
it('should not return an error message', () => {
const _initialValues = {
leader: initialValues.leader,
records: [
{
id: 1,
content: initialValues.leader,
tag: 'LDR',
},
{
id: 2,
content: {},
tag: '008',
},
{
id: 3,
tag: '110',
content: '$a Record title',
},
{
id: 4,
tag: '010',
content: '$a n123456',
},
],
};
const { result } = renderHook(() => useValidation({
...marcContext,
linksCount: 0,
initialValues: _initialValues,
}));

const record = {
...initialValues,
records: [
{
id: 'LDR',
content: initialValues.leader,
tag: 'LDR',
},
{
id: 1,
content: {},
tag: '008',
},
{
id: 2,
content: '$a',
tag: '010',
},
{
id: 3,
tag: '110',
},
],
};

expect(result.current.validate(record.records)).not.toBeDefined();
});
});
});
});

describe('when action is CREATE', () => {
Expand Down

0 comments on commit c194430

Please sign in to comment.