inspection finish and print

This commit is contained in:
Julian Krauser 2025-07-11 14:02:36 +02:00
parent 0f3e4488f4
commit 9ef82adef7
12 changed files with 358 additions and 76 deletions

View file

@ -4,7 +4,12 @@ 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 } from "./inspectionCommand";
import {
CreateInspectionCommand,
UpdateInspectionCommand,
DeleteInspectionCommand,
FinishInspectionCommand,
} from "./inspectionCommand";
export default abstract class InspectionCommandHandler {
/**
@ -80,6 +85,26 @@ export default abstract class InspectionCommandHandler {
});
}
/**
* @description finish inspection
* @param {FinishInspectionCommand} finishInspection
* @returns {Promise<void>}
*/
static async finish(finishInspection: FinishInspectionCommand): Promise<void> {
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
@ -89,31 +114,30 @@ export default abstract class InspectionCommandHandler {
let deleteInspectionData = await InspectionService.getById(deleteInspection.id);
return await dataSource
.transaction(async (manager) => {
await manager
let latestInspection = await manager
.createQueryBuilder()
.update(inspection)
.set({
hasNewer: false,
.from(inspection, "sub")
.where({
inspectionPlanId: deleteInspectionData.inspectionPlanId,
inspectionVersionedPlanId: deleteInspectionData.inspectionVersionedPlanId,
equipmentId: deleteInspectionData.equipmentId ?? IsNull(),
vehicleId: deleteInspectionData.vehicleId ?? IsNull(),
wearableId: deleteInspectionData.wearableId ?? IsNull(),
})
.where((qb) => {
const subQuery = qb
.createQueryBuilder()
.select("id")
.from(inspection, "sub")
.where({
inspectionPlanId: deleteInspectionData.inspectionPlanId,
inspectionVersionedPlanId: deleteInspectionData.inspectionVersionedPlanId,
equipmentId: deleteInspectionData.equipmentId,
vehicleId: deleteInspectionData.vehicleId,
wearableId: deleteInspectionData.wearableId,
})
.andWhere({ id: Not(deleteInspection.id) })
.orderBy("sub.createdAt", "DESC")
.limit(1)
.getQuery();
return "id = " + subQuery;
})
.execute();
.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()