2025-05-31 15:12:16 +02:00
|
|
|
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";
|
2025-06-04 14:30:57 +02:00
|
|
|
import BadRequestException from "../../../exceptions/badRequestException";
|
2025-07-09 12:57:37 +02:00
|
|
|
import ForbiddenRequestException from "../../../exceptions/forbiddenRequestException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all inspections sorted by id not having newer inspection
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getAllInspectionsSortedNotHavingNewer(req: Request, res: Response): Promise<any> {
|
|
|
|
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 [inspections, total] = await InspectionService.getAllSortedNotHavingNewer({ offset, count, noLimit });
|
|
|
|
|
|
|
|
res.json({
|
2025-07-10 13:22:29 +02:00
|
|
|
inspections: InspectionFactory.mapToBaseNext(inspections),
|
2025-07-09 12:57:37 +02:00
|
|
|
total: total,
|
|
|
|
offset: offset,
|
|
|
|
count: count,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all inspections running
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getAllInspectionsRunning(req: Request, res: Response): Promise<any> {
|
|
|
|
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 [inspections, total] = await InspectionService.getAllRunning({ offset, count, noLimit });
|
|
|
|
|
|
|
|
res.json({
|
2025-07-10 13:22:29 +02:00
|
|
|
inspections: InspectionFactory.mapToBaseMinified(inspections),
|
2025-07-09 12:57:37 +02:00
|
|
|
total: total,
|
|
|
|
offset: offset,
|
|
|
|
count: count,
|
|
|
|
});
|
|
|
|
}
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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> {
|
2025-06-13 11:31:34 +02:00
|
|
|
let relation = req.params.related as "vehicle" | "equipment" | "wearable";
|
2025-05-31 15:12:16 +02:00
|
|
|
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";
|
|
|
|
|
2025-06-13 11:31:34 +02:00
|
|
|
let where;
|
|
|
|
if (relation == "equipment") {
|
|
|
|
where = { equipmentId: relationId };
|
|
|
|
} else if (relation == "vehicle") {
|
|
|
|
where = { vehicleId: relationId };
|
|
|
|
} else {
|
|
|
|
where = { wearableId: relationId };
|
|
|
|
}
|
2025-06-02 13:14:09 +02:00
|
|
|
let [inspections, total] = await InspectionService.getAllForRelated(where, { offset, count, noLimit });
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
res.json({
|
2025-06-04 14:30:57 +02:00
|
|
|
inspections: InspectionFactory.mapToBase(inspections),
|
2025-05-31 15:12:16 +02:00
|
|
|
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> {
|
2025-06-04 14:30:57 +02:00
|
|
|
const context = req.body.context;
|
|
|
|
const inspectionPlanId = req.body.inspectionPlanId;
|
|
|
|
const relatedId = req.body.relatedId;
|
|
|
|
const assigned = req.body.assigned;
|
|
|
|
const nextInspection = req.body.nextInspection || null;
|
|
|
|
|
2025-06-13 11:31:34 +02:00
|
|
|
if (assigned != "equipment" && assigned != "vehicle" && assigned != "wearable")
|
|
|
|
throw new BadRequestException("set assigned to equipment or vehicle or wearable");
|
2025-05-31 15:12:16 +02:00
|
|
|
|
2025-07-10 13:22:29 +02:00
|
|
|
let existsUnfinished = await InspectionService.existsUnfinishedInspectionToPlan(
|
|
|
|
inspectionPlanId,
|
|
|
|
assigned,
|
|
|
|
relatedId
|
|
|
|
);
|
|
|
|
if (existsUnfinished) throw new ForbiddenRequestException("there is already an unfinished inspection existing");
|
|
|
|
|
2025-05-31 15:12:16 +02:00
|
|
|
let createInspection: CreateInspectionCommand = {
|
2025-06-04 14:30:57 +02:00
|
|
|
context,
|
|
|
|
nextInspection,
|
|
|
|
inspectionPlanId,
|
|
|
|
relatedId,
|
|
|
|
assigned,
|
2025-05-31 15:12:16 +02:00
|
|
|
};
|
|
|
|
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;
|
2025-06-04 14:30:57 +02:00
|
|
|
const context = req.body.context;
|
|
|
|
const nextInspection = req.body.nextInspection || null;
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
let updateInspection: UpdateInspectionCommand = {
|
|
|
|
id: inspectionId,
|
2025-06-04 14:30:57 +02:00
|
|
|
context,
|
|
|
|
nextInspection,
|
2025-05-31 15:12:16 +02:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
|
2025-07-09 12:57:37 +02:00
|
|
|
let deleteInspectionData = await InspectionService.getById(inspectionId);
|
|
|
|
if (deleteInspectionData.finishedAt != null) {
|
|
|
|
throw new ForbiddenRequestException("Cannot delete as inspection is already finished");
|
|
|
|
}
|
2025-05-31 15:12:16 +02:00
|
|
|
|
|
|
|
let deleteInspection: DeleteInspectionCommand = {
|
|
|
|
id: inspectionId,
|
|
|
|
};
|
|
|
|
await InspectionCommandHandler.delete(deleteInspection);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|