import { IsNull, Not } from "typeorm"; import { dataSource } from "../../../data-source"; import { inspection } from "../../../entity/unit/inspection/inspection"; import DatabaseActionException from "../../../exceptions/databaseActionException"; import InspectionService from "../../../service/unit/inspection/inspectionService"; import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService"; import { CreateInspectionCommand, UpdateInspectionCommand, DeleteInspectionCommand, FinishInspectionCommand, } from "./inspectionCommand"; export default abstract class InspectionCommandHandler { /** * @description create inspection * @param {CreateInspectionCommand} createInspection * @returns {Promise} */ static async create(createInspection: CreateInspectionCommand): Promise { let latestVersionedPlan = await InspectionVersionedPlanService.getLatestForInspectionPlan( createInspection.inspectionPlanId ); let insertId = ""; return await dataSource .transaction(async (manager) => { await manager .createQueryBuilder() .update(inspection) .set({ hasNewer: true, }) .where({ inspectionPlanId: createInspection.inspectionPlanId, equipmentId: createInspection.assigned == "equipment" ? createInspection.relatedId : IsNull(), vehicleId: createInspection.assigned == "vehicle" ? createInspection.relatedId : IsNull(), wearableId: createInspection.assigned == "wearable" ? createInspection.relatedId : IsNull(), }) .execute(); await manager .createQueryBuilder() .insert() .into(inspection) .values({ context: createInspection.context, nextInspection: createInspection.nextInspection, inspectionPlanId: createInspection.inspectionPlanId, inspectionVersionedPlanId: latestVersionedPlan.id, equipmentId: createInspection.assigned == "equipment" ? createInspection.relatedId : null, vehicleId: createInspection.assigned == "vehicle" ? createInspection.relatedId : null, wearableId: createInspection.assigned == "wearable" ? createInspection.relatedId : null, }) .execute() .then((result) => { insertId = result.identifiers[0].id; }); }) .then(() => { return insertId; }) .catch((err) => { throw new DatabaseActionException("CREATE", "inspection", err); }); } /** * @description update inspection * @param {UpdateInspectionCommand} updateInspection * @returns {Promise} */ static async update(updateInspection: UpdateInspectionCommand): Promise { return await dataSource .createQueryBuilder() .update(inspection) .set({ context: updateInspection.context, nextInspection: updateInspection.nextInspection, }) .where("id = :id", { id: updateInspection.id }) .execute() .then(() => {}) .catch((err) => { throw new DatabaseActionException("UPDATE", "inspection", err); }); } /** * @description finish inspection * @param {FinishInspectionCommand} finishInspection * @returns {Promise} */ static async finish(finishInspection: FinishInspectionCommand): Promise { return await dataSource .createQueryBuilder() .update(inspection) .set({ finishedAt: new Date(), }) .where("id = :id", { id: finishInspection.id }) .execute() .then(() => {}) .catch((err) => { throw new DatabaseActionException("FINISH", "inspection", err); }); } /** * @description delete inspection * @param {DeleteInspectionCommand} deleteInspection * @returns {Promise} */ static async delete(deleteInspection: DeleteInspectionCommand): Promise { let deleteInspectionData = await InspectionService.getById(deleteInspection.id); return await dataSource .transaction(async (manager) => { let latestInspection = await manager .createQueryBuilder() .from(inspection, "sub") .where({ inspectionPlanId: deleteInspectionData.inspectionPlanId, inspectionVersionedPlanId: deleteInspectionData.inspectionVersionedPlanId, equipmentId: deleteInspectionData.equipmentId ?? IsNull(), vehicleId: deleteInspectionData.vehicleId ?? IsNull(), wearableId: deleteInspectionData.wearableId ?? IsNull(), }) .andWhere({ id: Not(deleteInspection.id) }) .orderBy("sub.createdAt", "DESC") .limit(1) .getOne(); if (latestInspection) await manager .createQueryBuilder() .update(inspection) .set({ hasNewer: false, }) .where({ id: latestInspection.id }) .execute(); await manager .createQueryBuilder() .delete() .from(inspection) .where("id = :id", { id: deleteInspection.id }) .execute(); }) .then(() => {}) .catch((err) => { throw new DatabaseActionException("DELETE", "inspection", err); }); } }