Skip to content

Commit

Permalink
fix(cb2-13791): workaround data migration (#1591)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Crawley <[email protected]>
  • Loading branch information
pbardy2000 and tomcrawleyy authored Sep 16, 2024
1 parent d86db95 commit b79ff7f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('AdrComponent', () => {
let component: AdrComponent;
let fixture: ComponentFixture<AdrComponent>;

const hgv = createMockHgv(1234);

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [AdrComponent],
Expand All @@ -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();
});

Expand All @@ -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();
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/forms/custom-sections/adr/adr.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/services/adr/__tests__/adr.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
26 changes: 14 additions & 12 deletions src/app/services/adr/adr.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit b79ff7f

Please sign in to comment.