Skip to content

Commit

Permalink
fix(cb2-13684): prevent lost of tech record status when filtering tes…
Browse files Browse the repository at this point in the history
…t types (#1557)

* fix(cb2-13684): prevent lost of tech record status when filtering test types

* fix(cb2-13684): use statusCode form tech record instead of off test result

* fix(cb2-13684): fix unit tests
  • Loading branch information
pbardy2000 authored Aug 23, 2024
1 parent f719aff commit 6507b00
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
QueryList,
ViewChild,
ViewChildren,
inject,
} from '@angular/core';
import { GlobalError } from '@core/components/global-error/global-error.interface';
import { GlobalErrorService } from '@core/components/global-error/global-error.service';
Expand All @@ -21,12 +22,11 @@ import { Roles } from '@models/roles.enum';
import { TestResultStatus } from '@models/test-results/test-result-status.enum';
import { TestResultModel } from '@models/test-results/test-result.model';
import { resultOfTestEnum } from '@models/test-types/test-type.model';
import { V3TechRecordModel, VehicleTypes } from '@models/vehicle-tech-record.model';
import { VehicleTypes } from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { RouterService } from '@services/router/router.service';
import { TestRecordsService } from '@services/test-records/test-records.service';
import { DefectsState, filteredDefects } from '@store/defects';
import { selectTechRecord } from '@store/technical-records';
import merge from 'lodash.merge';
import { Observable, map } from 'rxjs';

Expand All @@ -48,16 +48,10 @@ export class BaseTestRecordComponent implements AfterViewInit {

@Output() newTestResult = new EventEmitter<TestResultModel>();

techRecord$: Observable<V3TechRecordModel | undefined>;
constructor(
private defectsStore: Store<DefectsState>,
private routerService: RouterService,
private testRecordsService: TestRecordsService,
private store: Store,
private globalErrorService: GlobalErrorService
) {
this.techRecord$ = this.store.select(selectTechRecord);
}
private defectsStore = inject(Store<DefectsState>);
private routerService = inject(RouterService);
private testRecordsService = inject(TestRecordsService);
private globalErrorService = inject(GlobalErrorService);

ngAfterViewInit(): void {
this.handleFormChange({});
Expand All @@ -77,11 +71,15 @@ export class BaseTestRecordComponent implements AfterViewInit {

latestTest = merge(latestTest, defectsValue, customDefectsValue, requiredStandardsValue, event);

if (latestTest && Object.keys(latestTest).length > 0) {
this.newTestResult.emit(latestTest as TestResultModel);
if (this.shouldUpdateTest(latestTest)) {
this.newTestResult.emit(latestTest);
}
}

shouldUpdateTest(latestTest: unknown): latestTest is TestResultModel {
return !!latestTest && Object.keys(latestTest).length > 0;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
validateEuVehicleCategory(_event: unknown) {
this.sections?.forEach((section) => {
Expand Down
73 changes: 53 additions & 20 deletions src/app/store/test-types/selectors/test-types.selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { TestTypeCategory, TestTypesTaxonomy } from '@api/test-types';
import { TechRecordSearchSchema } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/search';
import { TestResultModel } from '@models/test-results/test-result.model';
import { StatusCodes, VehicleSubclass, VehicleTypes } from '@models/vehicle-tech-record.model';
import { StatusCodes, V3TechRecordModel, VehicleSubclass, VehicleTypes } from '@models/vehicle-tech-record.model';
import { selectTestType, selectTestTypesByVehicleType, sortedTestTypes } from './test-types.selectors';

describe('selectors', () => {
const techRecord = { techRecord_statusCode: 'current' } as V3TechRecordModel;

describe('selectTestTypesByVehicleType', () => {
it('test with no data', () => {
const selector = selectTestTypesByVehicleType.projector([], { vehicleType: 'psv' } as TestResultModel, []);
const selector = selectTestTypesByVehicleType.projector(
[],
{ vehicleType: 'psv' } as TestResultModel,
[],
techRecord
);
expect(selector).toHaveLength(0);
});

Expand All @@ -27,7 +34,12 @@ describe('selectors', () => {
{ forVehicleType: ['psv'], forEuVehicleCategory: ['m2'] },
{ forVehicleType: ['psv', 'hgv'], nextTestTypesOrCategories: [{ forVehicleType: ['psv', 'hgv'] }] },
] as TestTypesTaxonomy;
const selector = selectTestTypesByVehicleType.projector(testTypes, { vehicleType: 'psv' } as TestResultModel, []);
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleType: 'psv' } as TestResultModel,
[],
techRecord
);
expect(selector).toHaveLength(3);
expect(selector).toEqual(expectedTestTypes);
});
Expand Down Expand Up @@ -102,7 +114,8 @@ describe('selectors', () => {
vehicleType: VehicleTypes.TRL,
statusCode: StatusCodes.PROVISIONAL,
} as TestResultModel,
techRecordHistorys
techRecordHistorys,
{ ...techRecord, techRecord_statusCode: StatusCodes.PROVISIONAL }
);
expect(selector).toEqual(expectedTestTypes);

Expand All @@ -112,7 +125,8 @@ describe('selectors', () => {
vehicleType: VehicleTypes.TRL,
statusCode: StatusCodes.CURRENT,
} as TestResultModel,
techRecordHistorys
techRecordHistorys,
techRecord
);

expect(selectorAdditional).toEqual(additionalExpectedTestTypes);
Expand Down Expand Up @@ -140,7 +154,8 @@ describe('selectors', () => {
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ euVehicleCategory: 'm1' } as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(3);
expect(selector).toEqual(expectedTestTypes);
Expand Down Expand Up @@ -170,7 +185,8 @@ describe('selectors', () => {
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleSize: 'small' } as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(4);
expect(selector).toEqual(expectedTestTypes);
Expand Down Expand Up @@ -201,15 +217,17 @@ describe('selectors', () => {
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleConfiguration: 'rigid' } as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(4);
expect(selector).toEqual(expectedTestTypes);

const selectorAdditional = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleConfiguration: 'articulated' } as TestResultModel,
[]
[],
techRecord
);
expect(selectorAdditional).toHaveLength(5);
});
Expand All @@ -236,14 +254,20 @@ describe('selectors', () => {
},
] as TestTypesTaxonomy;

const selector = selectTestTypesByVehicleType.projector(testTypes, { noOfAxles: 4 } as TestResultModel, []);
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ noOfAxles: 4 } as TestResultModel,
[],
techRecord
);
expect(selector).toHaveLength(4);
expect(selector).toEqual(expectedTestTypes);

const selectorAdditional = selectTestTypesByVehicleType.projector(
testTypes,
{ noOfAxles: 2 } as TestResultModel,
[]
[],
techRecord
);
expect(selectorAdditional).toHaveLength(5);
});
Expand Down Expand Up @@ -273,15 +297,17 @@ describe('selectors', () => {
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleClass: { code: 's' } } as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(4);
expect(selector).toEqual(expectedTestTypes);

const selectorAdditional = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleClass: { code: 'n' } } as TestResultModel,
[]
[],
techRecord
);
expect(selectorAdditional).toHaveLength(5);
});
Expand Down Expand Up @@ -313,7 +339,8 @@ describe('selectors', () => {
{
vehicleClass: { description: 'motorbikes up to 200cc' },
} as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(4);
expect(selector).toEqual(expectedTestTypes);
Expand All @@ -323,7 +350,8 @@ describe('selectors', () => {
{
vehicleClass: { description: '3 wheelers' },
} as TestResultModel,
[]
[],
techRecord
);
expect(selectorAdditional).toHaveLength(5);
});
Expand Down Expand Up @@ -355,15 +383,17 @@ describe('selectors', () => {
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleSubclass: [VehicleSubclass.L] } as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(4);
expect(selector).toEqual(expectedTestTypes);

const selectorAdditional = selectTestTypesByVehicleType.projector(
testTypes,
{ vehicleSubclass: [VehicleSubclass.C] } as TestResultModel,
[]
[],
techRecord
);
expect(selectorAdditional).toHaveLength(5);
});
Expand Down Expand Up @@ -395,15 +425,17 @@ describe('selectors', () => {
const selector = selectTestTypesByVehicleType.projector(
testTypes,
{ numberOfWheelsDriven: 4 } as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(5);
expect(selector).toEqual(expectedTestTypes);

const selectorAdditional = selectTestTypesByVehicleType.projector(
testTypes,
{ numberOfWheelsDriven: 2 } as TestResultModel,
[]
[],
techRecord
);
expect(selectorAdditional).toHaveLength(5);
});
Expand Down Expand Up @@ -450,7 +482,8 @@ describe('selectors', () => {
euVehicleCategory: 'm1',
vehicleSize: 'small',
} as TestResultModel,
[]
[],
techRecord
);
expect(selector).toHaveLength(3);
expect(selector).toEqual(expectedTestTypes);
Expand Down
19 changes: 11 additions & 8 deletions src/app/store/test-types/selectors/test-types.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { TestTypeCategory } from '@api/test-types/model/testTypeCategory';
import { TestTypesTaxonomy } from '@api/test-types/model/testTypesTaxonomy';
import { TechRecordSearchSchema } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/search';
import { TestResultModel } from '@models/test-results/test-result.model';
import { StatusCodes, VehicleSubclass } from '@models/vehicle-tech-record.model';
import { StatusCodes, V3TechRecordModel, VehicleSubclass } from '@models/vehicle-tech-record.model';
import { createSelector } from '@ngrx/store';
import { selectTechRecordHistory } from '@store/technical-records';
import { selectTechRecord, selectTechRecordHistory } from '@store/technical-records';
import { toEditOrNotToEdit } from '@store/test-records';
import { testTypesAdapter, testTypesFeatureState } from '../reducers/test-types.reducer';

Expand All @@ -29,11 +29,12 @@ export const selectTestTypesByVehicleType = createSelector(
selectAllTestTypes,
toEditOrNotToEdit,
selectTechRecordHistory,
(testTypes, testResult, techRecordHistory) => {
selectTechRecord,
(testTypes, testResult, techRecordHistory, techRecord) => {
const hasCurrentRecordInHistory = techRecordHistory ? currentRecordInHistoryCheck(techRecordHistory) : false;

if (testResult) {
return filterTestTypes(testTypes, testResult, hasCurrentRecordInHistory);
if (testResult && techRecord) {
return filterTestTypes(testTypes, testResult, hasCurrentRecordInHistory, techRecord);
}
return [];
}
Expand Down Expand Up @@ -110,11 +111,11 @@ function currentRecordInHistoryCheck(techRecordHistorys: TechRecordSearchSchema[
function filterTestTypes(
testTypes: TestTypesTaxonomy,
testResult: TestResultModel,
hasCurrentRecordInHistory: boolean
hasCurrentRecordInHistory: boolean,
techRecord: V3TechRecordModel
): TestTypesTaxonomy {
const {
vehicleType,
statusCode,
euVehicleCategory,
vehicleSize,
vehicleConfiguration,
Expand All @@ -123,6 +124,7 @@ function filterTestTypes(
vehicleSubclass,
numberOfWheelsDriven,
} = testResult;
const { techRecord_statusCode: statusCode } = techRecord;
const filterFirstTestIds: string[] = ['41', '95', '82', '83', '119', '120', '65', '66', '67', '103', '104', '51'];
return (
testTypes
Expand Down Expand Up @@ -183,7 +185,8 @@ function filterTestTypes(
newTestType.nextTestTypesOrCategories = filterTestTypes(
newTestType.nextTestTypesOrCategories!,
testResult,
hasCurrentRecordInHistory
hasCurrentRecordInHistory,
techRecord
);
}

Expand Down

0 comments on commit 6507b00

Please sign in to comment.