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 e39699206..1c1357cb1 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 @@ -18,10 +18,7 @@ import { someUserFeedbacksFixture, userFeedbackFixture, } from '@geonetwork-ui/common/fixtures' -import { - HttpClientTestingModule, - HttpTestingController, -} from '@angular/common/http/testing' +import { HttpClientTestingModule } from '@angular/common/http/testing' import { HttpClient, HttpEventType } from '@angular/common/http' let geonetworkVersion: string @@ -257,7 +254,6 @@ describe('Gn4PlatformService', () => { registriesApiService = TestBed.inject(RegistriesApiService) userFeedbackApiService = TestBed.inject(UserfeedbackApiService as any) recordsApiService = TestBed.inject(RecordsApiService) - TestBed.inject(HttpTestingController) }) it('creates', () => { @@ -776,6 +772,7 @@ describe('Gn4PlatformService', () => { }) it('calls api service', async () => { service.attachFileToRecord('12345', file) + expect(recordsApiService.getAllResources).toHaveBeenCalledWith('12345') expect(recordsApiService.putResource).toHaveBeenCalledWith( '12345', file, 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 2ce071699..b1bb7cdc8 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 @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core' -import { combineLatest, Observable, of, switchMap } from 'rxjs' +import { combineLatest, Observable, of, switchMap, throwError } from 'rxjs' import { catchError, filter, map, shareReplay, tap } from 'rxjs/operators' import { MeApiService, @@ -295,17 +295,13 @@ export class Gn4PlatformService implements PlatformServiceInterface { attachFileToRecord(recordUuid: string, file: File): Observable { let sizeBytes = -1 - this.recordsApiService - .getAssociatedResources(recordUuid) - .subscribe((res) => console.log(res)) - // Check if the ressource already exist on the server and rename it if that's the case return this.getRecordAttachments(recordUuid).pipe( map((recordAttachement) => recordAttachement.map((r) => r.fileName)), switchMap((fileNames) => { + console.log(fileNames) const fileName = noDuplicateFileName(file.name, fileNames) - console.log(fileName) file = new File([file], fileName, { type: file.type }) return this.recordsApiService @@ -333,9 +329,11 @@ export class Gn4PlatformService implements PlatformServiceInterface { } return undefined }), - filter((event) => !!event), - catchError(() => undefined) // todo + filter((event) => !!event) ) + }), + catchError((error) => { + return throwError(() => new Error(error.error.message)) }) ) } diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.spec.ts index 14c728c0d..ce65f266c 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.spec.ts @@ -142,7 +142,7 @@ describe('FormFieldOnlineLinkResourcesComponent', () => { const file = new File([''], 'test-file.txt') expect(component.uploadProgress).toBeUndefined() component.handleFileChange(file) - uploadSubject.error({ error: { message: 'something went wrong' } }) + uploadSubject.error(new Error('something went wrong')) expect(notificationsService.showNotification).toHaveBeenCalledWith({ type: 'error', closeMessage: 'editor.record.onlineResourceError.closeMessage', diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.ts index bf85a403c..333b4921a 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-online-link-resources/form-field-online-link-resources.component.ts @@ -26,10 +26,9 @@ import { import { NotificationsService } from '@geonetwork-ui/feature/notifications' import { TranslateModule, TranslateService } from '@ngx-translate/core' import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' -import { Subscription, switchMap } from 'rxjs' +import { Subscription } from 'rxjs' import { MatDialog } from '@angular/material/dialog' import { MAX_UPLOAD_SIZE_MB } from '../../../../fields.config' -import { map } from 'rxjs/operators' @Component({ selector: 'gn-ui-form-field-online-link-resources', @@ -78,26 +77,7 @@ export class FormFieldOnlineLinkResourcesComponent { handleFileChange(file: File) { this.uploadProgress = 0 this.uploadSubscription = this.platformService - .getRecordAttachments(this.metadataUuid) - .pipe( - map((recordAttachement) => recordAttachement.map((r) => r.fileName)), - switchMap((fileNames) => { - let fileToUpload = file - - if (fileNames.includes(file.name)) { - const fileNameParts = file.name.split('.') - const extension = fileNameParts.pop() - const baseName = fileNameParts.join('.') - const newFileName = `${baseName}_${Date.now()}.${extension}` - - fileToUpload = new File([file], newFileName, { type: file.type }) - } - return this.platformService.attachFileToRecord( - this.metadataUuid, - fileToUpload - ) - }) - ) + .attachFileToRecord(this.metadataUuid, file) .subscribe({ next: (event) => { if (event.type === 'progress') { @@ -114,7 +94,7 @@ export class FormFieldOnlineLinkResourcesComponent { this.valueChange.emit([...this.allResources, newResource]) } }, - error: (error: any) => this.handleError(error.error.message), + error: (error: Error) => this.handleError(error), }) } @@ -152,7 +132,7 @@ export class FormFieldOnlineLinkResourcesComponent { this.openEditDialog(resource, index) } - private handleError(error: string) { + private handleError(error: Error) { this.uploadProgress = undefined this.cd.detectChanges() this.notificationsService.showNotification({ @@ -162,7 +142,7 @@ export class FormFieldOnlineLinkResourcesComponent { ), text: `${this.translateService.instant( 'editor.record.onlineResourceError.body' - )} ${error}`, + )} ${error.message}`, closeMessage: this.translateService.instant( 'editor.record.onlineResourceError.closeMessage' ), diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.spec.ts index 12e06f2a2..a18dab701 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.spec.ts @@ -118,7 +118,7 @@ describe('FormFieldOverviewsComponent', () => { const file = new File([''], 'test-file.txt') expect(component.uploadProgress).toBeUndefined() component.handleFileChange(file) - uploadSubject.error({ error: { message: 'something went wrong' } }) + uploadSubject.error(new Error('something went wrong')) expect(notificationsService.showNotification).toHaveBeenCalledWith({ type: 'error', closeMessage: 'editor.record.resourceError.closeMessage', diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.ts index 2a804ff0e..5d750307b 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-overviews/form-field-overviews.component.ts @@ -68,7 +68,7 @@ export class FormFieldOverviewsComponent { }) } }, - error: (error: any) => this.handleError(error.error.message), + error: (error: Error) => this.handleError(error), }) } @@ -106,7 +106,7 @@ export class FormFieldOverviewsComponent { this.valueChange.emit(overView ? [overView] : []) } - private handleError = (error: string) => { + private handleError = (error: Error) => { this.uploadProgress = undefined this.cd.markForCheck() this.notificationsService.showNotification({ @@ -114,7 +114,7 @@ export class FormFieldOverviewsComponent { title: this.translateService.instant('editor.record.resourceError.title'), text: `${this.translateService.instant( 'editor.record.resourceError.body' - )} ${error}`, + )} ${error.message}`, closeMessage: this.translateService.instant( 'editor.record.resourceError.closeMessage' ),