From a78bd3f77ee045cb307a662423a94209f35521d1 Mon Sep 17 00:00:00 2001 From: Denys Bohdan Date: Fri, 4 Oct 2024 10:52:10 +0200 Subject: [PATCH] UIQM-709 use search search parameters to not include suppressed records --- src/hooks/useValidation/useValidation.test.js | 166 ------------------ src/hooks/useValidation/validators.js | 10 +- 2 files changed, 2 insertions(+), 174 deletions(-) diff --git a/src/hooks/useValidation/useValidation.test.js b/src/hooks/useValidation/useValidation.test.js index 8c021cf8..93fa3ec7 100644 --- a/src/hooks/useValidation/useValidation.test.js +++ b/src/hooks/useValidation/useValidation.test.js @@ -651,118 +651,6 @@ 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', () => { @@ -1151,60 +1039,6 @@ 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', () => { diff --git a/src/hooks/useValidation/validators.js b/src/hooks/useValidation/validators.js index 0f503d7c..c3d1bf65 100644 --- a/src/hooks/useValidation/validators.js +++ b/src/hooks/useValidation/validators.js @@ -480,7 +480,7 @@ export const validateLccnDuplication = async (context, rule) => { const searchParams = { limit: 1, - query: `(${lccnQuery})${idQuery}`, + query: `(${lccnQuery})${idQuery} and (staffSuppress=="false" or discoverySuppress=="false")`, }; const requests = { @@ -491,13 +491,7 @@ export const validateLccnDuplication = async (context, rule) => { try { const response = await requests[marcType]().json(); - const records = response?.authorities || response?.instances || []; - - if (!records.length) { - return undefined; - } - - const isLccnDuplicated = records.some((record) => !record.staffSuppress && !record.discoverySuppress); + const isLccnDuplicated = response?.authorities?.[0] || response?.instances?.[0]; if (isLccnDuplicated) { return rule.message();