Skip to content
Snippets Groups Projects
Commit def8bc33 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6170 OZG-6209 remove handling ApiError from abstract form service

parent 9e4b3828
No related branches found
No related tags found
No related merge requests found
...@@ -25,12 +25,12 @@ import { CommandResource } from '@alfa-client/command-shared'; ...@@ -25,12 +25,12 @@ import { CommandResource } from '@alfa-client/command-shared';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms'; import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { Resource } from '@ngxp/rest'; import { Resource } from '@ngxp/rest';
import { cold } from 'jest-marbles'; import { cold } from 'jest-marbles';
import { createApiError, createInvalidParam, createIssue, createProblemDetail } from 'libs/tech-shared/test/error'; import { createInvalidParam, createProblemDetail } from 'libs/tech-shared/test/error';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { AbstractFormService } from './formservice.abstract'; import { AbstractFormService } from './formservice.abstract';
import { createEmptyStateResource, createErrorStateResource, createStateResource, StateResource } from '../resource/resource.util'; import { createEmptyStateResource, createErrorStateResource, createStateResource, StateResource } from '../resource/resource.util';
import { ApiError, HttpError, InvalidParam, Issue, ProblemDetail } from '../tech.model'; import { HttpError, InvalidParam, ProblemDetail } from '../tech.model';
import { createCommandResource } from '../../../../command-shared/test/command'; import { createCommandResource } from '../../../../command-shared/test/command';
import * as ValidationUtil from '../validation/tech.validation.util'; import * as ValidationUtil from '../validation/tech.validation.util';
...@@ -47,16 +47,16 @@ describe('AbstractFormService', () => { ...@@ -47,16 +47,16 @@ describe('AbstractFormService', () => {
}); });
describe('submit', () => { describe('submit', () => {
describe('with api error', () => { describe('with ProblemDetail', () => {
const stateResourceWithError: StateResource<ApiError> = const stateResourceWithError: StateResource<ProblemDetail> =
createErrorStateResource(createApiError()); createErrorStateResource(createProblemDetail());
beforeEach(() => { beforeEach(() => {
TestFormService.SUBMIT_OBSERVABLE = () => of(stateResourceWithError); TestFormService.SUBMIT_OBSERVABLE = () => of(stateResourceWithError);
formService.handleResponse = jest.fn((stateResource) => stateResource); formService.handleResponse = jest.fn((stateResource) => stateResource);
}); });
it('should call handle response for api error', (done) => { it('should call handle response for ProblemDetail', (done) => {
formService.submit().subscribe(() => { formService.submit().subscribe(() => {
expect(formService.handleResponse).toHaveBeenCalledWith(stateResourceWithError); expect(formService.handleResponse).toHaveBeenCalledWith(stateResourceWithError);
done(); done();
...@@ -97,8 +97,8 @@ describe('AbstractFormService', () => { ...@@ -97,8 +97,8 @@ describe('AbstractFormService', () => {
}); });
describe('handleResponse', () => { describe('handleResponse', () => {
const apiError: ApiError = createApiError(); const problemDetail: ProblemDetail = createProblemDetail();
const stateResource: StateResource<CommandResource> = createErrorStateResource(apiError); const stateResource: StateResource<CommandResource> = createErrorStateResource(problemDetail);
beforeEach(() => { beforeEach(() => {
formService.handleError = jest.fn(); formService.handleError = jest.fn();
...@@ -107,7 +107,7 @@ describe('AbstractFormService', () => { ...@@ -107,7 +107,7 @@ describe('AbstractFormService', () => {
it('should handleError on validation error', () => { it('should handleError on validation error', () => {
formService.handleResponse({ ...stateResource, loading: false }); formService.handleResponse({ ...stateResource, loading: false });
expect(formService.handleError).toHaveBeenCalledWith(apiError); expect(formService.handleError).toHaveBeenCalledWith(problemDetail);
}); });
it('should return stateresource while loading', () => { it('should return stateresource while loading', () => {
...@@ -129,34 +129,6 @@ describe('AbstractFormService', () => { ...@@ -129,34 +129,6 @@ describe('AbstractFormService', () => {
expect(formService.setErrorByProblemDetail).toHaveBeenCalledWith(problemDetail); expect(formService.setErrorByProblemDetail).toHaveBeenCalledWith(problemDetail);
}); });
it('should set api error', () => {
formService.setErrorByApiError = jest.fn();
const apiError: ApiError = createApiError();
formService.handleError(apiError);
expect(formService.setErrorByApiError).toHaveBeenCalledWith(apiError);
});
});
describe('set error by api error', () => {
const issue: Issue = createIssue();
const apiError: ApiError = createApiError([issue]);
it('should call setIssueValidationError', () => {
const setInvalidParamValidationErrorSpy: jest.SpyInstance<void> = jest
.spyOn(ValidationUtil, 'setIssueValidationError')
.mockImplementation();
formService.setErrorByApiError(apiError);
expect(setInvalidParamValidationErrorSpy).toHaveBeenCalledWith(
formService.form,
issue,
TestFormService.PATH_PREFIX,
);
});
}); });
describe('set error by problem detail', () => { describe('set error by problem detail', () => {
......
...@@ -28,12 +28,9 @@ import { isNil } from 'lodash-es'; ...@@ -28,12 +28,9 @@ import { isNil } from 'lodash-es';
import { identity, Observable, OperatorFunction } from 'rxjs'; import { identity, Observable, OperatorFunction } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { hasStateResourceError, StateResource } from '../resource/resource.util'; import { hasStateResourceError, StateResource } from '../resource/resource.util';
import { ApiError, HttpError, InvalidParam, Issue, ProblemDetail } from '../tech.model'; import { HttpError, InvalidParam, ProblemDetail } from '../tech.model';
import { isNotUndefined } from '../tech.util'; import { isNotUndefined } from '../tech.util';
import { import { setInvalidParamValidationError } from '../validation/tech.validation.util';
setInvalidParamValidationError,
setIssueValidationError,
} from '../validation/tech.validation.util';
export abstract class AbstractFormService { export abstract class AbstractFormService {
form: UntypedFormGroup; form: UntypedFormGroup;
...@@ -68,28 +65,15 @@ export abstract class AbstractFormService { ...@@ -68,28 +65,15 @@ export abstract class AbstractFormService {
} }
handleError(error: HttpError): void { handleError(error: HttpError): void {
if (this.isApiError(error)) { if (this.hasError(error)) {
this.setErrorByApiError(<ApiError>error);
}
if (this.isProblemDetail(error)) {
this.setErrorByProblemDetail(<ProblemDetail>error); this.setErrorByProblemDetail(<ProblemDetail>error);
} }
} }
private isApiError(error: HttpError): boolean { private hasError(error: HttpError): boolean {
return isNotUndefined((<ApiError>error).issues);
}
private isProblemDetail(error: HttpError): boolean {
return isNotUndefined((<ProblemDetail>error)[this.PROBLEM_DETAIL_INVALID_PARAMS_KEY]); return isNotUndefined((<ProblemDetail>error)[this.PROBLEM_DETAIL_INVALID_PARAMS_KEY]);
} }
setErrorByApiError(apiError: ApiError): void {
apiError.issues.forEach((issue: Issue) =>
setIssueValidationError(this.form, issue, this.getPathPrefix()),
);
}
setErrorByProblemDetail(error: ProblemDetail): void { setErrorByProblemDetail(error: ProblemDetail): void {
error[this.PROBLEM_DETAIL_INVALID_PARAMS_KEY].forEach((invalidParam: InvalidParam) => { error[this.PROBLEM_DETAIL_INVALID_PARAMS_KEY].forEach((invalidParam: InvalidParam) => {
setInvalidParamValidationError(this.form, invalidParam, this.getPathPrefix()); setInvalidParamValidationError(this.form, invalidParam, this.getPathPrefix());
......
...@@ -31,6 +31,7 @@ export enum ValidationMessageCode { ...@@ -31,6 +31,7 @@ export enum ValidationMessageCode {
FIELD_DATE_PAST = 'validation_field_date_past', FIELD_DATE_PAST = 'validation_field_date_past',
FIELD_INVALID = 'validation_field_invalid', FIELD_INVALID = 'validation_field_invalid',
FIELD_DATE_FORMAT_INVALID = 'validation_field_date_format_invalid', FIELD_DATE_FORMAT_INVALID = 'validation_field_date_format_invalid',
FIELD_ASSIGN_BEARBEITER_NOT_EXIST = 'fe_only_validation_bearbeiter_not_exist',
} }
export const VALIDATION_MESSAGES: { [code: string]: string } = { export const VALIDATION_MESSAGES: { [code: string]: string } = {
......
...@@ -21,12 +21,13 @@ ...@@ -21,12 +21,13 @@
* Die sprachspezifischen Genehmigungen und Beschränkungen * Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen. * unter der Lizenz sind dem Lizenztext zu entnehmen.
*/ */
import { AbstractFormService, ProblemDetail, StateResource } from '@alfa-client/tech-shared';
import { UserProfileListResource, UserProfileService } from '@alfa-client/user-profile-shared';
import { Injectable, OnDestroy } from '@angular/core'; import { Injectable, OnDestroy } from '@angular/core';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms'; import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { AbstractFormService, StateResource } from '@alfa-client/tech-shared';
import { UserProfileListResource, UserProfileService } from '@alfa-client/user-profile-shared';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
import { Observable, Subscription } from 'rxjs'; import { Observable, Subscription } from 'rxjs';
import { ValidationMessageCode } from '../../../../../tech-shared/src/lib/validation/tech.validation.messages';
@Injectable() @Injectable()
export class UserProfileSearchFormService extends AbstractFormService implements OnDestroy { export class UserProfileSearchFormService extends AbstractFormService implements OnDestroy {
...@@ -62,11 +63,11 @@ export class UserProfileSearchFormService extends AbstractFormService implements ...@@ -62,11 +63,11 @@ export class UserProfileSearchFormService extends AbstractFormService implements
} }
public setEmptyUserProfileError(): void { public setEmptyUserProfileError(): void {
this.setErrorByApiError(emptyUserProfileError); this.setErrorByProblemDetail(emptyUserProfileError);
} }
public setNoUserProfileFoundError(): void { public setNoUserProfileFoundError(): void {
this.setErrorByApiError(noUserProfileFoundError); this.setErrorByProblemDetail(noUserProfileFoundError);
} }
ngOnDestroy(): void { ngOnDestroy(): void {
...@@ -74,23 +75,34 @@ export class UserProfileSearchFormService extends AbstractFormService implements ...@@ -74,23 +75,34 @@ export class UserProfileSearchFormService extends AbstractFormService implements
} }
} }
const noUserProfileFoundError = { const noUserProfileFoundError: ProblemDetail = {
issues: [ type: null,
title: null,
status: null,
detail: null,
instance: null,
invalidParams: [
{ {
field: 'only.fe.searchBy', name: 'only.fe.searchBy',
message: 'fe_only_validation_bearbeiter_not_exist', reason: ValidationMessageCode.FIELD_ASSIGN_BEARBEITER_NOT_EXIST,
messageCode: 'fe_only_validation_bearbeiter_not_exist', constraintParameters: [],
parameters: [], value: null,
}, },
], ],
}; };
const emptyUserProfileError = {
issues: [ const emptyUserProfileError: ProblemDetail = {
type: null,
title: null,
status: null,
detail: null,
instance: null,
invalidParams: [
{ {
field: 'only.fe.searchBy', name: 'only.fe.searchBy',
message: 'validation_field_empty', reason: ValidationMessageCode.FIELD_EMPTY,
messageCode: 'validation_field_empty', constraintParameters: [],
parameters: [], value: null,
}, },
], ],
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment