Skip to content

Commit

Permalink
fix(cb2-8973): move to resolver approach
Browse files Browse the repository at this point in the history
  • Loading branch information
pbardy2000 committed Aug 30, 2024
1 parent 3f251d1 commit 7970781
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TestTypesTaxonomy } from '@api/test-types';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { TEST_TYPES_GROUP7 } from '@forms/models/testTypeId.enum';
Expand All @@ -8,7 +9,6 @@ import { TestResultModel } from '@models/test-results/test-result.model';
import { TestType, resultOfTestEnum } from '@models/test-types/test-type.model';
import { V3TechRecordModel, VehicleTypes } from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { TestTypesService } from '@services/test-types/test-types.service';
import { TagType, TagTypes } from '@shared/components/tag/tag.component';
import { techRecord } from '@store/technical-records';
Expand All @@ -28,8 +28,8 @@ export class VehicleHeaderComponent {

constructor(
private testTypesService: TestTypesService,
private techRecordService: TechnicalRecordService,
private store: Store
private store: Store,
private activatedRoute: ActivatedRoute
) {}

get test(): TestType | undefined {
Expand Down Expand Up @@ -78,7 +78,7 @@ export class VehicleHeaderComponent {
}

get testCode(): string | undefined {
const testCode = this.testResult?.testTypes[0].testCode;
const testCode = this.testResult?.testTypes[0].testCode || this.activatedRoute.snapshot?.data?.['testCode'];
return testCode ? `(${testCode})` : '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TestRecordCreateRoutes } from '@models/routes.enum';
import { contingencyTestResolver } from 'src/app/resolvers/contingency-test/contingency-test.resolver';
import { defectsTaxonomyResolver } from 'src/app/resolvers/defects-taxonomy/defects-taxonomy.resolver';
import { requiredStandardsResolver } from 'src/app/resolvers/required-standards/required-standards.resolver';
import { testCodeResolver } from 'src/app/resolvers/test-code/test-code.resolver';
import { testStationsResolver } from 'src/app/resolvers/test-stations/test-stations.resolver';
import { testTypeTaxonomyResolver } from 'src/app/resolvers/test-type-taxonomy/test-type-taxonomy.resolver';
import { CreateTestRecordComponent } from './views/create-test-record/create-test-record.component';
Expand Down Expand Up @@ -38,6 +39,7 @@ const routes: Routes = [
TestTypeTaxonomy: testTypeTaxonomyResolver,
defectTaxonomy: defectsTaxonomyResolver,
testStations: testStationsResolver,
testCode: testCodeResolver,
},
data: { title: 'Test details', roles: Roles.TestResultCreateContingency, breadcrumbPreserveQueryParams: true },
canActivate: [RoleGuard],
Expand Down
33 changes: 33 additions & 0 deletions src/app/resolvers/test-code/test-code.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { inject } from '@angular/core';
import { ResolveFn } from '@angular/router';
import { TestTypesService } from '@api/test-types';
import { Store, select } from '@ngrx/store';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { techRecord } from '@store/technical-records';
import { Observable, map, switchMap } from 'rxjs';

export const testCodeResolver: ResolveFn<Observable<string | undefined>> = (route) => {
const store = inject(Store);
const testTypesService = inject(TestTypesService);
const techRecordService = inject(TechnicalRecordService);
const testTypeId: string | undefined = route.queryParams['testType'];

// fetch test code from back-end (because its not provided by reference data)
return store.pipe(
select(techRecord),
switchMap((record) => {
return testTypesService.getTestTypesid(
String(testTypeId),
['defaultTestCode'],
record!.techRecord_vehicleType,
techRecordService.getVehicleSize(record!) as string,
record!.techRecord_vehicleConfiguration!,
record!.techRecord_noOfAxles!,
record!.techRecord_euVehicleCategory!,
techRecordService.getVehicleClassDescription(record!) as string,
techRecordService.getVehicleSubClass(record!)?.[0] as string
);
}),
map((response) => response.defaultTestCode)
);
};
4 changes: 1 addition & 3 deletions src/app/resolvers/test-result/test-result.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export const testResultResolver: ResolveFn<boolean> = () => {
return action$.pipe(
ofType(fetchSelectedTestResultSuccess, fetchSelectedTestResultFailed),
take(1),
map((action) => {
return action.type === fetchSelectedTestResultSuccess.type;
})
map((action) => action.type === fetchSelectedTestResultSuccess.type)
);
};
19 changes: 19 additions & 0 deletions src/app/services/technical-record/technical-record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,23 @@ export class TechnicalRecordService {

return false;
}

getVehicleSize(techRecord: V3TechRecordModel) {
return techRecord.techRecord_vehicleType === 'psv' ? techRecord.techRecord_vehicleSize : undefined;
}

getVehicleClassDescription(techRecord: V3TechRecordModel) {
return techRecord.techRecord_vehicleType === 'psv' ||
techRecord.techRecord_vehicleType === 'hgv' ||
techRecord.techRecord_vehicleType === 'trl' ||
techRecord.techRecord_vehicleType === 'motorcycle'
? techRecord.techRecord_vehicleClass_description
: undefined;
}

getVehicleSubClass(techRecord: V3TechRecordModel) {
return techRecord.techRecord_vehicleType === 'car' || techRecord.techRecord_vehicleType === 'lgv'
? techRecord.techRecord_vehicleSubclass
: undefined;
}
}
18 changes: 1 addition & 17 deletions src/app/store/test-records/effects/test-records.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,22 +280,7 @@ export class TestResultsEffects {
take(1)
)
),
switchMap(([action, editedTestResult, testType, testStation, user]) => {
return this.testTypesService
.getTestTypesid(
String(testType?.id),
['defaultTestCode'],
editedTestResult!.vehicleType,
editedTestResult!.vehicleSize,
editedTestResult!.vehicleConfiguration!,
editedTestResult!.noOfAxles,
editedTestResult!.euVehicleCategory!,
String(editedTestResult!.vehicleClass!),
String(editedTestResult!.vehicleSubclass!)
)
.pipe(map((res) => [action, editedTestResult, testType, testStation, user, res.defaultTestCode] as const));
}),
concatMap(([action, editedTestResult, testTypeTaxonomy, testStation, user, testCode]) => {
concatMap(([action, editedTestResult, testTypeTaxonomy, testStation, user]) => {
const id = action.testType;

const vehicleType = editedTestResult?.vehicleType;
Expand Down Expand Up @@ -326,7 +311,6 @@ export class TestResultsEffects {
});

mergedForms.testTypes[0].testTypeId = id;
mergedForms.testTypes[0].testCode = String(testCode);
mergedForms.testTypes[0].name = testTypeTaxonomy?.name ?? '';
mergedForms.testTypes[0].testTypeName = testTypeTaxonomy?.testTypeName ?? '';
mergedForms.typeOfTest = (testTypeTaxonomy?.typeOfTest as TypeOfTest) ?? TypeOfTest.CONTINGENCY;
Expand Down
3 changes: 0 additions & 3 deletions src/app/store/test-records/reducers/test-records.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ function cleanTestResultPayload(testResult: TestResultModel | undefined) {
}

const testTypes = testResult.testTypes.map((testType, index) => {
// Always remove testCode
delete testType.testCode;

// Remove empty requiredStandards from pass/prs non-voluntary IVA/MVSA tests
if (index === 0) {
const { testTypeId, requiredStandards } = testType;
Expand Down

0 comments on commit 7970781

Please sign in to comment.