Skip to content

Commit

Permalink
Merge pull request #1023 from geonetwork/fix-attachments-cleanup
Browse files Browse the repository at this point in the history
[Editor]: Fix cleanup attachments function
  • Loading branch information
cmoinier authored Oct 23, 2024
2 parents 8665bf5 + 506684a commit f53d469
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
7 changes: 7 additions & 0 deletions apps/metadata-editor-e2e/src/e2e/edit.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
12 changes: 6 additions & 6 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f53d469

Please sign in to comment.