From b79ff7f453183b57fb9a58d265dfd80c2c3d4dd8 Mon Sep 17 00:00:00 2001 From: Philip Bardy <146740183+pbardy2000@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:32:35 +0100 Subject: [PATCH] fix(cb2-13791): workaround data migration (#1591) Co-authored-by: Thomas Crawley --- .../adr/__tests__/adr.component.spec.ts | 10 ++++--- .../custom-sections/adr/adr.component.ts | 4 +-- .../adr/__tests__/adr.service.spec.ts | 2 +- src/app/services/adr/adr.service.ts | 26 ++++++++++--------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/app/forms/custom-sections/adr/__tests__/adr.component.spec.ts b/src/app/forms/custom-sections/adr/__tests__/adr.component.spec.ts index 42f653188..ac9f32858 100644 --- a/src/app/forms/custom-sections/adr/__tests__/adr.component.spec.ts +++ b/src/app/forms/custom-sections/adr/__tests__/adr.component.spec.ts @@ -14,6 +14,8 @@ describe('AdrComponent', () => { let component: AdrComponent; let fixture: ComponentFixture; + const hgv = createMockHgv(1234); + beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [AdrComponent], @@ -23,14 +25,14 @@ describe('AdrComponent', () => { { provide: TechnicalRecordService, useValue: { updateEditingTechRecord: jest.fn() } }, { provide: AdrService, - useValue: { carriesDangerousGoods: jest.fn(), determineTankStatementSelect: jest.fn() }, + useValue: { preprocessTechRecord: jest.fn().mockReturnValue(hgv), determineTankStatementSelect: jest.fn() }, }, ], }).compileComponents(); fixture = TestBed.createComponent(AdrComponent); component = fixture.componentInstance; - component.techRecord = createMockHgv(1234); + component.techRecord = hgv; fixture.detectChanges(); }); @@ -39,8 +41,8 @@ describe('AdrComponent', () => { }); describe('ngOnInit', () => { - it('should populate the dangerous goods property', () => { - const spy = jest.spyOn(component.adrService, 'carriesDangerousGoods'); + it('should preprocess the tech record', () => { + const spy = jest.spyOn(component.adrService, 'preprocessTechRecord'); component.ngOnInit(); expect(spy).toHaveBeenCalled(); }); diff --git a/src/app/forms/custom-sections/adr/adr.component.ts b/src/app/forms/custom-sections/adr/adr.component.ts index 965220576..823e9ba43 100644 --- a/src/app/forms/custom-sections/adr/adr.component.ts +++ b/src/app/forms/custom-sections/adr/adr.component.ts @@ -34,12 +34,12 @@ export class AdrComponent implements OnInit, OnDestroy { ngOnInit(): void { this.template = this.isReviewScreen ? AdrSummaryTemplate : AdrTemplate; - this.form = this.dfs.createForm(this.template, this.techRecord) as CustomFormGroup; - this.techRecord.techRecord_adrDetails_dangerousGoods = this.adrService.carriesDangerousGoods(this.techRecord); if (this.techRecord.techRecord_adrDetails_dangerousGoods && !this.isReviewScreen) { this.techRecord.techRecord_adrDetails_tank_tankDetails_tankStatement_select = this.adrService.determineTankStatementSelect(this.techRecord); } + this.techRecord = this.adrService.preprocessTechRecord(this.techRecord); + this.form = this.dfs.createForm(this.template, this.techRecord) as CustomFormGroup; this.handleSubmit(); } diff --git a/src/app/services/adr/__tests__/adr.service.spec.ts b/src/app/services/adr/__tests__/adr.service.spec.ts index 9501a7122..33bb5d99f 100644 --- a/src/app/services/adr/__tests__/adr.service.spec.ts +++ b/src/app/services/adr/__tests__/adr.service.spec.ts @@ -19,7 +19,7 @@ describe('AdrService', () => { describe('carriesDangerousGoods', () => { it('should return true if vehicle carries dangerous goods', () => { expect( - service.carriesDangerousGoods({ techRecord_adrDetails_batteryListNumber: 'number' } as TechRecordType<'hgv'>) + service.carriesDangerousGoods({ techRecord_adrDetails_dangerousGoods: true } as TechRecordType<'hgv'>) ).toBe(true); }); it('should return false if vehicle does not carry dangerous goods', () => { diff --git a/src/app/services/adr/adr.service.ts b/src/app/services/adr/adr.service.ts index a1d6e3305..6f8477a60 100644 --- a/src/app/services/adr/adr.service.ts +++ b/src/app/services/adr/adr.service.ts @@ -22,17 +22,19 @@ export class AdrService { } carriesDangerousGoods(techRecord: TechRecordType<'hgv' | 'lgv' | 'trl'>) { - return ( - techRecord.techRecord_adrDetails_dangerousGoods || - (techRecord.techRecord_adrDetails_dangerousGoods !== false && - Boolean( - Object.keys(techRecord).find( - (key) => - key !== 'techRecord_adrDetails_dangerousGoods' && - key.includes('adrDetails') && - techRecord[key as keyof TechRecordType<'hgv' | 'lgv' | 'trl'>] != null - ) - )) - ); + return techRecord.techRecord_adrDetails_dangerousGoods === true; + } + + preprocessTechRecord(techRecord: TechRecordType<'hgv' | 'lgv' | 'trl'>) { + if (!this.carriesDangerousGoods(techRecord)) { + // Remove all ADR fields + for (const key in techRecord) { + if (key.startsWith('techRecord_adrDetails')) { + delete techRecord[key as keyof TechRecordType<'hgv' | 'lgv' | 'trl'>]; + } + } + } + + return techRecord; } }