2025-05-31 15:12:16 +02:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import InspectionService from "../../../service/unit/inspection/inspectionService";
|
|
|
|
import InspectionFactory from "../../../factory/admin/unit/inspection/inspection";
|
|
|
|
import {
|
|
|
|
CreateInspectionCommand,
|
|
|
|
DeleteInspectionCommand,
|
2025-07-11 14:02:36 +02:00
|
|
|
FinishInspectionCommand,
|
2025-05-31 15:12:16 +02:00
|
|
|
UpdateInspectionCommand,
|
|
|
|
} from "../../../command/unit/inspection/inspectionCommand";
|
|
|
|
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
|
2025-06-04 14:30:57 +02:00
|
|
|
import BadRequestException from "../../../exceptions/badRequestException";
|
2025-07-09 12:57:37 +02:00
|
|
|
import ForbiddenRequestException from "../../../exceptions/forbiddenRequestException";
|
2025-07-11 14:02:36 +02:00
|
|
|
import { CreateOrUpdateInspectionPointResultCommand } from "../../../command/unit/inspection/inspectionPointResultCommand";
|
|
|
|
import InspectionPointResultCommandHandler from "../../../command/unit/inspection/inspectionPointResultCommandHandler";
|
|
|
|
import { InspectionPointEnum } from "../../../enums/inspectionEnum";
|
|
|
|
import multer from "multer";
|
|
|
|
import { FileSystemHelper } from "../../../helpers/fileSystemHelper";
|
|
|
|
import { PdfExport } from "../../../helpers/pdfExport";
|
2025-07-09 12:57:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all inspections sorted by id not having newer inspection
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getAllInspectionsSortedNotHavingNewer(req: Request, res: Response): Promise<any> {
|
|
|
|
let offset = parseInt((req.query.offset as string) ?? "0");
|
|
|
|
let count = parseInt((req.query.count as string) ?? "25");
|
|
|
|
let noLimit = req.query.noLimit === "true";
|
|
|
|
|
|
|
|
let [inspections, total] = await InspectionService.getAllSortedNotHavingNewer({ offset, count, noLimit });
|
|
|
|
|
|
|
|
res.json({
|
2025-07-10 13:22:29 +02:00
|
|
|
inspections: InspectionFactory.mapToBaseNext(inspections),
|
2025-07-09 12:57:37 +02:00
|
|
|
total: total,
|
|
|
|
offset: offset,
|
|
|
|
count: count,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all inspections running
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getAllInspectionsRunning(req: Request, res: Response): Promise<any> {
|
|
|
|
let offset = parseInt((req.query.offset as string) ?? "0");
|
|
|
|
let count = parseInt((req.query.count as string) ?? "25");
|
|
|
|
let noLimit = req.query.noLimit === "true";
|
|
|
|
|
|
|
|
let [inspections, total] = await InspectionService.getAllRunning({ offset, count, noLimit });
|
|
|
|
|
|
|
|
res.json({
|
2025-07-10 13:22:29 +02:00
|
|
|
inspections: InspectionFactory.mapToBaseMinified(inspections),
|
2025-07-09 12:57:37 +02:00
|
|
|
total: total,
|
|
|
|
offset: offset,
|
|
|
|
count: count,
|
|
|
|
});
|
|
|
|
}
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all inspections for related id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getAllInspectionsForRelated(req: Request, res: Response): Promise<any> {
|
2025-06-13 11:31:34 +02:00
|
|
|
let relation = req.params.related as "vehicle" | "equipment" | "wearable";
|
2025-05-31 15:12:16 +02:00
|
|
|
let relationId = req.params.relatedId as string;
|
|
|
|
let offset = parseInt((req.query.offset as string) ?? "0");
|
|
|
|
let count = parseInt((req.query.count as string) ?? "25");
|
|
|
|
let noLimit = req.query.noLimit === "true";
|
|
|
|
|
2025-06-13 11:31:34 +02:00
|
|
|
let where;
|
|
|
|
if (relation == "equipment") {
|
|
|
|
where = { equipmentId: relationId };
|
|
|
|
} else if (relation == "vehicle") {
|
|
|
|
where = { vehicleId: relationId };
|
|
|
|
} else {
|
|
|
|
where = { wearableId: relationId };
|
|
|
|
}
|
2025-06-02 13:14:09 +02:00
|
|
|
let [inspections, total] = await InspectionService.getAllForRelated(where, { offset, count, noLimit });
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
res.json({
|
2025-07-11 14:02:36 +02:00
|
|
|
inspections: InspectionFactory.mapToBaseMinified(inspections),
|
2025-05-31 15:12:16 +02:00
|
|
|
total: total,
|
|
|
|
offset: offset,
|
|
|
|
count: count,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-07-11 14:02:36 +02:00
|
|
|
/**
|
|
|
|
* @description get inspection by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getInspectionPrintoutById(req: Request, res: Response): Promise<any> {
|
|
|
|
const inspectionId = req.params.id;
|
|
|
|
let inspection = await InspectionService.getById(inspectionId);
|
|
|
|
|
|
|
|
if (inspection.finishedAt == null)
|
|
|
|
throw new ForbiddenRequestException("this inspection has not been finished yet and it so does not have a printout");
|
|
|
|
|
|
|
|
let filepath = FileSystemHelper.formatPath("inspection", inspection.id, "printout.pdf");
|
|
|
|
|
|
|
|
res.sendFile(filepath, {
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/pdf",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-05-31 15:12:16 +02:00
|
|
|
/**
|
|
|
|
* @description get inspection by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getInspectionById(req: Request, res: Response): Promise<any> {
|
|
|
|
const inspectionId = req.params.id;
|
|
|
|
let inspection = await InspectionService.getById(inspectionId);
|
|
|
|
|
|
|
|
res.json(InspectionFactory.mapToSingle(inspection));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description create inspection
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function createInspection(req: Request, res: Response): Promise<any> {
|
2025-06-04 14:30:57 +02:00
|
|
|
const context = req.body.context;
|
|
|
|
const inspectionPlanId = req.body.inspectionPlanId;
|
|
|
|
const relatedId = req.body.relatedId;
|
|
|
|
const assigned = req.body.assigned;
|
|
|
|
const nextInspection = req.body.nextInspection || null;
|
|
|
|
|
2025-06-13 11:31:34 +02:00
|
|
|
if (assigned != "equipment" && assigned != "vehicle" && assigned != "wearable")
|
|
|
|
throw new BadRequestException("set assigned to equipment or vehicle or wearable");
|
2025-05-31 15:12:16 +02:00
|
|
|
|
2025-07-10 13:22:29 +02:00
|
|
|
let existsUnfinished = await InspectionService.existsUnfinishedInspectionToPlan(
|
|
|
|
inspectionPlanId,
|
|
|
|
assigned,
|
|
|
|
relatedId
|
|
|
|
);
|
|
|
|
if (existsUnfinished) throw new ForbiddenRequestException("there is already an unfinished inspection existing");
|
|
|
|
|
2025-05-31 15:12:16 +02:00
|
|
|
let createInspection: CreateInspectionCommand = {
|
2025-06-04 14:30:57 +02:00
|
|
|
context,
|
|
|
|
nextInspection,
|
|
|
|
inspectionPlanId,
|
|
|
|
relatedId,
|
|
|
|
assigned,
|
2025-05-31 15:12:16 +02:00
|
|
|
};
|
|
|
|
let inspectionId = await InspectionCommandHandler.create(createInspection);
|
|
|
|
|
|
|
|
res.status(200).send(inspectionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update inspection by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function updateInspectionById(req: Request, res: Response): Promise<any> {
|
|
|
|
const inspectionId = req.params.id;
|
2025-06-04 14:30:57 +02:00
|
|
|
const context = req.body.context;
|
|
|
|
const nextInspection = req.body.nextInspection || null;
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
let updateInspection: UpdateInspectionCommand = {
|
|
|
|
id: inspectionId,
|
2025-06-04 14:30:57 +02:00
|
|
|
context,
|
|
|
|
nextInspection,
|
2025-05-31 15:12:16 +02:00
|
|
|
};
|
|
|
|
await InspectionCommandHandler.update(updateInspection);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
2025-07-11 14:02:36 +02:00
|
|
|
/**
|
|
|
|
* @description update inspection by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function updateInspectionResults(req: Request, res: Response): Promise<any> {
|
|
|
|
const inspectionId = req.params.id;
|
|
|
|
const pointResults = JSON.parse(req.body.results) as Array<{ inspectionPointId: string; value: string }>;
|
|
|
|
const pointFiles = req.files as Array<Express.Multer.File>;
|
|
|
|
|
|
|
|
let inspection = await InspectionService.getById(inspectionId);
|
|
|
|
|
|
|
|
let updateResults: Array<CreateOrUpdateInspectionPointResultCommand> = pointResults.map((pr) => ({
|
|
|
|
inspectionPointId: pr.inspectionPointId,
|
|
|
|
value:
|
|
|
|
inspection.inspectionVersionedPlan.inspectionPoints.find((ip) => ip.id == pr.inspectionPointId).type ==
|
|
|
|
InspectionPointEnum.file && pr.value == "set"
|
|
|
|
? pointFiles.find((f) => f.filename.startsWith(pr.inspectionPointId))?.filename
|
|
|
|
: pr.value,
|
|
|
|
inspectionId,
|
|
|
|
}));
|
|
|
|
await InspectionPointResultCommandHandler.createOrUpdateMultiple(updateResults);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description finish inspection by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function finishInspection(req: Request, res: Response): Promise<any> {
|
|
|
|
const inspectionId = req.params.id;
|
|
|
|
|
|
|
|
let inspection = await InspectionService.getById(inspectionId);
|
|
|
|
|
|
|
|
function getValueToInspectionPoint(inspectionPointId: string) {
|
|
|
|
return inspection.pointResults.find((c) => c.inspectionPointId == inspectionPointId)?.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
let everythingFilled = inspection.inspectionVersionedPlan.inspectionPoints.every((p) => {
|
|
|
|
if (p.type == InspectionPointEnum.file) {
|
|
|
|
return getValueToInspectionPoint(p.id);
|
|
|
|
} else if (p.type == InspectionPointEnum.oknok) {
|
|
|
|
let value = getValueToInspectionPoint(p.id);
|
|
|
|
return (["true", "false"].includes(value) ? (value as "true" | "false") : "") != "";
|
|
|
|
} else {
|
|
|
|
return !!getValueToInspectionPoint(p.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!everythingFilled) throw new ForbiddenRequestException("fill out every field before finishing inspection");
|
|
|
|
|
|
|
|
let formattedInspection = InspectionFactory.mapToSingle(inspection);
|
|
|
|
let title = `Prüf-Ausdruck_${[formattedInspection.related.code ?? "", formattedInspection.related.name].join("_")}_${
|
|
|
|
formattedInspection.inspectionPlan.title
|
|
|
|
}_${new Date(formattedInspection.finished ?? "").toLocaleDateString("de-de")}`;
|
|
|
|
|
|
|
|
await PdfExport.renderFile({
|
|
|
|
template: "inspection",
|
|
|
|
title,
|
|
|
|
filename: "printout",
|
|
|
|
folder: `inspection/${inspection.id}`,
|
|
|
|
data: {
|
|
|
|
inspector: `${req.userId}`,
|
|
|
|
context: formattedInspection.context || "---",
|
|
|
|
createdAt: formattedInspection.created,
|
|
|
|
finishedAt: formattedInspection.finished ?? new Date(),
|
|
|
|
nextInspection: formattedInspection.nextInspection,
|
|
|
|
related: formattedInspection.related,
|
|
|
|
plan: formattedInspection.inspectionPlan,
|
|
|
|
planVersion: formattedInspection.inspectionVersionedPlan.version,
|
|
|
|
planTitle: formattedInspection.inspectionPlan.title,
|
|
|
|
checks: formattedInspection.inspectionVersionedPlan.inspectionPoints
|
|
|
|
.sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0))
|
|
|
|
.map((ip) => ({
|
|
|
|
title: ip.title,
|
|
|
|
description: ip.description,
|
|
|
|
type: ip.type,
|
|
|
|
min: ip.min,
|
|
|
|
max: ip.max,
|
|
|
|
value:
|
|
|
|
ip.type == InspectionPointEnum.file
|
|
|
|
? "siehe Anhang"
|
|
|
|
: formattedInspection.checks.find((c) => c.inspectionPointId == ip.id).value,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Anhang hinzufügen
|
|
|
|
|
|
|
|
let finish: FinishInspectionCommand = {
|
|
|
|
id: inspectionId,
|
|
|
|
};
|
|
|
|
await InspectionCommandHandler.finish(finish);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
2025-05-31 15:12:16 +02:00
|
|
|
/**
|
|
|
|
* @description delete inspection by id
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function deleteInspectionById(req: Request, res: Response): Promise<any> {
|
|
|
|
const inspectionId = req.params.id;
|
|
|
|
|
2025-07-09 12:57:37 +02:00
|
|
|
let deleteInspectionData = await InspectionService.getById(inspectionId);
|
|
|
|
if (deleteInspectionData.finishedAt != null) {
|
|
|
|
throw new ForbiddenRequestException("Cannot delete as inspection is already finished");
|
|
|
|
}
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
let deleteInspection: DeleteInspectionCommand = {
|
|
|
|
id: inspectionId,
|
|
|
|
};
|
|
|
|
await InspectionCommandHandler.delete(deleteInspection);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|