Skip to content

Commit

Permalink
fix: user role edit - api validation errors not shown (#3661)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmajgaard authored Oct 23, 2024
1 parent c47ec1c commit 786c065
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { inject, Injectable } from '@angular/core';
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
import { ComponentStore } from '@ngrx/component-store';
import { tapResponse } from '@ngrx/operators';
import { exhaustMap, filter, Observable, tap } from 'rxjs';
import { exhaustMap, Observable, tap } from 'rxjs';
import { Apollo } from 'apollo-angular';

import { ErrorState, LoadingState } from '@energinet-datahub/dh/shared/domain';
Expand All @@ -32,20 +32,22 @@ import {

interface DhEditUserRoleState {
readonly requestState: LoadingState | ErrorState;
readonly errorCode: string | null;
}

const initialState: DhEditUserRoleState = {
requestState: LoadingState.INIT,
errorCode: null,
};

@Injectable()
export class DhAdminUserRoleEditDataAccessApiStore extends ComponentStore<DhEditUserRoleState> {
private readonly apollo = inject(Apollo);

isLoading$ = this.select((state) => state.requestState === LoadingState.LOADING);
hasValidationError$ = this.select(
(state) => state.requestState === ErrorState.VALIDATION_EXCEPTION
).pipe(filter((value) => value));
validationError$ = this.select((state) =>
state.requestState === ErrorState.VALIDATION_EXCEPTION ? { errorCode: state.errorCode } : null
);

constructor() {
super(initialState);
Expand Down Expand Up @@ -116,15 +118,12 @@ export class DhAdminUserRoleEditDataAccessApiStore extends ComponentStore<DhEdit
);

private handleError(statusCode: number, firstApiError: ApiErrorDescriptor): void {
let requestState = ErrorState.GENERAL_ERROR;

if (
statusCode === HttpStatusCode.BadRequest &&
firstApiError.code === 'market_participant.validation.market_role.reserved'
) {
requestState = ErrorState.VALIDATION_EXCEPTION;
}

this.patchState({ requestState });
this.patchState({
requestState:
statusCode === HttpStatusCode.BadRequest
? ErrorState.VALIDATION_EXCEPTION
: ErrorState.GENERAL_ERROR,
errorCode: statusCode === HttpStatusCode.BadRequest ? firstApiError.code : null,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class DhEditUserRoleModalComponent implements OnInit, AfterViewInit {
readonly marketRolePermissionsIsLoading = this.permissionsQuery.loading;

readonly isLoading$ = this.userRoleEditStore.isLoading$;
readonly hasValidationError$ = this.userRoleEditStore.hasValidationError$;
readonly validationError$ = this.userRoleEditStore.validationError$;

readonly userRoleEditForm = this.formBuilder.group({
name: this.formBuilder.nonNullable.control('', [Validators.required]),
Expand All @@ -159,10 +159,22 @@ export class DhEditUserRoleModalComponent implements OnInit, AfterViewInit {
this.permissionsQuery.refetch({ eicFunction: userRole.eicFunction });
});

this.hasValidationError$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
this.userRoleEditForm.controls.name.setErrors({
nameAlreadyExists: true,
});
this.validationError$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((x) => {
if (x?.errorCode === 'market_participant.validation.market_role.reserved') {
this.userRoleEditForm.controls.name.setErrors({
nameAlreadyExists: true,
});
return;
}

this.userRoleEditForm.controls.name.setErrors(null);

if (x?.errorCode) {
this.toastService.open({
type: 'danger',
message: this.transloco.translate(`marketParticipant.${x.errorCode}`),
});
}
});
}

Expand Down Expand Up @@ -218,9 +230,10 @@ export class DhEditUserRoleModalComponent implements OnInit, AfterViewInit {

const onErrorFn = (statusCode: HttpStatusCode) => {
if (statusCode !== HttpStatusCode.BadRequest) {
const message = this.transloco.translate('admin.userManagement.editUserRole.saveError');

this.toastService.open({ type: 'danger', message });
this.toastService.open({
type: 'danger',
message: this.transloco.translate('admin.userManagement.editUserRole.saveError'),
});
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
}
},
"validation": {
"required_permission_removed": "Denne handling ville resultere i, at en påkrævet rettighed ikke længere ville være tilgængelig, og handlingen blev derfor blokeret.",
"organization": {
"business_register_identifier": {
"reserved": "CVR-nummer {{ identifier }} er i brug"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
}
},
"validation": {
"required_permission_removed": "This operation would result in a required permission no longer being accessable, hence the operation was blocked.",
"organization": {
"business_register_identifier": {
"reserved": "Business register identifier {{ identifier }} is already in use"
Expand Down

0 comments on commit 786c065

Please sign in to comment.