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

@ -12,6 +12,10 @@ export interface UpdateInspectionCommand {
nextInspection?: Date;
}
export interface FinishInspectionCommand {
id: string;
}
export interface DeleteInspectionCommand {
id: string;
}

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()

View file

@ -1,4 +1,4 @@
export interface CreateInspectionPointResultCommand {
export interface CreateOrUpdateInspectionPointResultCommand {
inspectionId: string;
inspectionPointId: string;
value: string;

View file

@ -1,15 +1,17 @@
import { dataSource } from "../../../data-source";
import { inspectionPointResult } from "../../../entity/unit/inspection/inspectionPointResult";
import DatabaseActionException from "../../../exceptions/databaseActionException";
import { CreateInspectionPointResultCommand } from "./inspectionPointResultCommand";
import { CreateOrUpdateInspectionPointResultCommand } from "./inspectionPointResultCommand";
export default abstract class InspectionPointResultCommandHandler {
/**
* @description create inspectionPointResult
* @param {CreateInspectionPointResultCommand} createInspectionPointResult
* @param {CreateOrUpdateInspectionPointResultCommand} createInspectionPointResult
* @returns {Promise<number>}
*/
static async createOrUpdate(createInspectionPointResult: CreateInspectionPointResultCommand): Promise<number> {
static async createOrUpdate(
createInspectionPointResult: CreateOrUpdateInspectionPointResultCommand
): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
@ -25,7 +27,28 @@ export default abstract class InspectionPointResultCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new DatabaseActionException("CREATE", "inspectionPointResult", err);
throw new DatabaseActionException("CREATE or UPDATE", "inspectionPointResult", err);
});
}
/**
* @description create inspectionPointResult
* @param {Array<CreateOrUpdateInspectionPointResultCommand>} results
* @returns {Promise<number>}
*/
static async createOrUpdateMultiple(results: Array<CreateOrUpdateInspectionPointResultCommand>): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
.into(inspectionPointResult)
.values(results)
.orUpdate(["value"], ["inspectionId", "inspectionPointId"])
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
throw new DatabaseActionException("CREATE or UPDATE", "inspectionPointResult", err);
});
}
}