Skip to content

Commit

Permalink
feat(me): Added an effect cleanRecordAttachments on saveRecordSuccess…
Browse files Browse the repository at this point in the history
… action that delete all ressources that are not used in the record.
  • Loading branch information
Romuald Caplier committed Sep 25, 2024
1 parent e5f8e61 commit 1d14433
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
48 changes: 47 additions & 1 deletion libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Injectable } from '@angular/core'
import { combineLatest, Observable, of, switchMap } from 'rxjs'
import {
combineLatest,
forkJoin,
mergeMap,
Observable,
of,
switchMap,
} from 'rxjs'
import { catchError, filter, map, shareReplay, tap } from 'rxjs/operators'
import {
MeApiService,
Expand All @@ -13,6 +20,7 @@ import {
import { PlatformServiceInterface } 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 Down Expand Up @@ -288,6 +296,44 @@ 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]) => {
const resourceIdsToKeep = [
...((associatedResources as any).onlines ?? [])
.map((o) => o.title)
.map((o) => o['']),
...((associatedResources as any).thumbnails ?? [])
.map((o) => o.title)
.map((o) => o['']),
]

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) {
let sizeBytes = -1
return this.recordsApiService
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'

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
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 @@ -11,12 +11,14 @@ import {
selectRecordAlreadySavedOnce,
} 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 @@ -53,6 +55,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

0 comments on commit 1d14433

Please sign in to comment.