112 lines
3.6 KiB
TypeScript
112 lines
3.6 KiB
TypeScript
import { Request, Response } from "express";
|
|
import InspectionPlanService from "../../../service/unit/inspection/inspectionPlanService";
|
|
import InspectionPlanFactory from "../../../factory/admin/unit/inspection/inspectionPlan";
|
|
import {
|
|
CreateInspectionPlanCommand,
|
|
DeleteInspectionPlanCommand,
|
|
UpdateInspectionPlanCommand,
|
|
} from "../../../command/unit/inspection/inspectionPlanCommand";
|
|
import InspectionPlanCommandHandler from "../../../command/unit/inspection/inspectionPlanCommandHandler";
|
|
|
|
/**
|
|
* @description get all inspectionPlans
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getAllInspectionPlansForRelated(req: Request, res: Response): Promise<any> {
|
|
let relation = req.params.related as "vehicle" | "equipment";
|
|
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 search = (req.query.search as string) ?? "";
|
|
let noLimit = req.query.noLimit === "true";
|
|
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
|
|
|
|
let where = relation === "equipment" ? { equipmentId: relationId } : { vehicleId: relationId };
|
|
let [inspectionPlans, total] = await InspectionPlanService.getAllForRelated(where, {
|
|
offset,
|
|
count,
|
|
search,
|
|
noLimit,
|
|
ids,
|
|
});
|
|
|
|
res.json({
|
|
inspectionPlans: inspectionPlans,
|
|
total: total,
|
|
offset: offset,
|
|
count: count,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get inspectionPlan by id
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getInspectionPlanById(req: Request, res: Response): Promise<any> {
|
|
const inspectionPlanId = req.params.id;
|
|
let inspectionPlan = await InspectionPlanService.getById(inspectionPlanId);
|
|
|
|
res.json(InspectionPlanFactory.mapToSingle(inspectionPlan));
|
|
}
|
|
|
|
/**
|
|
* @description create inspectionPlan
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function createInspectionPlan(req: Request, res: Response): Promise<any> {
|
|
const salutationId = parseInt(req.body.salutationId);
|
|
|
|
let createInspectionPlan: CreateInspectionPlanCommand = {
|
|
title: "",
|
|
inspectionInterval: "1-m",
|
|
remindTime: "1-m",
|
|
relatedId: "",
|
|
assigned: "equipment",
|
|
};
|
|
let inspectionPlanId = await InspectionPlanCommandHandler.create(createInspectionPlan);
|
|
|
|
res.status(200).send(inspectionPlanId);
|
|
}
|
|
|
|
/**
|
|
* @description update inspectionPlan by id
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function updateInspectionPlanById(req: Request, res: Response): Promise<any> {
|
|
const inspectionPlanId = req.params.id;
|
|
const salutationId = parseInt(req.body.salutationId);
|
|
|
|
let updateInspectionPlan: UpdateInspectionPlanCommand = {
|
|
id: inspectionPlanId,
|
|
title: "",
|
|
inspectionInterval: "1-m",
|
|
};
|
|
await InspectionPlanCommandHandler.update(updateInspectionPlan);
|
|
|
|
res.sendStatus(204);
|
|
}
|
|
|
|
/**
|
|
* @description delete inspectionPlan by id
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function deleteInspectionPlanById(req: Request, res: Response): Promise<any> {
|
|
const inspectionPlanId = req.params.id;
|
|
|
|
let deleteInspectionPlan: DeleteInspectionPlanCommand = {
|
|
id: inspectionPlanId,
|
|
};
|
|
await InspectionPlanCommandHandler.delete(deleteInspectionPlan);
|
|
|
|
res.sendStatus(204);
|
|
}
|