Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ME]: Clean up attachments on publishing #1004

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import { AvatarServiceInterface } from '../auth/avatar.service.interface'
import { Gn4PlatformMapper } from './gn4-platform.mapper'
import { LangService } from '@geonetwork-ui/util/i18n'
import {
datasetRecordsFixture,
someUserFeedbacksFixture,
userFeedbackFixture,
} from '@geonetwork-ui/common/fixtures'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { HttpClient, HttpEventType } from '@angular/common/http'
import { CatalogRecord } from '@geonetwork-ui/common/domain/model/record'

let geonetworkVersion: string

Expand Down Expand Up @@ -171,6 +173,11 @@ class LangServiceMock {
iso3 = 'fre'
}

const associatedResources = {
onlines: [],
thumbnails: [],
}

class RecordsApiServiceMock {
getAllResources = jest.fn(() =>
of([
Expand All @@ -184,6 +191,8 @@ class RecordsApiServiceMock {
},
])
)
getAssociatedResources = jest.fn(() => of(associatedResources))
delResource = jest.fn(() => of(undefined))
putResource = jest.fn(() =>
of({
type: HttpEventType.UploadProgress,
Expand Down Expand Up @@ -769,6 +778,21 @@ describe('Gn4PlatformService', () => {
})
})

describe('cleanRecordAttachments', () => {
it('calls api service', async () => {
const record = datasetRecordsFixture() as unknown as CatalogRecord

service.cleanRecordAttachments(record)

expect(recordsApiService.getAssociatedResources).toHaveBeenCalledWith(
record.uniqueIdentifier
)
expect(recordsApiService.getAllResources).toHaveBeenCalledWith(
record.uniqueIdentifier
)
})
})

describe('attachFileToRecord', () => {
let file: File
beforeEach(() => {
Expand Down
59 changes: 57 additions & 2 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Injectable } from '@angular/core'
import { combineLatest, Observable, of, switchMap, throwError } from 'rxjs'
import { catchError, filter, map, shareReplay, tap } from 'rxjs/operators'
import {
catchError,
filter,
map,
mergeMap,
shareReplay,
tap,
} from 'rxjs/operators'
import {
MeApiService,
RecordsApiService,
Expand All @@ -16,6 +22,7 @@ import {
} from '@geonetwork-ui/common/domain/platform.service.interface'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'
import {
CatalogRecord,
Keyword,
Organization,
UserFeedback,
Expand All @@ -30,6 +37,14 @@ import {
} from '@geonetwork-ui/api/metadata-converter'
import { KeywordType } from '@geonetwork-ui/common/domain/model/thesaurus'
import { noDuplicateFileName } from '@geonetwork-ui/util/shared'
import {
combineLatest,
forkJoin,
Observable,
of,
switchMap,
throwError,
} from 'rxjs'

const minApiVersion = '4.2.2'

Expand Down Expand Up @@ -292,6 +307,46 @@ export class Gn4PlatformService implements PlatformServiceInterface {
)
}

cleanRecordAttachments(record: CatalogRecord): Observable<void> {
return combineLatest([
this.recordsApiService.getAssociatedResources(record.uniqueIdentifier),
this.recordsApiService.getAllResources(record.uniqueIdentifier),
]).pipe(
map(([associatedResources, recordResources]) => {
// 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['']),
]
rcaplier marked this conversation as resolved.
Show resolved Hide resolved

const resourceIdsToRemove = recordResources
.map((r) => r.filename)
.filter((resourceId) => !resourceIdsToKeep.includes(resourceId))

return resourceIdsToRemove
}),
mergeMap((resourceIdsToRemove) =>
forkJoin(
resourceIdsToRemove.map((attachementId) =>
this.recordsApiService.delResource(
record.uniqueIdentifier,
attachementId
)
)
).pipe(map(() => undefined))
),
catchError((error) => {
console.error('Error while cleaning attachments:', error)
throw error
})
)
}

attachFileToRecord(recordUuid: string, file: File): Observable<UploadEvent> {
let sizeBytes = -1

Expand Down
3 changes: 2 additions & 1 deletion libs/common/domain/src/lib/platform.service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Observable } from 'rxjs'
import type { UserModel } from './model/user/user.model'
import type { Organization } from './model/record/organization.model'
import { Keyword, UserFeedback } from './model/record'
import { CatalogRecord, Keyword, UserFeedback } from './model/record'
import { KeywordType } from './model/thesaurus'

export interface RecordAttachment {
Expand Down Expand Up @@ -49,6 +49,7 @@ export abstract class PlatformServiceInterface {
abstract getRecordAttachments(
recordUuid: string
): Observable<RecordAttachment[]>
abstract cleanRecordAttachments(recordUuid: CatalogRecord): Observable<void>
abstract attachFileToRecord(
recordUuid: string,
file: File
Expand Down
5 changes: 5 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { datasetRecordsFixture } from '@geonetwork-ui/common/fixtures'
import { EditorService } from '../services/editor.service'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface'
import { EditorPartialState } from './editor.reducer'
import { MockProvider } from 'ng-mocks'
import { Gn4PlatformService } from '@geonetwork-ui/api/repository'

class EditorServiceMock {
saveRecord = jest.fn((record) => of([record, '<xml>blabla</xml>']))
Expand Down Expand Up @@ -54,6 +56,9 @@ describe('EditorEffects', () => {
provide: RecordsRepositoryInterface,
useClass: RecordsRepositoryMock,
},
MockProvider(Gn4PlatformService, {
cleanRecordAttachments: jest.fn(() => of(undefined)),
}),
],
})

Expand Down
26 changes: 25 additions & 1 deletion libs/feature/editor/src/lib/+state/editor.effects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, Injectable } from '@angular/core'
import { Actions, createEffect, ofType } from '@ngrx/effects'
import { debounceTime, filter, of, withLatestFrom } from 'rxjs'
import { debounceTime, EMPTY, filter, of, withLatestFrom } from 'rxjs'
import { catchError, map, switchMap } from 'rxjs/operators'
import * as EditorActions from './editor.actions'
import { EditorService } from '../services/editor.service'
Expand All @@ -12,12 +12,14 @@ import {
selectRecordSource,
} from './editor.selectors'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface'
import { Gn4PlatformService } from '@geonetwork-ui/api/repository'

@Injectable()
export class EditorEffects {
private actions$ = inject(Actions)
private editorService = inject(EditorService)
private recordsRepository = inject(RecordsRepositoryInterface)
private gn4PlateformService = inject(Gn4PlatformService)
private store = inject(Store)

saveRecord$ = createEffect(() =>
Expand Down Expand Up @@ -55,6 +57,28 @@ export class EditorEffects {
)
)

cleanRecordAttachments$ = createEffect(
() =>
this.actions$.pipe(
ofType(EditorActions.saveRecordSuccess),
withLatestFrom(this.store.select(selectRecord)),
switchMap(([_, record]) => {
this.gn4PlateformService.cleanRecordAttachments(record).subscribe({
next: (_) => undefined,
error: (err) => {
console.error(err)
},
})
return EMPTY
}),
catchError((error) => {
console.error(error)
return EMPTY
})
),
{ dispatch: false }
)

markAsChanged$ = createEffect(() =>
this.actions$.pipe(
ofType(EditorActions.updateRecordField),
Expand Down
Loading