Skip to content

Commit

Permalink
fix(cb2-8121): general improvements to the type system (#1339)
Browse files Browse the repository at this point in the history
* fix(cb2-8121): general improvements to the type system

* fix(cb2-8121): fix linting error

* fix(cb2-8121): rename type

* fix(cb2-8121): rename type
  • Loading branch information
pbardy2000 authored and JunYanBJSS committed Feb 26, 2024
1 parent 96ac3bf commit 4b04117
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Subject, take, takeUntil } from 'rxjs';
styleUrls: ['./tech-record-amend-vrm-reason.component.scss'],
})
export class AmendVrmReasonComponent implements OnDestroy, OnInit {
techRecord?: NotTrailer;
techRecord?: VehiclesOtherThan<'trl'>;
makeAndModel?: string;

form = new FormGroup({
Expand All @@ -45,7 +45,7 @@ export class AmendVrmReasonComponent implements OnDestroy, OnInit {
if (record?.techRecord_statusCode === 'archived' || !record) {
return this.routerService.navigateBack();
}
this.techRecord = record as NotTrailer;
this.techRecord = record as VehiclesOtherThan<'trl'>;
this.makeAndModel = this.technicalRecordService.getMakeAndModel(record);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { GlobalErrorService } from '@core/components/global-error/global-error.s
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-verb';
import { DynamicFormService } from '@forms/services/dynamic-form.service';
import { mockVehicleTechnicalRecord } from '@mocks/mock-vehicle-technical-record.mock';
import { NotTrailer } from '@models/vehicle-tech-record.model';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action } from '@ngrx/store';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
Expand Down Expand Up @@ -93,7 +92,7 @@ describe('TechRecordChangeVrmComponent', () => {

describe('handleSubmit', () => {
beforeEach(() => {
component.techRecord = mockVehicleTechnicalRecord('psv') as NotTrailer;
component.techRecord = mockVehicleTechnicalRecord('psv') as VehiclesOtherThan<'trl'>;
jest.resetAllMocks();
jest.resetModules();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DynamicFormGroupComponent } from '@forms/components/dynamic-form-group/
import { DynamicFormService } from '@forms/services/dynamic-form.service';
import { CustomFormControl, FormNodeTypes, FormNodeWidth } from '@forms/services/dynamic-form.types';
import { CustomValidators } from '@forms/validators/custom-validators';
import { NotTrailer, VehicleTypes } from '@models/vehicle-tech-record.model';
import { VehicleTypes, VehiclesOtherThan } from '@models/vehicle-tech-record.model';
import { Actions, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { RouterService } from '@services/router/router.service';
Expand All @@ -25,7 +25,7 @@ import { Subject, take, takeUntil } from 'rxjs';
styleUrls: ['./tech-record-amend-vrm.component.scss'],
})
export class AmendVrmComponent implements OnDestroy, OnInit {
techRecord?: NotTrailer;
techRecord?: VehiclesOtherThan<'trl'>;
makeAndModel?: string;
isCherishedTransfer = false;
systemNumber?: string;
Expand Down Expand Up @@ -100,7 +100,7 @@ export class AmendVrmComponent implements OnDestroy, OnInit {
if (record?.techRecord_statusCode === 'archived' || !record) {
return this.routerService.navigateBackTo('../../');
}
this.techRecord = record as NotTrailer;
this.techRecord = record as VehiclesOtherThan<'trl'>;
this.makeAndModel = this.technicalRecordService.getMakeAndModel(record);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class TechRecordSearchTyresComponent implements OnInit {
public form!: CustomFormGroup;
public searchResults: Array<ReferenceDataTyre> | null = null;
public vehicleTechRecord?: V3TechRecordModel;
public viewableTechRecord?: TechRecordType<'hgv'> | TechRecordType<'psv'> | TechRecordType<'trl'>;
public viewableTechRecord?: TechRecordType<'hgv' | 'psv' | 'trl'>;
private params: SearchParams = {};
private pageStart?: number;
private pageEnd?: number;
Expand Down Expand Up @@ -80,7 +80,7 @@ export class TechRecordSearchTyresComponent implements OnInit {
this.params = p;
});
this.technicalRecordService.techRecord$.pipe(take(1)).subscribe((data) => {
this.viewableTechRecord = data as TechRecordType<'hgv'> | TechRecordType<'psv'> | TechRecordType<'trl'>;
this.viewableTechRecord = data as TechRecordType<'hgv' | 'psv' | 'trl'>;
});
this.referenceDataService
.getTyreSearchReturn$()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import {
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { GlobalErrorService } from '@core/components/global-error/global-error.service';
import { HGVAxles } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/hgv/complete';
import { PSVAxles } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/psv/skeleton';
import { TRLAxles } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/trl/complete';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-verb';
import {
TechRecordGETCar,
Expand All @@ -17,11 +14,12 @@ import {
import { FormNode, FormNodeViewTypes } from '@forms/services/dynamic-form.types';
import { VehicleSummary } from '@forms/templates/tech-records/vehicle-summary.template';
import { vehicleTemplateMap } from '@forms/utils/tech-record-constants';
import { VehicleTypes } from '@models/vehicle-tech-record.model';
import { Axles, VehicleTypes } from '@models/vehicle-tech-record.model';
import { Actions, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { RouterService } from '@services/router/router.service';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { UserService } from '@services/user-service/user-service';
import { State } from '@store/index';
import {
clearADRDetailsBeforeUpdate,
Expand All @@ -37,7 +35,6 @@ import {
import {
Subject, combineLatest, map, take, takeUntil,
} from 'rxjs';
import { UserService } from '@services/user-service/user-service';

@Component({
selector: 'app-tech-record-summary-changes',
Expand Down Expand Up @@ -141,20 +138,20 @@ export class TechRecordSummaryChangesComponent implements OnInit, OnDestroy {
return VehicleSummary;
}

get deletedAxles(): HGVAxles[] | TRLAxles[] | PSVAxles[] {
get deletedAxles(): Axles {
if (this.techRecordEdited?.techRecord_vehicleType === 'hgv' && this.techRecordDeletions) {
return Object.values((this.techRecordDeletions as Partial<TechRecordGETHGV>).techRecord_axles ?? {}) as [HGVAxles, ...HGVAxles[]];
return Object.values((this.techRecordDeletions as Partial<TechRecordGETHGV>).techRecord_axles ?? {});
}

if (this.techRecordEdited?.techRecord_vehicleType === 'trl' && this.techRecordDeletions) {
return Object.values((this.techRecordDeletions as Partial<TechRecordGETTRL>).techRecord_axles ?? {}) as [TRLAxles, ...TRLAxles[]];
return Object.values((this.techRecordDeletions as Partial<TechRecordGETTRL>).techRecord_axles ?? {});
}

if (this.techRecordEdited?.techRecord_vehicleType === 'psv' && this.techRecordDeletions) {
return Object.values((this.techRecordDeletions as Partial<TechRecordGETPSV>).techRecord_axles ?? {}) as [PSVAxles, ...PSVAxles[]];
return Object.values((this.techRecordDeletions as Partial<TechRecordGETPSV>).techRecord_axles ?? {});
}

return [] as HGVAxles[] | TRLAxles[] | PSVAxles[];
return [];
}

get sectionTemplatesState$() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { DynamicFormsModule } from '@forms/dynamic-forms.module';
import { mockVehicleTechnicalRecord } from '@mocks/mock-vehicle-technical-record.mock';
import { Roles } from '@models/roles.enum';
import {
NotTrailer, V3TechRecordModel, VehicleTypes,
V3TechRecordModel, VehicleTypes,
VehiclesOtherThan,
} from '@models/vehicle-tech-record.model';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('TechRecordTitleComponent', () => {
expect(vrmField.textContent).not.toContain('TESTV RM4');
});
it('should not create previous-vrm-span if no secondary vrm exists', () => {
delete (mockRecord as NotTrailer).secondaryVrms;
delete (mockRecord as VehiclesOtherThan<'trl'>).secondaryVrms;
fixture.detectChanges();

const vrmField = fixture.debugElement.query(By.css('#previous-vrm-span'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { CustomFormControl, CustomFormGroup, FormNodeTypes } from '@forms/servic
import { CustomValidators } from '@forms/validators/custom-validators';
import { SEARCH_TYPES } from '@models/search-types-enum';
import {
NotTrailer, StatusCodes, V3TechRecordModel, VehicleTypes,
StatusCodes, V3TechRecordModel, VehicleTypes,
VehiclesOtherThan,
} from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { BatchTechnicalRecordService } from '@services/batch-technical-record/batch-technical-record.service';
Expand Down Expand Up @@ -176,9 +177,9 @@ export class CreateTechRecordComponent implements OnChanges {
}

async isVrmUnique() {
(this.techRecord as NotTrailer).primaryVrm = this.form.value.vrmTrm;
(this.techRecord as VehiclesOtherThan<'trl'>).primaryVrm = this.form.value.vrmTrm;
const isVrmUnique = await firstValueFrom(
this.technicalRecordService.isUnique((this.techRecord as NotTrailer).primaryVrm?.replace(/\s+/g, '') ?? '', SEARCH_TYPES.VRM),
this.technicalRecordService.isUnique((this.techRecord as VehiclesOtherThan<'trl'>).primaryVrm?.replace(/\s+/g, '') ?? '', SEARCH_TYPES.VRM),
);
if (!isVrmUnique) {
this.globalErrorService.addError({ error: 'Vrm not unique', anchorLink: 'input-vrm-or-trailer-id' });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { KeyValue } from '@angular/common';
import {
AfterContentInit,
Component, inject, OnDestroy, OnInit,
} from '@angular/core';
import { ReplaySubject, takeUntil } from 'rxjs';
import { FormArray, NgControl } from '@angular/forms';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { BaseControlComponent } from '@forms/components/base-control/base-control.component';
import { CustomControl, CustomFormControl } from '@forms/services/dynamic-form.types';
import { KeyValue } from '@angular/common';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { ReplaySubject, takeUntil } from 'rxjs';

@Component({
selector: 'app-adr-examiner-notes-history',
Expand All @@ -20,14 +20,14 @@ export class AdrExaminerNotesHistoryComponent extends BaseControlComponent imple
destroy$ = new ReplaySubject<boolean>(1);

formArray = new FormArray<CustomFormControl>([]);
currentTechRecord?: TechRecordType<'hgv'> | TechRecordType<'lgv'> | TechRecordType<'trl'> = undefined;
currentTechRecord?: TechRecordType<'hgv' | 'lgv' | 'trl'> = undefined;
technicalRecordService = inject(TechnicalRecordService);
ngOnInit() {
this.formArray.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((changes) => {
this.control?.patchValue(changes, { emitModelToViewChange: true });
});
this.technicalRecordService.techRecord$.pipe(takeUntil(this.destroy$)).subscribe((currentTechRecord) => {
this.currentTechRecord = currentTechRecord as TechRecordType<'hgv'> | TechRecordType<'lgv'> | TechRecordType<'trl'>;
this.currentTechRecord = currentTechRecord as TechRecordType<'hgv' | 'lgv' | 'trl'>;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Subject, debounceTime, takeUntil } from 'rxjs';
styleUrls: ['./approval-type.component.scss'],
})
export class ApprovalTypeComponent implements OnInit, OnChanges, OnDestroy {
@Input() techRecord!: TechRecordType<'trl'> | TechRecordType<'psv'> | TechRecordType<'hgv'>;
@Input() techRecord!: TechRecordType<'hgv' | 'psv' | 'trl'>;
@Input() isEditing = false;
@Output() formChange = new EventEmitter();
@Output() approvalTypeNumberChange = new EventEmitter<string>();
Expand Down
6 changes: 3 additions & 3 deletions src/app/forms/custom-sections/plates/plates.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { GlobalErrorService } from '@core/components/global-error/global-error.s
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { DynamicFormsModule } from '@forms/dynamic-forms.module';
import { Roles } from '@models/roles.enum';
import { HgvOrTrl, VehicleConfigurations } from '@models/vehicle-tech-record.model';
import { VehicleConfigurations } from '@models/vehicle-tech-record.model';
import { StoreModule } from '@ngrx/store';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { UserService } from '@services/user-service/user-service';
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('PlatesComponent', () => {
component = fixture.componentInstance;
router = TestBed.inject(Router);
errorService = TestBed.inject(GlobalErrorService);
component.techRecord = { systemNumber: 'foo', createdTimestamp: 'bar', vin: 'testVin' } as HgvOrTrl;
component.techRecord = { systemNumber: 'foo', createdTimestamp: 'bar', vin: 'testVin' } as TechRecordType<'hgv' | 'trl'>;
store = TestBed.inject(MockStore);
fixture.detectChanges();
});
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('PlatesComponent', () => {
techRecord_vehicleConfiguration: VehicleConfigurations.RIGID,
vin: 'HGVTEST01',
techRecord_axles: [],
} as unknown as HgvOrTrl;
} as unknown as TechRecordType<'hgv' | 'trl'>;

component.techRecord.techRecord_axles = [
{
Expand Down
13 changes: 7 additions & 6 deletions src/app/forms/custom-sections/plates/plates.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { GlobalErrorService } from '@core/components/global-error/global-error.service';
import { HGVAxles, HGVPlates } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/hgv/complete';
import { HGVPlates } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/hgv/complete';
import { TRLPlates } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/trl/complete';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { hgvRequiredFields, trlRequiredFields, tyreRequiredFields } from '@forms/models/plateRequiredFields.model';
import { DynamicFormService } from '@forms/services/dynamic-form.service';
import { CustomFormGroup, FormNodeEditTypes } from '@forms/services/dynamic-form.types';
import { PlatesTemplate } from '@forms/templates/general/plates.template';
import { Roles } from '@models/roles.enum';
import { HgvOrTrl, StatusCodes } from '@models/vehicle-tech-record.model';
import { Axle, StatusCodes } from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { canGeneratePlate, updateScrollPosition } from '@store/technical-records';
import { TechnicalRecordServiceState } from '@store/technical-records/reducers/technical-record-service.reducer';
Expand All @@ -24,7 +25,7 @@ import { Subscription, debounceTime } from 'rxjs';
styleUrls: ['./plates.component.scss'],
})
export class PlatesComponent implements OnInit, OnDestroy, OnChanges {
@Input() techRecord!: HgvOrTrl;
@Input() techRecord!: TechRecordType<'hgv' | 'trl'>;
@Input() isEditing = false;

@Output() formChange = new EventEmitter();
Expand Down Expand Up @@ -142,7 +143,7 @@ export class PlatesComponent implements OnInit, OnDestroy, OnChanges {

private cannotGeneratePlate(plateRequiredFields: string[]): boolean {
const isOneFieldEmpty = plateRequiredFields.some((field) => {
const value = this.techRecord[field as keyof HgvOrTrl];
const value = this.techRecord[field as keyof TechRecordType<'hgv' | 'trl'>];
return value === undefined || value === null || value === '';
});

Expand All @@ -153,11 +154,11 @@ export class PlatesComponent implements OnInit, OnDestroy, OnChanges {
// check tyre fields
const areTyresInvalid = this.techRecord.techRecord_axles?.some((axle) => {
tyreRequiredFields.some((field) => {
const value = axle[field as keyof HGVAxles];
const value = axle[field as keyof Axle<'hgv'>];
return value === undefined || value === null || value === '';
});
// either one of ply rating or load index is required
const plyOrLoad = axle['tyres_plyRating' as keyof HGVAxles] || axle['tyres_dataTrAxles' as keyof HGVAxles];
const plyOrLoad = axle['tyres_plyRating' as keyof Axle<'hgv'>] || axle['tyres_dataTrAxles' as keyof Axle<'hgv'>];
return plyOrLoad === undefined || plyOrLoad === null || plyOrLoad === '';
});

Expand Down
9 changes: 3 additions & 6 deletions src/app/forms/custom-sections/tyres/tyres.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { HttpClientTestingModule } from '@angular/common/http/testing';
import { SimpleChanges } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { DynamicFormsModule } from '@forms/dynamic-forms.module';
import { mockVehicleTechnicalRecord } from '@mocks/mock-vehicle-technical-record.mock';
import { Tyres } from '@models/vehicle-tech-record.model';
import { Axles, Tyres } from '@models/vehicle-tech-record.model';
import { StoreModule } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';
import { ReferenceDataService } from '@services/reference-data/reference-data.service';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { State, initialAppState } from '@store/index';
import { of, throwError } from 'rxjs';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { TyresComponent } from './tyres.component';

const mockReferenceDataService = {
Expand Down Expand Up @@ -259,9 +258,7 @@ describe('TyresComponent', () => {

component.addTyreToTechRecord(tyre, 1);

const axles = component.vehicleTechRecord.techRecord_axles as NonNullable<
(TechRecordType<'psv'> | TechRecordType<'trl'> | TechRecordType<'hgv'>)['techRecord_axles']
>;
const axles = component.vehicleTechRecord.techRecord_axles as Axles;

expect(axles[0]?.tyres_tyreSize).toBe(tyre.tyreSize);
expect(axles[0]?.tyres_dataTrAxles).toBe(tyre.dataTrAxles);
Expand Down
7 changes: 2 additions & 5 deletions src/app/forms/custom-sections/tyres/tyres.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import {
import { ActivatedRoute, Router } from '@angular/router';
import { TyreUseCode as HgvTyreUseCode } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/tyreUseCodeHgv.enum.js';
import { TyreUseCode as TrlTyreUseCode } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/tyreUseCodeTrl.enum.js';
import { HGVAxles } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/hgv/complete';
import { PSVAxles } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/psv/skeleton';
import { TRLAxles } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/trl/complete';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { MultiOptions } from '@forms/models/options.model';
import { DynamicFormService } from '@forms/services/dynamic-form.service';
Expand Down Expand Up @@ -42,7 +39,7 @@ import {
styleUrls: ['./tyres.component.scss'],
})
export class TyresComponent implements OnInit, OnDestroy, OnChanges {
@Input() vehicleTechRecord!: TechRecordType<'psv'> | TechRecordType<'trl'> | TechRecordType<'hgv'>;
@Input() vehicleTechRecord!: TechRecordType<'hgv' | 'psv' | 'trl'>;
@Input() isEditing = false;

@Output() formChange = new EventEmitter();
Expand Down Expand Up @@ -166,7 +163,7 @@ export class TyresComponent implements OnInit, OnDestroy, OnChanges {
&& (vehicleTechRecord.currentValue.techRecord_axles === vehicleTechRecord.previousValue.techRecord_axles))) {
return;
}
vehicleTechRecord.currentValue.techRecord_axles.forEach((axle: HGVAxles | TRLAxles | PSVAxles) => {
vehicleTechRecord.currentValue.techRecord_axles.forEach((axle: Axle) => {
if (axle.tyres_dataTrAxles && axle.weights_gbWeight && axle.axleNumber) {
const weightValue = this.technicalRecordService.getAxleFittingWeightValueFromLoadIndex(
axle.tyres_dataTrAxles.toString(),
Expand Down
Loading

0 comments on commit 4b04117

Please sign in to comment.