Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cb2-12580): add cert-gen-init logic and types refactor #202

Merged
merged 16 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21,680 changes: 8,658 additions & 13,022 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@
"@aws-sdk/client-lambda": "^3.564.0",
"@aws-sdk/client-s3": "^3.564.0",
"@aws-sdk/client-secrets-manager": "^3.564.0",
"@aws-sdk/util-dynamodb": "^3.704.0",
"@dvsa/cvs-feature-flags": "^0.13.0",
"@dvsa/cvs-type-definitions": "^7.6.3",
"@dvsa/cvs-microservice-common": "^1.2.4",
"@dvsa/cvs-type-definitions": "^7.8.0",
"@smithy/types": "^2.12.0",
"@smithy/util-utf8": "^2.3.0",
"aws-lambda": "^1.0.5",
Expand Down
5 changes: 2 additions & 3 deletions src/certificate/CertificatePayloadGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import merge from 'lodash.merge';
import { Service } from 'typedi';
import { ITestResult } from '../models';
import { ICertificatePayload } from '../models';
import { ICertificatePayload, TestResultSchemaTestTypesAsObject } from '../models';
import { CERTIFICATE_DATA } from '../models/Enums';
import { CertificatePayloadStateBag } from './CertificatePayloadStateBag';
import { ICertificatePayloadCommand } from './ICertificatePayloadCommand';
Expand Down Expand Up @@ -58,7 +57,7 @@ export class CertificatePayloadGenerator implements ICertificatePayloadCommand {
* @param isWelsh - the boolean value whether the atf where test was conducted resides in Wales
*/
public async generateCertificateData(
testResult: ITestResult,
testResult: TestResultSchemaTestTypesAsObject,
type: CERTIFICATE_DATA,
isWelsh = false
): Promise<ICertificatePayload> {
Expand Down
4 changes: 2 additions & 2 deletions src/certificate/CertificatePayloadStateBag.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ITestResult } from '../models';
import { TestResultSchemaTestTypesAsObject } from '../models';
import { CERTIFICATE_DATA } from '../models/Enums';

export type CertificatePayloadStateBag = {
type: CERTIFICATE_DATA;
isWelsh: boolean;
testResult: ITestResult;
testResult: TestResultSchemaTestTypesAsObject;
};
7 changes: 4 additions & 3 deletions src/certificate/commands/AdrCertificateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class AdrCertificateCommand extends BasePayloadCommand {
}

const { testResult } = this.state;
const testTypes = testResult.testTypes;

const [adrDetails, makeAndModel] = await Promise.all([
this.techRecordService.getAdrDetails(testResult),
Expand Down Expand Up @@ -50,10 +51,10 @@ export class AdrCertificateCommand extends BasePayloadCommand {
TankCode: adrDetails?.techRecord_adrDetails_tank_tankDetails_tankCode,
SpecialProvisions: adrDetails?.techRecord_adrDetails_tank_tankDetails_specialProvisions,
TankStatement: adrDetails?.techRecord_adrDetails_tank_tankDetails_tankStatement_statement,
ExpiryDate: testResult.testTypes.testExpiryDate,
ExpiryDate: testTypes.testExpiryDate,
AtfNameAtfPNumber: testResult.testStationName + ' ' + testResult.testStationPNumber,
Notes: testResult.testTypes.additionalNotesRecorded,
TestTypeDate: testResult.testTypes.testTypeStartTimestamp,
Notes: testTypes.additionalNotesRecorded,
TestTypeDate: testTypes.testTypeStartTimestamp,
};

return {
Expand Down
36 changes: 20 additions & 16 deletions src/certificate/commands/DefectsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TestResults } from '@dvsa/cvs-type-definitions/types/v1/enums/testResult.enum.js';
import { TestTypeSchema } from '@dvsa/cvs-type-definitions/types/v1/test-result';
import { Service } from 'typedi';
import { DefectRepository } from '../../defect/DefectRepository';
import { DefectService } from '../../defect/DefectService';
import { IFlatDefect, ITestResult } from '../../models';
import { ICertificatePayload } from '../../models';
import { ITestType } from '../../models';
import { CERTIFICATE_DATA, TEST_RESULTS } from '../../models/Enums';
import { ICertificatePayload, IFlatDefect, TestResultSchemaTestTypesAsObject } from '../../models';
import { CERTIFICATE_DATA } from '../../models/Enums';
import { BasePayloadCommand } from '../ICertificatePayloadCommand';

@Service()
Expand All @@ -24,20 +24,18 @@ export class DefectsCommand extends BasePayloadCommand {
return {} as ICertificatePayload;
}

const {
testResult,
testResult: { testTypes },
} = this.state;
const { testResult } = this.state;
const testTypes = testResult.testTypes;

const result = {} as ICertificatePayload;

if (testTypes.testResult !== TEST_RESULTS.FAIL) {
if (testTypes.testResult !== TestResults.FAIL) {
result.DATA = {
...(await this.getPayloadData(testResult, CERTIFICATE_DATA.PASS_DATA)),
};
}

if (testTypes.testResult !== TEST_RESULTS.PASS) {
if (testTypes.testResult !== TestResults.PASS) {
result.FAIL_DATA = {
...(await this.getPayloadData(testResult, CERTIFICATE_DATA.FAIL_DATA)),
};
Expand All @@ -46,7 +44,7 @@ export class DefectsCommand extends BasePayloadCommand {
return result;
}

private async getPayloadData(testResult: ITestResult, type: CERTIFICATE_DATA): Promise<any> {
private async getPayloadData(testResult: TestResultSchemaTestTypesAsObject, type: CERTIFICATE_DATA): Promise<any> {
const { isWelsh } = this.state;

let flattenedDefects: IFlatDefect[] = [];
Expand Down Expand Up @@ -75,7 +73,7 @@ export class DefectsCommand extends BasePayloadCommand {
* @param isWelsh - determines whether the atf in which the test result was conducted resides in Wales
*/
private generateDefects(
testTypes: ITestType,
testTypes: TestTypeSchema,
type: CERTIFICATE_DATA,
vehicleType: string,
flattenedDefects: IFlatDefect[],
Expand All @@ -98,7 +96,7 @@ export class DefectsCommand extends BasePayloadCommand {
switch (defect.deficiencyCategory.toLowerCase()) {
case 'dangerous':
this.defectService.generateDangerousDefects(
testTypes.testResult,
testTypes.testResult as TestResults,
defect,
type,
defects,
Expand All @@ -109,7 +107,7 @@ export class DefectsCommand extends BasePayloadCommand {
break;
case 'major':
this.defectService.generateMajorDefects(
testTypes.testResult,
testTypes.testResult as TestResults,
defect,
type,
defects,
Expand All @@ -123,13 +121,19 @@ export class DefectsCommand extends BasePayloadCommand {
defects,
defect,
vehicleType,
testTypes.testResult,
testTypes.testResult as TestResults,
isWelsh,
flattenedDefects
);
break;
case 'advisory':
this.defectService.generateAdvisoryDefects(defects, defect, vehicleType, testTypes.testResult, isWelsh);
this.defectService.generateAdvisoryDefects(
defects,
defect,
vehicleType,
testTypes.testResult as TestResults,
isWelsh
);
break;
default:
break;
Expand Down
19 changes: 9 additions & 10 deletions src/certificate/commands/IvaCertificateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SpecialistCustomDefectsSchemaPut } from '@dvsa/cvs-type-definitions/types/v1/test-result';
import moment from 'moment';
import { Service } from 'typedi';
import { DefectService } from '../../defect/DefectService';
import { IRequiredStandard } from '../../models';
import { ICertificatePayload } from '../../models';
import { CERTIFICATE_DATA, IVA_30 } from '../../models/Enums';
import { TestResultService } from '../../test-result/TestResultService';
Expand All @@ -24,26 +24,25 @@ export class IvaCertificateCommand extends BasePayloadCommand {
}

const { testResult } = this.state;
const testTypes = testResult.testTypes;

const ivaFailDetailsForDocGen = {
vin: testResult.vin,
serialNumber: testResult.vehicleType === 'trl' ? testResult.trailerId : testResult.vrm,
vehicleTrailerNrNo: testResult.vehicleType === 'trl' ? testResult.trailerId : testResult.vrm,
testCategoryClass: testResult.euVehicleCategory,
testCategoryBasicNormal: this.testResultService.isBasicIvaTest(testResult.testTypes.testTypeId)
testCategoryBasicNormal: this.testResultService.isBasicIvaTest(testTypes.testTypeId)
? IVA_30.BASIC
: IVA_30.NORMAL,
make: testResult.make,
model: testResult.model,
bodyType: testResult.bodyType?.description,
date: moment(testResult.testTypes.testTypeStartTimestamp).format('DD/MM/YYYY'),
date: moment(testTypes.testTypeStartTimestamp).format('DD/MM/YYYY'),
testerName: testResult.testerName,
reapplicationDate: testResult.testTypes?.reapplicationDate
? moment(testResult.testTypes?.reapplicationDate).format('DD/MM/YYYY')
: '',
reapplicationDate: testTypes.reapplicationDate ? moment(testTypes.reapplicationDate).format('DD/MM/YYYY') : '',
station: testResult.testStationName,
additionalDefects: this.defectService.formatVehicleApprovalAdditionalDefects(testResult.testTypes.customDefects),
requiredStandards: this.sortRequiredStandards(testResult.testTypes.requiredStandards),
additionalDefects: this.defectService.formatVehicleApprovalAdditionalDefects(testTypes.customDefects ?? []),
requiredStandards: this.sortRequiredStandards(testTypes.requiredStandards),
};

return {
Expand All @@ -57,8 +56,8 @@ export class IvaCertificateCommand extends BasePayloadCommand {
* @returns - the sorted requiredStandards array
*/
private sortRequiredStandards = (
requiredStandards: IRequiredStandard[] | undefined
): IRequiredStandard[] | undefined => {
requiredStandards: SpecialistCustomDefectsSchemaPut[] | undefined
): SpecialistCustomDefectsSchemaPut[] | undefined => {
if (!requiredStandards) {
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/certificate/commands/MakeAndModelCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TestResults } from '@dvsa/cvs-type-definitions/types/v1/enums/testResult.enum.js';
import { Service } from 'typedi';
import { ICertificatePayload, IMakeAndModel } from '../../models';
import { CERTIFICATE_DATA, TEST_RESULTS } from '../../models/Enums';
import { CERTIFICATE_DATA } from '../../models/Enums';
import { TechRecordService } from '../../tech-record/TechRecordService';
import { TestResultService } from '../../test-result/TestResultService';
import { IGetTrailerRegistrationResult } from '../../trailer/IGetTrailerRegistrationResult';
Expand Down Expand Up @@ -35,14 +36,14 @@ export class MakeAndModelCommand extends BasePayloadCommand {
const makeAndModel = (await this.techRecordService.getVehicleMakeAndModel(testResult)) as Required<IMakeAndModel>;
const trnRegistration = await this.trailerRegistration(makeAndModel);

if (testTypes.testResult !== TEST_RESULTS.FAIL) {
if (testTypes.testResult !== TestResults.FAIL) {
result.DATA = {
...makeAndModel,
...trnRegistration,
};
}

if (testTypes.testResult !== TEST_RESULTS.PASS) {
if (testTypes.testResult !== TestResults.PASS) {
result.FAIL_DATA = {
...makeAndModel,
...trnRegistration,
Expand Down
17 changes: 8 additions & 9 deletions src/certificate/commands/MsvaCertificateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SpecialistCustomDefectsSchemaPut } from '@dvsa/cvs-type-definitions/types/v1/test-result';
import moment from 'moment';
import { Service } from 'typedi';
import { DefectService } from '../../defect/DefectService';
import { ICertificatePayload } from '../../models';
import { IRequiredStandard } from '../../models';
import { CERTIFICATE_DATA } from '../../models/Enums';
import { BasePayloadCommand } from '../ICertificatePayloadCommand';

Expand All @@ -20,6 +20,7 @@ export class MsvaCertificateCommand extends BasePayloadCommand {
}

const { testResult } = this.state;
const testTypes = testResult.testTypes;

const msvaFailDetailsForDocGen = {
vin: testResult.vin,
Expand All @@ -29,13 +30,11 @@ export class MsvaCertificateCommand extends BasePayloadCommand {
model: testResult.model,
type: testResult.vehicleType,
testerName: testResult.testerName,
date: moment(testResult.testTypes.testTypeStartTimestamp).format('DD/MM/YYYY'),
reapplicationDate: testResult.testTypes?.reapplicationDate
? moment(testResult.testTypes?.reapplicationDate).format('DD/MM/YYYY')
: '',
date: moment(testTypes.testTypeStartTimestamp).format('DD/MM/YYYY'),
reapplicationDate: testTypes.reapplicationDate ? moment(testTypes.reapplicationDate).format('DD/MM/YYYY') : '',
station: testResult.testStationName,
additionalDefects: this.defectService.formatVehicleApprovalAdditionalDefects(testResult.testTypes.customDefects),
requiredStandards: this.sortRequiredStandards(testResult.testTypes.requiredStandards),
additionalDefects: this.defectService.formatVehicleApprovalAdditionalDefects(testTypes.customDefects ?? []),
requiredStandards: this.sortRequiredStandards(testTypes.requiredStandards),
};

return {
Expand All @@ -49,8 +48,8 @@ export class MsvaCertificateCommand extends BasePayloadCommand {
* @returns - the sorted requiredStandards array
*/
private sortRequiredStandards = (
requiredStandards: IRequiredStandard[] | undefined
): IRequiredStandard[] | undefined => {
requiredStandards: SpecialistCustomDefectsSchemaPut[] | undefined
): SpecialistCustomDefectsSchemaPut[] | undefined => {
if (!requiredStandards) {
return;
}
Expand Down
21 changes: 10 additions & 11 deletions src/certificate/commands/OdometerHistoryCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestResults } from '@dvsa/cvs-type-definitions/types/v1/enums/testResult.enum.js';
import { Service } from 'typedi';
import { ICertificatePayload } from '../../models';
import { ITestResult } from '../../models';
import { CERTIFICATE_DATA, TEST_RESULTS, VEHICLE_TYPES } from '../../models/Enums';
import { ICertificatePayload, TestResultSchemaTestTypesAsObject } from '../../models';
import { CERTIFICATE_DATA, VEHICLE_TYPES } from '../../models/Enums';
import { TestResultRepository } from '../../test-result/TestResultRepository';
import { BasePayloadCommand } from '../ICertificatePayloadCommand';

Expand All @@ -14,7 +14,8 @@ export class OdometerHistoryCommand extends BasePayloadCommand {
private certificateIsAnPassOrFail = (): boolean =>
this.state.type === CERTIFICATE_DATA.PASS_DATA || this.state.type === CERTIFICATE_DATA.FAIL_DATA;

private vehicleIsTrailer = (testResult: ITestResult): boolean => testResult.vehicleType === VEHICLE_TYPES.TRL;
private vehicleIsTrailer = (testResult: TestResultSchemaTestTypesAsObject): boolean =>
testResult.vehicleType === VEHICLE_TYPES.TRL;

public async generate(): Promise<ICertificatePayload> {
const result = {} as ICertificatePayload;
Expand All @@ -23,24 +24,22 @@ export class OdometerHistoryCommand extends BasePayloadCommand {
return result;
}

const {
testResult,
testResult: { testTypes, systemNumber },
} = this.state;
const { testResult } = this.state;
const testTypes = testResult.testTypes;

if (this.vehicleIsTrailer(testResult)) {
return result;
}

const odometerHistory = await this.testResultRepository.getOdometerHistory(systemNumber);
const odometerHistory = await this.testResultRepository.getOdometerHistory(testResult.systemNumber);

if (testTypes.testResult !== TEST_RESULTS.FAIL) {
if (testTypes.testResult !== TestResults.FAIL) {
result.DATA = {
...odometerHistory,
};
}

if (testTypes.testResult !== TEST_RESULTS.PASS) {
if (testTypes.testResult !== TestResults.PASS) {
result.FAIL_DATA = {
...odometerHistory,
};
Expand Down
20 changes: 9 additions & 11 deletions src/certificate/commands/PassOrFailCertificateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TestResults } from '@dvsa/cvs-type-definitions/types/v1/enums/testResult.enum.js';
import moment from 'moment';
import { Service } from 'typedi';
import { ICertificatePayload, ITestResult } from '../../models';
import { CERTIFICATE_DATA, TEST_RESULTS, VEHICLE_TYPES } from '../../models/Enums';
import { ICertificatePayload, TestResultSchemaTestTypesAsObject } from '../../models';
import { CERTIFICATE_DATA, VEHICLE_TYPES } from '../../models/Enums';
import { BasePayloadCommand } from '../ICertificatePayloadCommand';

@Service()
Expand All @@ -16,20 +17,18 @@ export class PassOrFailCertificateCommand extends BasePayloadCommand {
return result;
}

const {
testResult,
testResult: { testTypes },
} = this.state;
const { testResult } = this.state;
const testTypes = testResult.testTypes;

const payload = await this.getPayloadData(testResult);

if (testTypes.testResult !== TEST_RESULTS.FAIL) {
if (testTypes.testResult !== TestResults.FAIL) {
result.DATA = {
...payload,
};
}

if (testTypes.testResult !== TEST_RESULTS.PASS) {
if (testTypes.testResult !== TestResults.PASS) {
result.FAIL_DATA = {
...payload,
};
Expand All @@ -38,7 +37,7 @@ export class PassOrFailCertificateCommand extends BasePayloadCommand {
return result;
}

private async getPayloadData(testResult: ITestResult): Promise<any> {
private async getPayloadData(testResult: TestResultSchemaTestTypesAsObject): Promise<any> {
const testType = testResult.testTypes;

return {
Expand All @@ -59,8 +58,7 @@ export class PassOrFailCertificateCommand extends BasePayloadCommand {
EarliestDateOfTheNextTest:
((testResult.vehicleType as VEHICLE_TYPES) === VEHICLE_TYPES.HGV ||
(testResult.vehicleType as VEHICLE_TYPES) === VEHICLE_TYPES.TRL) &&
((testResult.testTypes.testResult as TEST_RESULTS) === TEST_RESULTS.PASS ||
(testResult.testTypes.testResult as TEST_RESULTS) === TEST_RESULTS.PRS)
(testType.testResult === TestResults.PASS || testType.testResult === TestResults.PRS)
? moment(testType.testAnniversaryDate).subtract(1, 'months').startOf('month').format('DD.MM.YYYY')
: moment(testType.testAnniversaryDate).format('DD.MM.YYYY'),
SeatBeltTested: testType.seatbeltInstallationCheckDate ? 'Yes' : 'No',
Expand Down
Loading
Loading