Skip to content

Commit

Permalink
Merge pull request #2029 from bcgov/feature/ALCS-2431-add-validation-…
Browse files Browse the repository at this point in the history
…error-fees

ALCS-2431 Validate Admin fee and security amount
  • Loading branch information
fbarreta authored Dec 18, 2024
2 parents fa417dd + a009566 commit ebad124
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ <h4>{{ isEdit ? 'Edit' : 'Create' }} Decision Condition Type</h4>
<div class="condition-field-header">
<div>
<mat-checkbox formControlName="isAdministrativeFeeAmountChecked">Administrative Fee Amount</mat-checkbox>
<app-error-message
*ngIf="!conditionTypeForm.controls['isAdministrativeFeeAmountChecked'].valid"
[message]="'Field contains data, cannot be removed'"
></app-error-message>
</div>

<div class="toggle" *ngIf="conditionTypeForm.get('isAdministrativeFeeAmountChecked')?.value">
Expand Down Expand Up @@ -117,6 +121,10 @@ <h4>{{ isEdit ? 'Edit' : 'Create' }} Decision Condition Type</h4>
<div class="condition-field-header">
<div>
<mat-checkbox formControlName="isSecurityAmountChecked">Security Amount</mat-checkbox>
<app-error-message
*ngIf="!conditionTypeForm.controls['isSecurityAmountChecked'].valid"
[message]="'Field contains data, cannot be removed'"
></app-error-message>
</div>

<div class="toggle" *ngIf="conditionTypeForm.get('isSecurityAmountChecked')?.value">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import { NoticeOfIntentDecisionConditionService } from '../../../../services/not
import { catchError, debounceTime, map, Observable, of, switchMap } from 'rxjs';
import { codeExistsValidator } from '../../../../shared/validators/code-exists-validator';

enum ValidationFields {
Dates,
AdminFee,
SecurityAmount,
}

@Component({
selector: 'app-decision-condition-types-dialog',
templateUrl: './decision-condition-types-dialog.component.html',
Expand Down Expand Up @@ -66,6 +72,8 @@ export class DecisionConditionTypesDialogComponent {
this.data?.content?.isAdministrativeFeeAmountChecked
? this.data.content.isAdministrativeFeeAmountChecked
: false,
[],
[this.conditionAsyncValidator(ValidationFields.AdminFee)],
),
isAdministrativeFeeAmountRequired: new FormControl(
this.data?.content?.isAdministrativeFeeAmountRequired
Expand All @@ -74,11 +82,12 @@ export class DecisionConditionTypesDialogComponent {
),
administrativeFeeAmount: new FormControl(
this.data?.content?.administrativeFeeAmount ? this.data.content.administrativeFeeAmount : '',
[]
),
isDateChecked: new FormControl(
this.data?.content?.isDateChecked ? this.data.content.isDateChecked : false,
[],
[this.conditionAsyncValidator()],
[this.conditionAsyncValidator(ValidationFields.Dates)],
),
isDateRequired: new FormControl(this.data?.content?.isDateRequired ? this.data.content.isDateRequired : false),
dateType: new FormControl(this.data?.content?.dateType),
Expand All @@ -87,10 +96,13 @@ export class DecisionConditionTypesDialogComponent {
),
isSecurityAmountChecked: new FormControl(
this.data?.content?.isSecurityAmountChecked ? this.data.content.isSecurityAmountChecked : false,
[],
[this.conditionAsyncValidator(ValidationFields.SecurityAmount)],
),
isSecurityAmountRequired: new FormControl(
this.data?.content?.isSecurityAmountRequired ? this.data.content.isSecurityAmountRequired : false,
),

});

if (this.isEdit) {
Expand Down Expand Up @@ -142,7 +154,7 @@ export class DecisionConditionTypesDialogComponent {
this.dialogRef.close(true);
}

conditionAsyncValidator(): AsyncValidatorFn {
conditionAsyncValidator(field: ValidationFields): AsyncValidatorFn {
return (control: AbstractControl): Observable<{ [key: string]: any } | null> => {
return of(control.value).pipe(
debounceTime(300),
Expand All @@ -153,7 +165,18 @@ export class DecisionConditionTypesDialogComponent {
return this.conditionService.fetchByTypeCode(this.conditionTypeForm.controls['code'].value);
}),
map((conditions) =>
!control.value && conditions && this.hasAnyDates(conditions) ? { hasConditions: true } : null,
{
switch (field) {
case ValidationFields.Dates:
return !control.value && conditions && this.hasAnyDates(conditions) ? { hasConditions: true } : null;
case ValidationFields.AdminFee:
return !control.value && conditions && this.hasAdminFee(conditions) ? { hasConditions: true } : null;
case ValidationFields.SecurityAmount:
return !control.value && conditions && this.hasSecurityAmount(conditions) ? { hasConditions: true } : null;
default:
return null;
}
},
),
catchError((e) => of({ hasConditions: true })),
);
Expand All @@ -165,4 +188,16 @@ export class DecisionConditionTypesDialogComponent {
): boolean {
return conditions.some((condition) => condition.dates && condition.dates.length > 0);
}

hasAdminFee(
conditions: Partial<ApplicationDecisionConditionDto>[] | Partial<NoticeOfIntentDecisionConditionDto>[],
): boolean {
return conditions.map((c) => c.administrativeFee).filter((f) => f !== null).length > 0;
}

hasSecurityAmount(
conditions: Partial<ApplicationDecisionConditionDto>[] | Partial<NoticeOfIntentDecisionConditionDto>[],
): boolean {
return conditions.map((c) => c.securityAmount).filter((f) => f !== null).length > 0;
}
}

0 comments on commit ebad124

Please sign in to comment.