Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cb2-14944): show body declaration under specific conditions #1629

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/app/forms/templates/general/adr.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const AdrTemplate: FormNode = {
{ value: false, label: 'No' },
],
validators: [
{ name: ValidatorNames.SetBodyDeclarationVisibility },
{ name: ValidatorNames.ShowGroupsWhenEqualTo, args: { values: [true], groups: ['dangerous_goods'] } },
{
name: ValidatorNames.HideGroupsWhenEqualTo,
Expand Down Expand Up @@ -129,6 +130,7 @@ export const AdrTemplate: FormNode = {
hide: true,
options: getOptionsFromEnum(ADRBodyType),
validators: [
{ name: ValidatorNames.SetBodyDeclarationVisibility },
{
name: ValidatorNames.RequiredIfEquals,
args: { sibling: 'techRecord_adrDetails_dangerousGoods', value: [true] },
Expand Down Expand Up @@ -207,20 +209,7 @@ export const AdrTemplate: FormNode = {
groups: ['adr_details', 'dangerous_goods'],
hide: true,
validators: [
{
name: ValidatorNames.ShowGroupsWhenIncludes,
args: {
values: [ADRDangerousGood, ADRDangerousGood.EXPLOSIVES_TYPE_3],
groups: ['body_declaration'],
},
},
{
name: ValidatorNames.HideGroupsWhenExcludes,
args: {
values: [ADRDangerousGood.EXPLOSIVES_TYPE_3],
groups: ['body_declaration'],
},
},
{ name: ValidatorNames.SetBodyDeclarationVisibility },
{
name: ValidatorNames.ShowGroupsWhenIncludes,
args: {
Expand All @@ -246,7 +235,7 @@ export const AdrTemplate: FormNode = {
label: 'Body declaration',
type: FormNodeTypes.CONTROL,
editType: FormNodeEditTypes.RADIO,
groups: ['body_declaration', 'adr_details', 'dangerous_goods'],
groups: ['adr_details', 'dangerous_goods'],
hide: true,
options: [
{ value: ADRBodyDeclarationTypes.PRE_1ST_JULY_2005, label: ADRBodyDeclarationTypes.PRE_1ST_JULY_2005 },
Expand Down
90 changes: 90 additions & 0 deletions src/app/forms/validators/adr/__tests__/adr.validators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ADRBodyType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/adrBodyType.enum.js';
import { ADRDangerousGood } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/adrDangerousGood.enum.js';
import { CustomFormControl, CustomFormGroup, FormNodeTypes } from '@services/dynamic-forms/dynamic-form.types';
import { AdrValidators } from '../adr.validators';

Expand Down Expand Up @@ -156,4 +158,92 @@ describe('ADR Validators', () => {
expect(AdrValidators.validateProductListUNNumbers(b)).toBeNull();
});
});

describe('setBodyDeclarationVisibility', () => {
const form = new CustomFormGroup(
{ name: 'ADRForm', type: FormNodeTypes.GROUP },
{
techRecord_adrDetails_dangerousGoods: new CustomFormControl({
name: 'techRecord_adrDetails_dangerousGoods',
type: FormNodeTypes.CONTROL,
value: false,
}),
techRecord_adrDetails_permittedDangerousGoods: new CustomFormControl({
name: 'techRecord_adrDetails_dangerousGoodsExplosives',
type: FormNodeTypes.CONTROL,
value: [],
}),
techRecord_adrDetails_vehicleDetails_type: new CustomFormControl({
name: 'techRecord_adrDetails_vehicleDetails_type',
type: FormNodeTypes.CONTROL,
value: null,
}),
techRecord_adrDetails_bodyDeclaration_type: new CustomFormControl({
name: 'techRecord_adrDetails_bodyDeclaration_type',
type: FormNodeTypes.CONTROL,
value: null,
}),
}
);

beforeEach(() => {
form.reset();
});

it('should hide body declaration when dangerous goods is false', () => {
form.patchValue({
techRecord_adrDetails_dangerousGoods: false,
techRecord_adrDetails_permittedDangerousGoods: [ADRDangerousGood.EXPLOSIVES_TYPE_3],
techRecord_adrDetails_vehicleDetails_type: ADRBodyType.RIGID_BOX_BODY,
techRecord_adrDetails_bodyDeclaration_type: null,
});

const validator = AdrValidators.setBodyDeclarationVisibility();
const control = form.get('techRecord_adrDetails_bodyDeclaration_type') as CustomFormControl;
validator(control);
expect(control.meta.hide).toBe(true);
});

it('should hide body declaration when explosives type 3 is not a selected permitted dangerous good', () => {
form.patchValue({
techRecord_adrDetails_dangerousGoods: true,
techRecord_adrDetails_permittedDangerousGoods: [ADRDangerousGood.EXPLOSIVES_TYPE_2, ADRDangerousGood.AT],
techRecord_adrDetails_vehicleDetails_type: ADRBodyType.RIGID_BOX_BODY,
techRecord_adrDetails_bodyDeclaration_type: null,
});

const validator = AdrValidators.setBodyDeclarationVisibility();
const control = form.get('techRecord_adrDetails_bodyDeclaration_type') as CustomFormControl;
validator(control);
expect(control.meta.hide).toBe(true);
});

it('should hide body declaration when carry explosives type 3, but the ADR body type is not applicable', () => {
form.patchValue({
techRecord_adrDetails_dangerousGoods: true,
techRecord_adrDetails_permittedDangerousGoods: [ADRDangerousGood.EXPLOSIVES_TYPE_3],
techRecord_adrDetails_vehicleDetails_type: ADRBodyType.ARTIC_TRACTOR,
techRecord_adrDetails_bodyDeclaration_type: null,
});

const validator = AdrValidators.setBodyDeclarationVisibility();
const control = form.get('techRecord_adrDetails_bodyDeclaration_type') as CustomFormControl;
validator(control);
expect(control.meta.hide).toBe(true);
});

it('should show body declaration when carrying dangerous goods, including explosives type 3, and is of an applicable ADR body type', () => {
form.patchValue({
techRecord_adrDetails_dangerousGoods: true,
techRecord_adrDetails_permittedDangerousGoods: [ADRDangerousGood.EXPLOSIVES_TYPE_3],
techRecord_adrDetails_vehicleDetails_type: ADRBodyType.RIGID_BOX_BODY,
techRecord_adrDetails_bodyDeclaration_type: null,
});

const validator = AdrValidators.setBodyDeclarationVisibility();
const control = form.get('techRecord_adrDetails_bodyDeclaration_type') as CustomFormControl;
validator(control);
expect(control.meta.hide).toBe(false);
});
});
});
44 changes: 43 additions & 1 deletion src/app/forms/validators/adr/adr.validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ValidationErrors } from '@angular/forms';
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import { ADRBodyType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/adrBodyType.enum.js';
import { ADRDangerousGood } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/adrDangerousGood.enum.js';
import { CustomFormControl } from '@services/dynamic-forms/dynamic-form.types';

export class AdrValidators {
Expand Down Expand Up @@ -67,4 +69,44 @@ export class AdrValidators {

return null;
};

/**
* Body declaration should only be visible when:
* - Carries dangerous goods is true
* - AND The ADR body type is one of the following:
* > Rigid box body
* > Full drawbar box body
* > Centre axle box body
* > Semi trailer box body
* - AND The permitted dangerous good 'Explosive (type 3)' is selected
*
* @param control
*/
static setBodyDeclarationVisibility = (): ValidatorFn => {
return (control: AbstractControl) => {
const bodyDeclaration = control.parent?.get('techRecord_adrDetails_bodyDeclaration_type');

if (bodyDeclaration instanceof CustomFormControl && bodyDeclaration.meta) {
const carriesDangerousGoods = control.parent?.get('techRecord_adrDetails_dangerousGoods')?.value;
const permittedDangerousGoods = control.parent?.get('techRecord_adrDetails_permittedDangerousGoods')?.value;
const adrBodyType = control.parent?.get('techRecord_adrDetails_vehicleDetails_type')?.value;

if (
carriesDangerousGoods === true &&
Array.isArray(permittedDangerousGoods) &&
permittedDangerousGoods.includes(ADRDangerousGood.EXPLOSIVES_TYPE_3) &&
(adrBodyType === ADRBodyType.RIGID_BOX_BODY ||
adrBodyType === ADRBodyType.FULL_DRAWBAR_BOX_BODY ||
adrBodyType === ADRBodyType.CENTRE_AXLE_BOX_BODY ||
adrBodyType === ADRBodyType.SEMI_TRAILER_BOX_BODY)
) {
bodyDeclaration.meta.hide = false;
} else {
bodyDeclaration.meta.hide = true;
}
}

return null;
};
};
}
1 change: 1 addition & 0 deletions src/app/models/validators.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ export enum ValidatorNames {
DateIsInvalid = 'dateIsInvalid',
MinArrayLengthIfNotEmpty = 'minArrayLengthIfNotEmpty',
IssueRequired = 'issueRequired',
SetBodyDeclarationVisibility = 'setBodyDeclarationVisibility',
}
3 changes: 2 additions & 1 deletion src/app/services/dynamic-forms/dynamic-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { AsyncValidatorFn, FormArray, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { GlobalError } from '@core/components/global-error/global-error.interface';
import { ErrorMessageMap } from '@forms/utils/error-message-map';
// eslint-disable-next-line import/no-cycle
import { AdrValidators } from '@forms/validators/adr/adr.validators';
import { CustomAsyncValidators } from '@forms/validators/custom-async-validator/custom-async-validators';
import {
CustomValidators,
Expand Down Expand Up @@ -97,6 +97,7 @@ export class DynamicFormService {
[ValidatorNames.MinArrayLengthIfNotEmpty]: (args: { minimumLength: number; message: string }) =>
CustomValidators.minArrayLengthIfNotEmpty(args.minimumLength, args.message),
[ValidatorNames.IssueRequired]: () => CustomValidators.issueRequired(),
[ValidatorNames.SetBodyDeclarationVisibility]: () => AdrValidators.setBodyDeclarationVisibility(),
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading