extend inspection_plan by wearable and enable optional interval

This commit is contained in:
Julian Krauser 2025-07-25 12:21:40 +02:00
parent 9b38c6a5e9
commit 063b949ae1
7 changed files with 31 additions and 17 deletions

View file

@ -5,7 +5,7 @@ export interface CreateInspectionPlanCommand {
inspectionInterval: PlanTimeDefinition; inspectionInterval: PlanTimeDefinition;
remindTime: PlanTimeDefinition; remindTime: PlanTimeDefinition;
relatedId: string; relatedId: string;
assigned: "vehicle" | "equipment"; assigned: "vehicle" | "equipment" | "wearable";
} }
export interface UpdateInspectionPlanCommand { export interface UpdateInspectionPlanCommand {

View file

@ -24,6 +24,7 @@ export default abstract class InspectionPlanCommandHandler {
remindTime: createInspectionPlan.remindTime, remindTime: createInspectionPlan.remindTime,
equipmentTypeId: createInspectionPlan.assigned == "equipment" ? createInspectionPlan.relatedId : null, equipmentTypeId: createInspectionPlan.assigned == "equipment" ? createInspectionPlan.relatedId : null,
vehicleTypeId: createInspectionPlan.assigned == "vehicle" ? createInspectionPlan.relatedId : null, vehicleTypeId: createInspectionPlan.assigned == "vehicle" ? createInspectionPlan.relatedId : null,
wearableTypeId: createInspectionPlan.assigned == "wearable" ? createInspectionPlan.relatedId : null,
}) })
.execute() .execute()
.then((result) => { .then((result) => {

View file

@ -18,6 +18,7 @@ import { PdfExport } from "../../../helpers/pdfExport";
import { PDFDocument } from "pdf-lib"; import { PDFDocument } from "pdf-lib";
import sharp from "sharp"; import sharp from "sharp";
import InspectionPointResultService from "../../../service/unit/inspection/inspectionPointResultService"; import InspectionPointResultService from "../../../service/unit/inspection/inspectionPointResultService";
import InspectionPlanService from "../../../service/unit/inspection/inspectionPlanService";
/** /**
* @description get all inspections sorted by id not having newer inspection * @description get all inspections sorted by id not having newer inspection
@ -168,9 +169,15 @@ export async function createInspection(req: Request, res: Response): Promise<any
const assigned = req.body.assigned; const assigned = req.body.assigned;
const nextInspection = req.body.nextInspection || null; const nextInspection = req.body.nextInspection || null;
let inspectionPlan = await InspectionPlanService.getById(inspectionPlanId);
if (inspectionPlan.inspectionInterval && !nextInspection)
throw new BadRequestException("inspection has to have nextInspection date");
if (assigned != "equipment" && assigned != "vehicle" && assigned != "wearable") if (assigned != "equipment" && assigned != "vehicle" && assigned != "wearable")
throw new BadRequestException("set assigned to equipment or vehicle or wearable"); throw new BadRequestException("set assigned to equipment or vehicle or wearable");
if (relatedId == null) throw new BadRequestException("provide related equipment or vehicle or wearable");
let existsUnfinished = await InspectionService.existsUnfinishedInspectionToPlan( let existsUnfinished = await InspectionService.existsUnfinishedInspectionToPlan(
inspectionPlanId, inspectionPlanId,
assigned, assigned,

View file

@ -123,16 +123,18 @@ export async function getInspectionPlanById(req: Request, res: Response): Promis
*/ */
export async function createInspectionPlan(req: Request, res: Response): Promise<any> { export async function createInspectionPlan(req: Request, res: Response): Promise<any> {
const title = req.body.title; const title = req.body.title;
const inspectionInterval = req.body.inspectionInterval; const inspectionInterval = req.body.inspectionInterval || null;
const remindTime = req.body.remindTime; const remindTime = req.body.remindTime || null;
const relatedId = req.body.relatedId; const relatedId = req.body.relatedId;
const assigned = req.body.assigned; const assigned = req.body.assigned;
TypeTester.testPlanTimeDefinition(inspectionInterval, "inspectionInterval", true); TypeTester.testPlanTimeDefinition(inspectionInterval, { key: "inspectionInterval", throwErr: true, allowNull: true });
TypeTester.testPlanTimeDefinition(remindTime, "remindTime", true); TypeTester.testPlanTimeDefinition(remindTime, { key: "inspectionInterval", throwErr: true, allowNull: true });
if (assigned != "equipment" && assigned != "vehicle" && assigned != "wearable") if (assigned != "equipment" && assigned != "vehicle" && assigned != "wearable")
throw new BadRequestException("set assigned to equipment or vehicle or wearable"); throw new BadRequestException("set assigned to equipmenttype or vehicletype or wearabletype");
if (relatedId == null) throw new BadRequestException("provide related equipmenttype or vehicletype or wearabletype");
let createInspectionPlan: CreateInspectionPlanCommand = { let createInspectionPlan: CreateInspectionPlanCommand = {
title, title,
@ -155,11 +157,11 @@ export async function createInspectionPlan(req: Request, res: Response): Promise
export async function updateInspectionPlanById(req: Request, res: Response): Promise<any> { export async function updateInspectionPlanById(req: Request, res: Response): Promise<any> {
const inspectionPlanId = req.params.id; const inspectionPlanId = req.params.id;
const title = req.body.title; const title = req.body.title;
const inspectionInterval = req.body.inspectionInterval; const inspectionInterval = req.body.inspectionInterval || null;
const remindTime = req.body.remindTime; const remindTime = req.body.remindTime || null;
TypeTester.testPlanTimeDefinition(inspectionInterval, "inspectionInterval", true); TypeTester.testPlanTimeDefinition(inspectionInterval, { key: "inspectionInterval", throwErr: true, allowNull: true });
TypeTester.testPlanTimeDefinition(remindTime, "remindTime", true); TypeTester.testPlanTimeDefinition(remindTime, { key: "inspectionInterval", throwErr: true, allowNull: true });
let updateInspectionPlan: UpdateInspectionPlanCommand = { let updateInspectionPlan: UpdateInspectionPlanCommand = {
id: inspectionPlanId, id: inspectionPlanId,

View file

@ -13,11 +13,11 @@ export class inspectionPlan {
@Column({ type: "varchar", length: 255 }) @Column({ type: "varchar", length: 255 })
title: string; title: string;
@Column({ type: "varchar", length: 255 }) @Column({ type: "varchar", length: 255, nullable: true, default: null })
inspectionInterval: PlanTimeDefinition; inspectionInterval?: PlanTimeDefinition;
@Column({ type: "varchar", length: 255 }) @Column({ type: "varchar", length: 255, nullable: true, default: null })
remindTime: PlanTimeDefinition; remindTime?: PlanTimeDefinition;
@CreateDateColumn() @CreateDateColumn()
createdAt: Date; createdAt: Date;

View file

@ -1,7 +1,11 @@
import { PlanTimeDefinition } from "../viewmodel/admin/unit/inspection/inspectionPlan.models"; import { PlanTimeDefinition } from "../viewmodel/admin/unit/inspection/inspectionPlan.models";
export default abstract class TypeTester { export default abstract class TypeTester {
static testPlanTimeDefinition(val: string, key: string = "", throwErr: boolean = false): PlanTimeDefinition | null { static testPlanTimeDefinition(
val: string | null,
{ key = "", throwErr = false, allowNull = false }: { key?: string; throwErr?: boolean; allowNull?: boolean }
): PlanTimeDefinition | null {
if (val == null && allowNull) return null;
if (/^(\d+-(d|m|y)|\d+\/(\d+|\*))$/.test(val)) { if (/^(\d+-(d|m|y)|\d+\/(\d+|\*))$/.test(val)) {
return val as PlanTimeDefinition; return val as PlanTimeDefinition;
} else if (throwErr) { } else if (throwErr) {

View file

@ -6,8 +6,8 @@ export const inspection_plan_table = new Table({
columns: [ columns: [
{ name: "id", ...getTypeByORM("uuid"), ...isUUIDPrimary }, { name: "id", ...getTypeByORM("uuid"), ...isUUIDPrimary },
{ name: "title", ...getTypeByORM("varchar") }, { name: "title", ...getTypeByORM("varchar") },
{ name: "inspectionInterval", ...getTypeByORM("varchar") }, { name: "inspectionInterval", ...getTypeByORM("varchar", true) },
{ name: "remindTime", ...getTypeByORM("varchar") }, { name: "remindTime", ...getTypeByORM("varchar", true) },
{ name: "createdAt", ...getTypeByORM("datetime"), default: getDefaultByORM("currentTimestamp") }, { name: "createdAt", ...getTypeByORM("datetime"), default: getDefaultByORM("currentTimestamp") },
{ name: "equipmentTypeId", ...getTypeByORM("uuid", true) }, { name: "equipmentTypeId", ...getTypeByORM("uuid", true) },
{ name: "vehicleTypeId", ...getTypeByORM("uuid", true) }, { name: "vehicleTypeId", ...getTypeByORM("uuid", true) },