104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
import { Request, Response } from "express";
|
|
import InspectionService from "../../../service/unit/inspection/inspectionService";
|
|
import InspectionFactory from "../../../factory/admin/unit/inspection/inspection";
|
|
import {
|
|
CreateInspectionCommand,
|
|
DeleteInspectionCommand,
|
|
UpdateInspectionCommand,
|
|
} from "../../../command/unit/inspection/inspectionCommand";
|
|
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
|
|
|
|
/**
|
|
* @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> {
|
|
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 noLimit = req.query.noLimit === "true";
|
|
|
|
let where = relation === "equipment" ? { equipmentId: relationId } : { vehicleId: relationId };
|
|
let [inspections, total] = await InspectionService.getAllForRelated(where, { offset, count, noLimit });
|
|
|
|
res.json({
|
|
inspections: 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 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> {
|
|
const salutationId = parseInt(req.body.salutationId);
|
|
|
|
let createInspection: CreateInspectionCommand = {
|
|
context: "",
|
|
inspectionPlanId: "",
|
|
relatedId: "",
|
|
assigned: "equipment",
|
|
};
|
|
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;
|
|
const salutationId = parseInt(req.body.salutationId);
|
|
|
|
let updateInspection: UpdateInspectionCommand = {
|
|
id: inspectionId,
|
|
context: "",
|
|
};
|
|
await InspectionCommandHandler.update(updateInspection);
|
|
|
|
res.sendStatus(204);
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
|
|
// TODO finished inspection cannot be deleted
|
|
|
|
let deleteInspection: DeleteInspectionCommand = {
|
|
id: inspectionId,
|
|
};
|
|
await InspectionCommandHandler.delete(deleteInspection);
|
|
|
|
res.sendStatus(204);
|
|
}
|