Skip to content

Commit

Permalink
feat(cb2-8661): remove validation as moved to earlier step (#329)
Browse files Browse the repository at this point in the history
* feat(cb2-8661): remove validation as moved to earlier step

* feat(cb2-8661): remove old int test
  • Loading branch information
naathanbrown authored Jul 14, 2023
1 parent 98169eb commit ed20491
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 138 deletions.
42 changes: 23 additions & 19 deletions src/functions/updateTechRecordStatus.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import TechRecordsDAO from "../models/TechRecordsDAO";
import TechRecordsService from "../services/TechRecordsService";
import HTTPResponse from "../models/HTTPResponse";
import {STATUS, HTTPRESPONSE} from "../assets/Enums";
import {TechRecordStatusHandler} from "../handlers/TechRecordStatusHandler";
import { STATUS, HTTPRESPONSE } from "../assets/Enums";
import { TechRecordStatusHandler } from "../handlers/TechRecordStatusHandler";

export async function updateTechRecordStatus(event: any) {
const techRecordsService = new TechRecordsService(new TechRecordsDAO());
const techRecordsService = new TechRecordsService(new TechRecordsDAO());

const systemNumber: string = event.pathParameters.systemNumber;
const testStatus: string = event.queryStringParameters!.testStatus;
const testResult: string = event.queryStringParameters!.testResult;
const testTypeId: string = event.queryStringParameters!.testTypeId;
const newStatus = event.queryStringParameters ? STATUS[event.queryStringParameters.newStatus as keyof typeof STATUS] : undefined;
const createdById: string = event.queryStringParameters!.createdById;
const createdByName: string = event.queryStringParameters!.createdByName;
const systemNumber: string = event.pathParameters.systemNumber;
const testStatus: string = event.queryStringParameters!.testStatus;
const testResult: string = event.queryStringParameters!.testResult;
const testTypeId: string = event.queryStringParameters!.testTypeId;
const newStatus = event.queryStringParameters
? STATUS[event.queryStringParameters.newStatus as keyof typeof STATUS]
: undefined;
const createdById: string = event.queryStringParameters!.createdById;
const createdByName: string = event.queryStringParameters!.createdByName;

if (!TechRecordStatusHandler.isStatusUpdateRequired(testStatus, testResult, testTypeId)) {
return new HTTPResponse(200, HTTPRESPONSE.NO_STATUS_UPDATE_REQUIRED);
}
try {
const updatedTechRec = await techRecordsService.updateTechRecordStatusCode(systemNumber, newStatus, createdById, createdByName);
return new HTTPResponse(200, updatedTechRec);
} catch (error) {
return new HTTPResponse(error.statusCode, error.body);
}
try {
const updatedTechRec = await techRecordsService.updateTechRecordStatusCode(
systemNumber,
newStatus,
createdById,
createdByName
);
return new HTTPResponse(200, updatedTechRec);
} catch (error) {
return new HTTPResponse(error.statusCode, error.body);
}
}
84 changes: 53 additions & 31 deletions src/handlers/TechRecordStatusHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as handlers from "./index";
import { Vehicle } from "../../@Types/TechRecords";
import { VehicleProcessor } from "../domain/Processors";


export class TechRecordStatusHandler<T extends Vehicle> {
private techRecordsListHandler: handlers.TechRecordsListHandler<T>;
private readonly auditHandler: handlers.AuditDetailsHandler;
Expand All @@ -15,20 +14,50 @@ export class TechRecordStatusHandler<T extends Vehicle> {
this.auditHandler = new handlers.AuditDetailsHandler();
}

public async prepareTechRecordForStatusUpdate(systemNumber: string, newStatus: STATUS = STATUS.CURRENT, createdById: string, createdByName: string): Promise<T> {
const techRecordWrapper = await this.techRecordsListHandler.getFormattedTechRecordsList(systemNumber, STATUS.ALL, SEARCHCRITERIA.SYSTEM_NUMBER, false);
const uniqueRecord = VehicleProcessor.getTechRecordToUpdate(techRecordWrapper, (techRecord) => techRecord.statusCode === STATUS.PROVISIONAL || techRecord.statusCode === STATUS.CURRENT) as T;
const provisionalTechRecords = uniqueRecord.techRecord.filter((techRecord) => techRecord.statusCode === STATUS.PROVISIONAL);
const currentTechRecords = uniqueRecord.techRecord.filter((techRecord) => techRecord.statusCode === STATUS.CURRENT);
public async prepareTechRecordForStatusUpdate(
systemNumber: string,
newStatus: STATUS = STATUS.CURRENT,
createdById: string,
createdByName: string
): Promise<T> {
const techRecordWrapper =
await this.techRecordsListHandler.getFormattedTechRecordsList(
systemNumber,
STATUS.ALL,
SEARCHCRITERIA.SYSTEM_NUMBER,
false
);
const uniqueRecord = VehicleProcessor.getTechRecordToUpdate(
techRecordWrapper,
(techRecord) =>
techRecord.statusCode === STATUS.PROVISIONAL ||
techRecord.statusCode === STATUS.CURRENT
) as T;
const provisionalTechRecords = uniqueRecord.techRecord.filter(
(techRecord) => techRecord.statusCode === STATUS.PROVISIONAL
);
const currentTechRecords = uniqueRecord.techRecord.filter(
(techRecord) => techRecord.statusCode === STATUS.CURRENT
);
let newTechRecord;
if (provisionalTechRecords.length === 1) {
provisionalTechRecords[0].statusCode = STATUS.ARCHIVED;
newTechRecord = cloneDeep(provisionalTechRecords[0]);
newTechRecord.statusCode = newStatus;

const date = new Date().toISOString();
this.auditHandler.setCreatedAuditDetails(newTechRecord, createdByName, createdById, date);
this.auditHandler.setLastUpdatedAuditDetails(provisionalTechRecords[0], createdByName, createdById, date);
this.auditHandler.setCreatedAuditDetails(
newTechRecord,
createdByName,
createdById,
date
);
this.auditHandler.setLastUpdatedAuditDetails(
provisionalTechRecords[0],
createdByName,
createdById,
date
);
provisionalTechRecords[0].updateType = UPDATE_TYPE.TECH_RECORD_UPDATE;
}
if (currentTechRecords.length === 1) {
Expand All @@ -37,37 +66,30 @@ export class TechRecordStatusHandler<T extends Vehicle> {
if (!newTechRecord) {
newTechRecord = cloneDeep(currentTechRecords[0]);
newTechRecord.statusCode = newStatus;
this.auditHandler.setCreatedAuditDetails(newTechRecord, createdByName, createdById, date);
this.auditHandler.setCreatedAuditDetails(
newTechRecord,
createdByName,
createdById,
date
);
}
this.auditHandler.setLastUpdatedAuditDetails(currentTechRecords[0], createdByName, createdById, date);
this.auditHandler.setLastUpdatedAuditDetails(
currentTechRecords[0],
createdByName,
createdById,
date
);
currentTechRecords[0].updateType = UPDATE_TYPE.TECH_RECORD_UPDATE;
}

// if newTechRecord is undefined that means there multiple or no current/provisional records were found
if (!newTechRecord) {
throw new HTTPError(400, "The tech record status cannot be updated to " + newStatus);
throw new HTTPError(
400,
"The tech record status cannot be updated to " + newStatus
);
}
uniqueRecord.techRecord.push(newTechRecord);
return uniqueRecord;
}

public static isStatusUpdateRequired(testStatus: string, testResult: string, testTypeId: string): boolean {
return testStatus === "submitted" && (testResult === "pass" || testResult === "prs") &&
(this.isTestTypeFirstTest(testTypeId) || this.isTestTypeNotifiableAlteration(testTypeId) || this.isTestTypeCOIF(testTypeId));
}

private static isTestTypeFirstTest(testTypeId: string): boolean {
const firstTestIds = ["41", "95", "65", "66", "67", "103", "104", "82", "83", "119", "120", "186", "188", "192", "194"];
return firstTestIds.includes(testTypeId);
}

private static isTestTypeNotifiableAlteration(testTypeId: string): boolean {
const notifiableAlterationIds = ["38", "47", "48"];
return notifiableAlterationIds.includes(testTypeId);
}

private static isTestTypeCOIF(testTypeId: string): boolean {
const coifIds = ["142", "143", "175", "176"];
return coifIds.includes(testTypeId);
}
}
23 changes: 0 additions & 23 deletions tests/integration/updateTechRecordStatus.intTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@ describe("UpdateTechRecordStatus", () => {
});

context("when trying to update a vehicle", () => {
it("should not update the status if the test type does not require a tech record update", async () => {
const systemNumber: string = "11000001";
expect.assertions(2);
await LambdaTester(updateTechRecordStatus)
.event({
path: "/vehicles/update-status/" + systemNumber,
pathParameters: {
systemNumber,
},
queryStringParameters: {
testStatus: "submitted",
testResult: "pass",
testTypeId: "1",
},
httpMethod: "PUT",
resource: "/vehicles/update-status/{systemNumber}"
})
.expectResolve((result: any) => {
expect(result.statusCode).toBe(200);
expect(JSON.parse(result.body)).toBe(HTTPRESPONSE.NO_STATUS_UPDATE_REQUIRED);
});
});

it("should return 200 and the updated vehicle if it has a provisional techRecord", async () => {
const systemNumber: string = "11000027";
expect.assertions(4);
Expand Down
Loading

0 comments on commit ed20491

Please sign in to comment.