Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Romuald Caplier committed Sep 30, 2024
1 parent 83481bf commit 7c1883e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -295,17 +295,13 @@ export class Gn4PlatformService implements PlatformServiceInterface {
attachFileToRecord(recordUuid: string, file: File): Observable<UploadEvent> {
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
Expand Down Expand Up @@ -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))
})
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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') {
Expand All @@ -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),
})
}

Expand Down Expand Up @@ -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({
Expand All @@ -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'
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class FormFieldOverviewsComponent {
})
}
},
error: (error: any) => this.handleError(error.error.message),
error: (error: Error) => this.handleError(error),
})
}

Expand Down Expand Up @@ -106,15 +106,15 @@ 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({
type: 'error',
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'
),
Expand Down

0 comments on commit 7c1883e

Please sign in to comment.