Skip to content

Commit

Permalink
ALCS-2453 Handle unique descriptions on condition types
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarreta committed Jan 6, 2025
1 parent 39b998e commit a34af0a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ <h4>{{ isEdit ? 'Edit' : 'Create' }} Decision Condition Type</h4>
<mat-label>Description</mat-label>
<input required matInput id="description" formControlName="description" name="description" />
</mat-form-field>
<div *ngIf="conditionTypeForm.get('description')?.hasError('descriptionExists')">
<app-error-message message="Description already in use or deteled, pick a different description."></app-error-message>
</div>
</div>

<div class="dialog-field full-width">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { ApplicationDecisionConditionService } from '../../../../services/application/decision/application-decision-v2/application-decision-condition/application-decision-condition.service';
import { NoticeOfIntentDecisionConditionService } from '../../../../services/notice-of-intent/decision-v2/notice-of-intent-decision-condition/notice-of-intent-decision-condition.service';
import { catchError, debounceTime, map, Observable, of, switchMap } from 'rxjs';
import { codeExistsValidator } from '../../../../shared/validators/code-exists-validator';
import { codeExistsValidator, descriptionExistsValidator } from '../../../../shared/validators/code-exists-validator';

enum ValidationFields {
Dates,
Expand Down Expand Up @@ -54,6 +54,7 @@ export class DecisionConditionTypesDialogComponent {
this.conditionTypeForm = new FormGroup({
description: new FormControl(this.data?.content?.description ? this.data.content.description : '', [
Validators.required,
descriptionExistsValidator(this.data?.existingDescriptions ? this.data.existingDescriptions : []),
]),
label: new FormControl(this.data?.content?.label ? this.data.content.label : '', [Validators.required]),
code: new FormControl(this.data?.content?.code ? this.data.content.code : '', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class DecisionConditionTypesComponent implements OnInit {

decisionConditionTypeDtos: ApplicationDecisionConditionTypeDto[] | NoticeOfIntentDecisionConditionTypeDto[] = [];
decisionConditionTypeCodeDtos: string[] = [];
decisionConditionTypeDescriptionDtos: string[] = [];
displayedColumns: string[] = ['label', 'description', 'code', 'isActive', 'actions'];

constructor(
Expand All @@ -45,6 +46,7 @@ export class DecisionConditionTypesComponent implements OnInit {
if (!this.service) return;
this.decisionConditionTypeDtos = await this.service.fetch();
this.decisionConditionTypeCodeDtos = await this.service.fetchCodesWithDeleted();
this.decisionConditionTypeDescriptionDtos = this.decisionConditionTypeDtos.map((d) => d.description);
}

async onCreate() {
Expand All @@ -57,6 +59,7 @@ export class DecisionConditionTypesComponent implements OnInit {
service: this.service,
conditionService: this.conditionService,
existingCodes: this.decisionConditionTypeCodeDtos,
existingDescriptions: this.decisionConditionTypeDescriptionDtos,
},
});
dialog.beforeClosed().subscribe(async (result) => {
Expand All @@ -77,6 +80,7 @@ export class DecisionConditionTypesComponent implements OnInit {
conditionService: this.conditionService,
content: dto,
existingCodes: this.decisionConditionTypeCodeDtos,
existingDescriptions: this.decisionConditionTypeDescriptionDtos,
},
});
dialog.beforeClosed().subscribe(async (result) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface DecisionDialogDataInterface {
conditionService: ApplicationDecisionConditionService | NoticeOfIntentDecisionConditionService;
content: ApplicationDecisionConditionTypeDto;
existingCodes: string[];
existingDescriptions: string[];
}
11 changes: 11 additions & 0 deletions alcs-frontend/src/app/shared/validators/code-exists-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export function codeExistsValidator(existingCodes: string[]): ValidatorFn {
};
}

export function descriptionExistsValidator(existingDescriptions: string[]): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (!control.value) return null;
const inputValue = control.value.toLowerCase();
if (existingDescriptions.some((description) => description.toLowerCase() === inputValue)) {
return { descriptionExists: true };
}
return null;
};
}

export function codeExistsDirectiveValidator(model: NgModel, existingCodes: string[], code: string) {
const existingErrors = model.control.errors || {};
const isExisting = existingCodes.includes(code.toLowerCase());
Expand Down

0 comments on commit a34af0a

Please sign in to comment.