Skip to content

Commit

Permalink
feat(cb2-11930): ensure certificate number is sent to backend if it e…
Browse files Browse the repository at this point in the history
…xists on MVSA IVA tests (#1482)

* feat(cb2-11930): ensure certificate number is sent to backend if its exists on MVSA IVA tests

* feat(cb2-11930): fix linting issue
  • Loading branch information
pbardy2000 authored May 2, 2024
1 parent ab070d6 commit 7cc436a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
21 changes: 20 additions & 1 deletion src/app/services/test-records/test-records.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import {
CompleteTestResults, DefaultService as CreateTestResultsService, GetTestResultsService, UpdateTestResultsService,
} from '@api/test-results';
import { TEST_TYPES } from '@forms/models/testTypeId.enum';
import { TEST_TYPES, TEST_TYPES_GROUP1_SPEC_TEST, TEST_TYPES_GROUP5_SPEC_TEST } from '@forms/models/testTypeId.enum';
import { FormNode } from '@forms/services/dynamic-form.types';
import { contingencyTestTemplates } from '@forms/templates/test-records/create-master.template';
import { masterTpl } from '@forms/templates/test-records/master.template';
Expand Down Expand Up @@ -139,6 +139,25 @@ export class TestRecordsService {
return this.store.dispatch(cleanTestResult());
}

prepareTestResultForAmendment(testResults: TestResultModel[], testResult: TestResultModel): TestResultModel {
const lastIvaOrMsvaTest = testResults.find((test) => {
const testType = test?.testTypes[0];
const testTypeId = testType?.testTypeId ?? '';
const isIVAorMSVATest = TEST_TYPES_GROUP1_SPEC_TEST.includes(testTypeId) || TEST_TYPES_GROUP5_SPEC_TEST.includes(testTypeId);

return isIVAorMSVATest;
});

if (!lastIvaOrMsvaTest) {
return testResult;
}

// If certificateNumber is falsy, then use the last IVA or MSVA test certificate number
testResult.testTypes[0].certificateNumber ??= lastIvaOrMsvaTest.testTypes[0].certificateNumber;

return testResult;
}

static getTestTypeGroup(testTypeId: string): string | undefined {
// eslint-disable-next-line no-restricted-syntax
for (const groupName in TEST_TYPES) {
Expand Down
25 changes: 20 additions & 5 deletions src/app/store/test-records/effects/test-records.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TestStationType } from '@models/test-stations/test-station-type.enum';
import { StatusCodes } from '@models/vehicle-tech-record.model';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store, select } from '@ngrx/store';
import { FeatureToggleService } from '@services/feature-toggle-service/feature-toggle-service';
import { TechnicalRecordHttpService } from '@services/technical-record-http/technical-record-http.service';
import { TestRecordsService } from '@services/test-records/test-records.service';
import { UserService } from '@services/user-service/user-service';
Expand All @@ -24,7 +25,6 @@ import {
catchError, concatMap, delay, filter, map, mergeMap, of, switchMap, take,
withLatestFrom,
} from 'rxjs';
import { FeatureToggleService } from '@services/feature-toggle-service/feature-toggle-service';
import {
contingencyTestTypeSelected,
createTestResult,
Expand All @@ -43,7 +43,12 @@ import {
updateTestResultFailed,
updateTestResultSuccess,
} from '../actions/test-records.actions';
import { isTestTypeOldIvaOrMsva, selectedTestResultState, testResultInEdit } from '../selectors/test-records.selectors';
import {
isTestTypeOldIvaOrMsva,
selectAllTestResultsInDateOrder,
selectedTestResultState,
testResultInEdit,
} from '../selectors/test-records.selectors';

@Injectable()
export class TestResultsEffects {
Expand Down Expand Up @@ -127,11 +132,21 @@ export class TestResultsEffects {
ofType(updateTestResult),
mergeMap((action) =>
of(action.value).pipe(
withLatestFrom(this.userService.name$, this.userService.id$, this.userService.userEmail$, this.store.pipe(select(selectRouteNestedParams))),
withLatestFrom(
this.userService.name$,
this.userService.id$,
this.userService.userEmail$,
this.store.select(selectRouteNestedParams),
this.store.select(selectAllTestResultsInDateOrder),
),
take(1),
)),
mergeMap(([testResult, name, id, userEmail, { systemNumber }]) => {
return this.testRecordsService.saveTestResult(systemNumber, { name, id, userEmail }, testResult).pipe(
mergeMap(([testResult, name, id, userEmail, { systemNumber }, testResults]) => {
return this.testRecordsService.saveTestResult(
systemNumber,
{ name, id, userEmail },
this.testRecordsService.prepareTestResultForAmendment(testResults, testResult),
).pipe(
take(1),
map((responseBody) => updateTestResultSuccess({ payload: { id: responseBody.testResultId, changes: responseBody } })),
catchError((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const selectTestResultsEntities = createSelector(testResultsFeatureState,
// select the array of tests result
export const selectAllTestResults = createSelector(testResultsFeatureState, (state) => selectAll(state));

// select the array of tests results in order of createdAt (most recent to oldest)
export const selectAllTestResultsInDateOrder = createSelector(selectAllTestResults, (testResults) => testResults.sort(byDate));

// select the total test results count
export const selectTestResultsTotal = createSelector(testResultsFeatureState, (state) => selectTotal(state));

Expand Down Expand Up @@ -53,8 +56,7 @@ export const testResultLoadingState = createSelector(testResultsFeatureState, (s
export const selectDefectData = createSelector(selectedTestResultState, (testResult) => getDefectFromTestResult(testResult));

export const isTestTypeOldIvaOrMsva = createSelector(toEditOrNotToEdit, (testResult) => {
return !!testResult?.testTypes[0]?.customDefects?.length
&& !!testResult?.testTypes[0]?.customDefects?.every((defect) => !!defect.referenceNumber);
return !!testResult?.testTypes[0]?.customDefects?.length && !!testResult?.testTypes[0]?.customDefects?.every((defect) => !!defect.referenceNumber);
});

export const selectedTestSortedAmendmentHistory = createSelector(selectedTestResultState, (testResult) => {
Expand Down

0 comments on commit 7cc436a

Please sign in to comment.