diff --git a/src/command/unit/inspection/inspectionPlanCommand.ts b/src/command/unit/inspection/inspectionPlanCommand.ts index 0daac13..58593ae 100644 --- a/src/command/unit/inspection/inspectionPlanCommand.ts +++ b/src/command/unit/inspection/inspectionPlanCommand.ts @@ -5,7 +5,7 @@ export interface CreateInspectionPlanCommand { inspectionInterval: PlanTimeDefinition; remindTime: PlanTimeDefinition; relatedId: string; - assigned: "vehicle" | "equipment"; + assigned: "vehicle" | "equipment" | "wearable"; } export interface UpdateInspectionPlanCommand { diff --git a/src/command/unit/inspection/inspectionPlanCommandHandler.ts b/src/command/unit/inspection/inspectionPlanCommandHandler.ts index 179f8a7..e140f64 100644 --- a/src/command/unit/inspection/inspectionPlanCommandHandler.ts +++ b/src/command/unit/inspection/inspectionPlanCommandHandler.ts @@ -24,6 +24,7 @@ export default abstract class InspectionPlanCommandHandler { remindTime: createInspectionPlan.remindTime, equipmentTypeId: createInspectionPlan.assigned == "equipment" ? createInspectionPlan.relatedId : null, vehicleTypeId: createInspectionPlan.assigned == "vehicle" ? createInspectionPlan.relatedId : null, + wearableTypeId: createInspectionPlan.assigned == "wearable" ? createInspectionPlan.relatedId : null, }) .execute() .then((result) => { diff --git a/src/controller/admin/unit/inspectionController.ts b/src/controller/admin/unit/inspectionController.ts index 7307820..211ea10 100644 --- a/src/controller/admin/unit/inspectionController.ts +++ b/src/controller/admin/unit/inspectionController.ts @@ -18,6 +18,7 @@ import { PdfExport } from "../../../helpers/pdfExport"; import { PDFDocument } from "pdf-lib"; import sharp from "sharp"; 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 @@ -168,9 +169,15 @@ export async function createInspection(req: Request, res: Response): Promise { const title = req.body.title; - const inspectionInterval = req.body.inspectionInterval; - const remindTime = req.body.remindTime; + const inspectionInterval = req.body.inspectionInterval || null; + const remindTime = req.body.remindTime || null; const relatedId = req.body.relatedId; const assigned = req.body.assigned; - TypeTester.testPlanTimeDefinition(inspectionInterval, "inspectionInterval", true); - TypeTester.testPlanTimeDefinition(remindTime, "remindTime", true); + TypeTester.testPlanTimeDefinition(inspectionInterval, { key: "inspectionInterval", throwErr: true, allowNull: true }); + TypeTester.testPlanTimeDefinition(remindTime, { key: "inspectionInterval", throwErr: true, allowNull: true }); 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 = { title, @@ -155,11 +157,11 @@ export async function createInspectionPlan(req: Request, res: Response): Promise export async function updateInspectionPlanById(req: Request, res: Response): Promise { const inspectionPlanId = req.params.id; const title = req.body.title; - const inspectionInterval = req.body.inspectionInterval; - const remindTime = req.body.remindTime; + const inspectionInterval = req.body.inspectionInterval || null; + const remindTime = req.body.remindTime || null; - TypeTester.testPlanTimeDefinition(inspectionInterval, "inspectionInterval", true); - TypeTester.testPlanTimeDefinition(remindTime, "remindTime", true); + TypeTester.testPlanTimeDefinition(inspectionInterval, { key: "inspectionInterval", throwErr: true, allowNull: true }); + TypeTester.testPlanTimeDefinition(remindTime, { key: "inspectionInterval", throwErr: true, allowNull: true }); let updateInspectionPlan: UpdateInspectionPlanCommand = { id: inspectionPlanId, diff --git a/src/entity/unit/inspection/inspectionPlan.ts b/src/entity/unit/inspection/inspectionPlan.ts index 9fa6fad..3b954a4 100644 --- a/src/entity/unit/inspection/inspectionPlan.ts +++ b/src/entity/unit/inspection/inspectionPlan.ts @@ -13,11 +13,11 @@ export class inspectionPlan { @Column({ type: "varchar", length: 255 }) title: string; - @Column({ type: "varchar", length: 255 }) - inspectionInterval: PlanTimeDefinition; + @Column({ type: "varchar", length: 255, nullable: true, default: null }) + inspectionInterval?: PlanTimeDefinition; - @Column({ type: "varchar", length: 255 }) - remindTime: PlanTimeDefinition; + @Column({ type: "varchar", length: 255, nullable: true, default: null }) + remindTime?: PlanTimeDefinition; @CreateDateColumn() createdAt: Date; diff --git a/src/helpers/typeTester.ts b/src/helpers/typeTester.ts index ab37808..d81f8eb 100644 --- a/src/helpers/typeTester.ts +++ b/src/helpers/typeTester.ts @@ -1,7 +1,11 @@ import { PlanTimeDefinition } from "../viewmodel/admin/unit/inspection/inspectionPlan.models"; 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)) { return val as PlanTimeDefinition; } else if (throwErr) { diff --git a/src/migrations/baseSchemaTables/inspection.ts b/src/migrations/baseSchemaTables/inspection.ts index 76ca627..6a71127 100644 --- a/src/migrations/baseSchemaTables/inspection.ts +++ b/src/migrations/baseSchemaTables/inspection.ts @@ -6,8 +6,8 @@ export const inspection_plan_table = new Table({ columns: [ { name: "id", ...getTypeByORM("uuid"), ...isUUIDPrimary }, { name: "title", ...getTypeByORM("varchar") }, - { name: "inspectionInterval", ...getTypeByORM("varchar") }, - { name: "remindTime", ...getTypeByORM("varchar") }, + { name: "inspectionInterval", ...getTypeByORM("varchar", true) }, + { name: "remindTime", ...getTypeByORM("varchar", true) }, { name: "createdAt", ...getTypeByORM("datetime"), default: getDefaultByORM("currentTimestamp") }, { name: "equipmentTypeId", ...getTypeByORM("uuid", true) }, { name: "vehicleTypeId", ...getTypeByORM("uuid", true) },