Skip to content

Commit

Permalink
Merge pull request #885 from geonetwork/allow-users-answer-feedback
Browse files Browse the repository at this point in the history
[User Feedback] : allow any auth user to answer & handle errors
  • Loading branch information
cmoinier authored May 28, 2024
2 parents 639c48f + 9a5b125 commit ff2a9db
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
[userFeedBacksAnswers]="
userFeedBacksAnswers.get(userFeedbackParent.uuid)
"
[isActiveUserEditor]="isActiveUserMetadaEditor"
[activeUser]="activeUser"
[isAddUserFeedbackLoading]="isAddUserFeedbackLoading"
(newUserFeedbackAnswer)="onNewUserFeedbackAnswer($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
OnDestroy,
OnInit,
} from '@angular/core'
import { filter, switchMap, takeUntil } from 'rxjs/operators'
import { BehaviorSubject, combineLatest, Observable, Subject } from 'rxjs'
import { catchError, filter, switchMap, takeUntil } from 'rxjs/operators'
import { BehaviorSubject, combineLatest, Observable, of, Subject } from 'rxjs'
import {
UserFeedback,
UserFeedbackViewModel,
Expand Down Expand Up @@ -85,24 +85,30 @@ export class RecordUserFeedbacksComponent implements OnInit, OnDestroy {

ngOnInit(): void {
this.metadataViewFacade.isAllUserFeedbackLoading$
.pipe(takeUntil(this.destroy$))
.pipe(
takeUntil(this.destroy$),
catchError((error) => {
console.error('Error while loading all the user feedbacks:', error)
return of(false)
})
)
.subscribe((isLoading) => (this.isAllUserFeedbackLoading = isLoading))

this.metadataViewFacade.isAddUserFeedbackLoading$
.pipe(takeUntil(this.destroy$))
.pipe(
takeUntil(this.destroy$),
catchError((error) => {
console.error('Error while adding the user feedback:', error)
return of(false)
})
)
.subscribe((isLoading) => (this.isAddUserFeedbackLoading = isLoading))

this.metadataViewFacade.loadUserFeedbacks(this.metadataUuid)

this.activeUser$.pipe(takeUntil(this.destroy$)).subscribe((user) => {
this.activeUser = user
this.isActiveUserMetadaEditor = [
'Administrator',
'UserAdmin',
'Reviewer',
'Editor',
].includes(user?.profile)
})
try {
this.metadataViewFacade.loadUserFeedbacks(this.metadataUuid)
} catch (error) {
console.error('Error while loading the user feedbacks:', error)
}

combineLatest([
this.metadataViewFacade.userFeedbacks$,
Expand All @@ -114,50 +120,59 @@ export class RecordUserFeedbacksComponent implements OnInit, OnDestroy {
takeUntil(this.destroy$),
switchMap(
async ([userFeedbacks, selectedSortingStrategy, activeUser]) => {
this.activeUser = activeUser
try {
this.activeUser = activeUser

const userFeedbacksParents = userFeedbacks
.filter((feedback) => !feedback.parentUuid)
.sort(selectedSortingStrategy)
const userFeedbacksParents = userFeedbacks
.filter((feedback) => !feedback.parentUuid)
.sort(selectedSortingStrategy)

const userFeedbacksAnswers = userFeedbacks
.filter((feedback) => feedback.parentUuid)
.sort(this.sortByDateFromOldestToNewest)
const userFeedbacksAnswers = userFeedbacks
.filter((feedback) => feedback.parentUuid)
.sort(this.sortByDateFromOldestToNewest)

const userFeedbacksParentsViewModels = await Promise.all(
userFeedbacksParents.map((feedback) =>
this.mapper.createUserFeedbackViewModel(feedback)
const userFeedbacksParentsViewModels = await Promise.all(
userFeedbacksParents.map((feedback) =>
this.mapper.createUserFeedbackViewModel(feedback)
)
)
)

const userFeedbacksAnswersViewModels = await Promise.all(
userFeedbacksAnswers.map((feedback) =>
this.mapper.createUserFeedbackViewModel(feedback)
const userFeedbacksAnswersViewModels = await Promise.all(
userFeedbacksAnswers.map((feedback) =>
this.mapper.createUserFeedbackViewModel(feedback)
)
)
)

const userFeedBacksAnswersMap = new Map()
userFeedbacksAnswersViewModels.forEach(
(userFeedbackAnswerViewModel) => {
const parentUuid = userFeedbackAnswerViewModel.parentUuid
if (userFeedBacksAnswersMap.has(parentUuid)) {
userFeedBacksAnswersMap
.get(parentUuid)
.push(userFeedbackAnswerViewModel)
} else {
userFeedBacksAnswersMap.set(parentUuid, [
userFeedbackAnswerViewModel,
])

const userFeedBacksAnswersMap = new Map()
userFeedbacksAnswersViewModels.forEach(
(userFeedbackAnswerViewModel) => {
const parentUuid = userFeedbackAnswerViewModel.parentUuid
if (userFeedBacksAnswersMap.has(parentUuid)) {
userFeedBacksAnswersMap
.get(parentUuid)
.push(userFeedbackAnswerViewModel)
} else {
userFeedBacksAnswersMap.set(parentUuid, [
userFeedbackAnswerViewModel,
])
}
}
}
)
)

return {
parentsViewModels: userFeedbacksParentsViewModels,
answersMap: userFeedBacksAnswersMap,
return {
parentsViewModels: userFeedbacksParentsViewModels,
answersMap: userFeedBacksAnswersMap,
}
} catch (error) {
console.error('Error processing user feedbacks:', error)
throw error
}
}
)
),
catchError((error) => {
console.error('Error combining feedbacks and answers:', error)
return of({ parentsViewModels: [], answersMap: new Map() })
})
)
.subscribe({
next: ({ parentsViewModels, answersMap }) => {
Expand All @@ -166,7 +181,10 @@ export class RecordUserFeedbacksComponent implements OnInit, OnDestroy {
this.cdr.markForCheck()
},
error: (err) => {
console.error('Error processing feedback', err)
console.error(
'Error during the subscription to user feedback data:',
err
)
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ describe('Gn4PlatformService', () => {
)

service.getUserFeedbacks(mockUuid).subscribe({
next: () => {
done('Expected error, but got success')
},
error: (error) => {
expect(error).toBe(errorResponse)
next: (result) => {
expect(result).toBeUndefined()
done()
},
error: () => {
done('Expected success, but got error')
},
})
})
})
Expand Down
26 changes: 16 additions & 10 deletions libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,25 @@ export class Gn4PlatformService implements PlatformServiceInterface {
}

getUserFeedbacks(uuid: string): Observable<UserFeedback[]> {
return this.userfeedbackApiService
.getUserComments(uuid)
.pipe(
map((userFeedbacks) =>
userFeedbacks.map(this.mapper.userFeedbacksFromApi)
)
)
return this.userfeedbackApiService.getUserComments(uuid).pipe(
map((userFeedbacks) =>
userFeedbacks.map(this.mapper.userFeedbacksFromApi)
),
catchError((error) => {
console.error('Error fetching user feedbacks:', error)
return of(undefined)
})
)
}

postUserFeedbacks(userFeedback: UserFeedback): Observable<void> {
const mappedUserFeedBack = this.mapper.userFeedbacksToApi(userFeedback)
return this.userfeedbackApiService
.newUserFeedback(mappedUserFeedBack)
.pipe(map(() => undefined))
return this.userfeedbackApiService.newUserFeedback(mappedUserFeedBack).pipe(
map(() => undefined),
catchError((error) => {
console.error('Error posting user feedback:', error)
return of(undefined)
})
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
></gn-ui-user-feedback-item>
</div>

<div *ngIf="isActiveUserEditor" class="mt-2 flex flex-col">
<div *ngIf="activeUser" class="mt-2 flex flex-col">
<hr class="-mx-4 my-4" />
<div
id="new-comment-buttons"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { UserModel } from '@geonetwork-ui/common/domain/model/user'
export class UserFeedbackItemComponent implements OnInit {
@Input() userFeedbackParent: UserFeedbackViewModel
@Input() userFeedBacksAnswers: UserFeedbackViewModel[]
@Input() isActiveUserEditor: boolean
@Input() activeUser: UserModel
@Input() isLastComment: boolean
@Input() isAddUserFeedbackLoading: boolean
Expand Down

0 comments on commit ff2a9db

Please sign in to comment.