inspection finish and print
This commit is contained in:
parent
0f3e4488f4
commit
9ef82adef7
12 changed files with 358 additions and 76 deletions
|
@ -4,11 +4,18 @@ import InspectionFactory from "../../../factory/admin/unit/inspection/inspection
|
|||
import {
|
||||
CreateInspectionCommand,
|
||||
DeleteInspectionCommand,
|
||||
FinishInspectionCommand,
|
||||
UpdateInspectionCommand,
|
||||
} from "../../../command/unit/inspection/inspectionCommand";
|
||||
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
|
||||
import BadRequestException from "../../../exceptions/badRequestException";
|
||||
import ForbiddenRequestException from "../../../exceptions/forbiddenRequestException";
|
||||
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";
|
||||
|
||||
/**
|
||||
* @description get all inspections sorted by id not having newer inspection
|
||||
|
@ -76,13 +83,35 @@ export async function getAllInspectionsForRelated(req: Request, res: Response):
|
|||
let [inspections, total] = await InspectionService.getAllForRelated(where, { offset, count, noLimit });
|
||||
|
||||
res.json({
|
||||
inspections: InspectionFactory.mapToBase(inspections),
|
||||
inspections: InspectionFactory.mapToBaseMinified(inspections),
|
||||
total: total,
|
||||
offset: offset,
|
||||
count: count,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get inspection by id
|
||||
* @param req {Request} Express req object
|
||||
|
@ -152,6 +181,106 @@ export async function updateInspectionById(req: Request, res: Response): Promise
|
|||
res.sendStatus(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete inspection by id
|
||||
* @param req {Request} Express req object
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue