Skip to content

Commit

Permalink
UIQM-709 Duplicate LCCN checking query > Do not return instance/bib r…
Browse files Browse the repository at this point in the history
…ecord that is set for deletion
  • Loading branch information
BogdanDenis committed Oct 3, 2024
1 parent 30c579b commit f3acbd9
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* [UIQM-706](https://issues.folio.org/browse/UIQM-706) *BREAKING* Upgrade `marc-records-editor` to `6.0`.
* [UIQM-698](https://issues.folio.org/browse/UIQM-698) Validate 006/007 field lengths.
* [UIQM-704](https://issues.folio.org/browse/UIQM-704) Linked fields - combine split fields before sending for validation.
* [UIQM-709](https://issues.folio.org/browse/UIQM-709) Duplicate LCCN checking query > Do not return instance/bib record that is set for deletion.

## [8.0.1] (https://github.com/folio-org/ui-quick-marc/tree/v8.0.1) (2024-04-18)

Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useValidation/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const BASE_BIB_VALIDATORS = [
{
tag: '010',
validator: RULES.DUPLICATE_LCCN,
ignore: ({ duplicateLccnCheckingEnabled }) => !duplicateLccnCheckingEnabled,
message: () => ({ id: 'ui-quick-marc.record.error.010.lccnDuplicated' }),
},
];
Expand Down Expand Up @@ -273,6 +274,7 @@ const BASE_AUTHORITY_VALIDATORS = [
{
tag: '010',
validator: RULES.DUPLICATE_LCCN,
ignore: ({ duplicateLccnCheckingEnabled }) => !duplicateLccnCheckingEnabled,
message: () => ({ id: 'ui-quick-marc.record.error.010.lccnDuplicated' }),
},
{
Expand Down
166 changes: 166 additions & 0 deletions src/hooks/useValidation/useValidation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,118 @@ describe('useValidation', () => {
}));
});
});

describe('when all instances are staff suppressed', () => {
it('should not return LCCN duplication error', async () => {
const instances = [{
staffSuppress: true,
}, {
staffSuppress: true,
}];

useOkapiKy.mockReturnValueOnce({
get: jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ instances }),
}),
});
const { result } = renderHook(() => useValidation(marcContext), {
wrapper: getWrapper(),
});

await result.current.validate(record.records);

expect(quickMarcContext.setValidationErrors).not.toHaveBeenCalledWith(expect.objectContaining({
4: [{
id: 'ui-quick-marc.record.error.010.lccnDuplicated',
severity: 'error',
}],
}));
});
});

describe('when one of the instances is not staff suppressed', () => {
it('should return LCCN duplication error', async () => {
const instances = [{
staffSuppress: true,
}, {
staffSuppress: false,
}];

useOkapiKy.mockReturnValueOnce({
get: jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ instances }),
}),
});
const { result } = renderHook(() => useValidation(marcContext), {
wrapper: getWrapper(),
});

await result.current.validate(record.records);

expect(quickMarcContext.setValidationErrors).toHaveBeenCalledWith(expect.objectContaining({
4: [{
id: 'ui-quick-marc.record.error.010.lccnDuplicated',
severity: 'error',
}],
}));
});
});

describe('when all instances are discovery suppressed', () => {
it('should not return LCCN duplication error', async () => {
const instances = [{
discoverySuppress: true,
}, {
discoverySuppress: true,
}];

useOkapiKy.mockReturnValueOnce({
get: jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ instances }),
}),
});
const { result } = renderHook(() => useValidation(marcContext), {
wrapper: getWrapper(),
});

await result.current.validate(record.records);

expect(quickMarcContext.setValidationErrors).not.toHaveBeenCalledWith(expect.objectContaining({
4: [{
id: 'ui-quick-marc.record.error.010.lccnDuplicated',
severity: 'error',
}],
}));
});
});

describe('when one of the instances is not discovery suppressed', () => {
it('should return LCCN duplication error', async () => {
const instances = [{
discoverySuppress: true,
}, {
discoverySuppress: false,
}];

useOkapiKy.mockReturnValueOnce({
get: jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ instances }),
}),
});
const { result } = renderHook(() => useValidation(marcContext), {
wrapper: getWrapper(),
});

await result.current.validate(record.records);

expect(quickMarcContext.setValidationErrors).toHaveBeenCalledWith(expect.objectContaining({
4: [{
id: 'ui-quick-marc.record.error.010.lccnDuplicated',
severity: 'error',
}],
}));
});
});
});

describe('when validating Holdings record', () => {
Expand Down Expand Up @@ -1039,6 +1151,60 @@ describe('useValidation', () => {
}]);
});
});

describe('when instance is staff suppressed', () => {
it('should not validate LCCN duplication', async () => {
useOkapiKy.mockReturnValueOnce({
get: jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ instances: [{}] }),
}),
});
const { result } = renderHook(() => useValidation({
...marcContext,
instance: {
staffSuppress: true,
},
}), {
wrapper: getWrapper(),
});

await result.current.validate(record.records);

expect(quickMarcContext.setValidationErrors).not.toHaveBeenCalledWith(expect.objectContaining({
4: [{
id: 'ui-quick-marc.record.error.010.lccnDuplicated',
severity: 'error',
}],
}));
});
});

describe('when instance is discovery suppressed', () => {
it('should not validate LCCN duplication', async () => {
useOkapiKy.mockReturnValueOnce({
get: jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ instances: [{}] }),
}),
});
const { result } = renderHook(() => useValidation({
...marcContext,
instance: {
discoverySuppress: true,
},
}), {
wrapper: getWrapper(),
});

await result.current.validate(record.records);

expect(quickMarcContext.setValidationErrors).not.toHaveBeenCalledWith(expect.objectContaining({
4: [{
id: 'ui-quick-marc.record.error.010.lccnDuplicated',
severity: 'error',
}],
}));
});
});
});

describe('when action is CREATE', () => {
Expand Down
29 changes: 18 additions & 11 deletions src/hooks/useValidation/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,19 @@ export const validateFixedFieldLength = ({ marcRecords, fixedFieldSpec, marcType

return undefined;
};
export const validateLccnDuplication = async ({
ky,
marcRecords,
duplicateLccnCheckingEnabled,
instanceId,
action,
marcType,
}, rule) => {
if (!duplicateLccnCheckingEnabled) {
export const validateLccnDuplication = async (context, rule) => {
if (rule.ignore?.(context)) {
return undefined;
}

const {
ky,
marcRecords,
instanceId,
action,
marcType,
} = context;

const fields = marcRecords.filter(record => record.tag.match(rule.tag));

const validateField = async (field) => {
Expand Down Expand Up @@ -488,9 +489,15 @@ export const validateLccnDuplication = async ({
};

try {
const records = await requests[marcType]().json();
const response = await requests[marcType]().json();

const records = response?.authorities || response?.instances || [];

if (!records.length) {
return undefined;
}

const isLccnDuplicated = records?.authorities?.[0] || records?.instances?.[0];
const isLccnDuplicated = records.some((record) => !record.staffSuppress && !record.discoverySuppress);

if (isLccnDuplicated) {
return rule.message();
Expand Down

0 comments on commit f3acbd9

Please sign in to comment.