From 7db601c1d5e1be83d7fa6ddcf7e238f27ab0e5ac Mon Sep 17 00:00:00 2001 From: cmoinier Date: Tue, 22 Oct 2024 10:13:25 +0200 Subject: [PATCH 1/3] fix cleanupRecordAttachments --- .../src/lib/gn4/platform/gn4-platform.service.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts index d187ed8d0..cb5ea9c07 100644 --- a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts +++ b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts @@ -316,12 +316,12 @@ export class Gn4PlatformService implements PlatformServiceInterface { // Received object from API is not a RelatedResponseApiModel, so we need // to cast it as any and do the bellow mappings to get the wanted values. const resourceIdsToKeep = [ - ...((associatedResources as any).onlines ?? []) - .map((o) => o.title) - .map((o) => o['']), - ...((associatedResources as any).thumbnails ?? []) - .map((o) => o.title) - .map((o) => o['']), + ...((associatedResources as any).onlines ?? []).map( + (o) => Object.values(o.title)[0] + ), + ...((associatedResources as any).thumbnails ?? []).map( + (o) => Object.values(o.title)[0] + ), ] const resourceIdsToRemove = recordResources From f318700ac8f85369f7b3dd1f0bf96a680358a9aa Mon Sep 17 00:00:00 2001 From: cmoinier Date: Tue, 22 Oct 2024 10:13:52 +0200 Subject: [PATCH 2/3] add ut for cleanupRecordAttachments --- .../gn4/platform/gn4-platform.service.spec.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts index d61307167..13df79fe0 100644 --- a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts +++ b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts @@ -791,6 +791,37 @@ describe('Gn4PlatformService', () => { record.uniqueIdentifier ) }) + it('should clean record attachments no longer used', (done) => { + const record = { uniqueIdentifier: '123' } as CatalogRecord + const associatedResources = { + onlines: [{ title: { en: 'doge.jpg' } }], + thumbnails: [{ title: { en: 'flower.jpg' } }], + } + ;(recordsApiService.getAssociatedResources as jest.Mock).mockReturnValue( + of(associatedResources) + ) + ;(recordsApiService.getAllResources as jest.Mock).mockReturnValue( + of([ + { filename: 'doge.jpg' }, + { filename: 'flower.jpg' }, + { filename: 'remove1.jpg' }, + ]) + ) + ;(recordsApiService.delResource as jest.Mock).mockReturnValue( + of(undefined) + ) + + service.cleanRecordAttachments(record).subscribe({ + next: () => { + expect(recordsApiService.delResource).toHaveBeenCalledWith( + record.uniqueIdentifier, + 'remove1.jpg' + ) + done() + }, + error: done.fail, + }) + }) }) describe('attachFileToRecord', () => { From 506684ae5a09fa0b78ba8e5c48b13aa2241dd112 Mon Sep 17 00:00:00 2001 From: cmoinier Date: Wed, 23 Oct 2024 11:02:44 +0200 Subject: [PATCH 3/3] modify attachments e2e test to check if image is not cleaned up --- apps/metadata-editor-e2e/src/e2e/edit.cy.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/metadata-editor-e2e/src/e2e/edit.cy.ts b/apps/metadata-editor-e2e/src/e2e/edit.cy.ts index abed248ec..22db16567 100644 --- a/apps/metadata-editor-e2e/src/e2e/edit.cy.ts +++ b/apps/metadata-editor-e2e/src/e2e/edit.cy.ts @@ -186,6 +186,13 @@ describe('editor form', () => { 'src/fixtures/sample.png' ) cy.editor_publishAndReload() + cy.intercept({ + method: 'GET', + url: '**/attachments/sample.png', + }).as('importUrlRequest') + cy.get('@importUrlRequest') + .its('response.statusCode') + .should('eq', 200) cy.get('@saveStatus').should('eq', 'record_up_to_date') cy.get('gn-ui-image-input').find('img').should('have.length', 1) })