unit/#102-base-management #121

Merged
jkeffects merged 35 commits from unit/#102-base-management into milestone/ff-admin-unit 2025-07-14 13:36:35 +00:00
6 changed files with 189 additions and 21 deletions
Showing only changes of commit 95b6cec66d - Show all commits

View file

@ -1,6 +1,8 @@
import { Not } from "typeorm";
import { dataSource } from "../../../data-source";
import { inspection } from "../../../entity/unit/inspection/inspection";
import DatabaseActionException from "../../../exceptions/databaseActionException";
import InspectionService from "../../../service/unit/inspection/inspectionService";
import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService";
import { CreateInspectionCommand, UpdateInspectionCommand, DeleteInspectionCommand } from "./inspectionCommand";
@ -8,13 +10,24 @@ export default abstract class InspectionCommandHandler {
/**
* @description create inspection
* @param {CreateInspectionCommand} createInspection
* @returns {Promise<number>}
* @returns {Promise<string>}
*/
static async create(createInspection: CreateInspectionCommand): Promise<number> {
static async create(createInspection: CreateInspectionCommand): Promise<string> {
let latestVersionedPlan = await InspectionVersionedPlanService.getLatestForInspectionPlan(
createInspection.inspectionPlanId
);
let insertId = "";
return await dataSource
.transaction(async (manager) => {
await manager
.createQueryBuilder()
.update(inspection)
.set({
hasNewer: true,
})
.execute();
await manager
.createQueryBuilder()
.insert()
.into(inspection)
@ -28,7 +41,11 @@ export default abstract class InspectionCommandHandler {
})
.execute()
.then((result) => {
return result.identifiers[0].id;
insertId = result.identifiers[0].id;
});
})
.then(() => {
return insertId;
})
.catch((err) => {
throw new DatabaseActionException("CREATE", "inspection", err);
@ -62,12 +79,42 @@ export default abstract class InspectionCommandHandler {
* @returns {Promise<void>}
*/
static async delete(deleteInspection: DeleteInspectionCommand): Promise<void> {
let deleteInspectionData = await InspectionService.getById(deleteInspection.id);
return await dataSource
.transaction(async (manager) => {
await manager
.createQueryBuilder()
.update(inspection)
.set({
hasNewer: false,
})
.where((qb) => {
const subQuery = qb
.createQueryBuilder()
.select("id")
.from(inspection, "sub")
.where({
inspectionPlanId: deleteInspectionData.inspectionPlanId,
inspectionVersionedPlanId: deleteInspectionData.inspectionVersionedPlanId,
equipmentId: deleteInspectionData.equipmentId,
vehicleId: deleteInspectionData.vehicleId,
wearableId: deleteInspectionData.wearableId,
})
.andWhere({ id: Not(deleteInspection.id) })
.orderBy("sub.createdAt", "DESC")
.limit(1)
.getQuery();
return "id = " + subQuery;
})
.execute();
await manager
.createQueryBuilder()
.delete()
.from(inspection)
.where("id = :id", { id: deleteInspection.id })
.execute()
.execute();
})
.then(() => {})
.catch((err) => {
throw new DatabaseActionException("DELETE", "inspection", err);

View file

@ -8,6 +8,49 @@ import {
} from "../../../command/unit/inspection/inspectionCommand";
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
import BadRequestException from "../../../exceptions/badRequestException";
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({
inspections: InspectionFactory.mapToBase(inspections),
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({
inspections: InspectionFactory.mapToBase(inspections),
total: total,
offset: offset,
count: count,
});
}
/**
* @description get all inspections for related id
@ -111,7 +154,10 @@ export async function updateInspectionById(req: Request, res: Response): Promise
export async function deleteInspectionById(req: Request, res: Response): Promise<any> {
const inspectionId = req.params.id;
// TODO finished inspection cannot be deleted
let deleteInspectionData = await InspectionService.getById(inspectionId);
if (deleteInspectionData.finishedAt != null) {
throw new ForbiddenRequestException("Cannot delete as inspection is already finished");
}
let deleteInspection: DeleteInspectionCommand = {
id: inspectionId,

View file

@ -24,6 +24,9 @@ export class inspection {
@Column({ type: getTypeByORM("date").type as ColumnType, nullable: true, default: null })
nextInspection?: Date;
@Column({ type: "boolean", default: false })
hasNewer: boolean;
@Column()
inspectionPlanId: string;

View file

@ -94,6 +94,7 @@ export const inspection_table = new Table({
{ name: "createdAt", ...getTypeByORM("datetime"), default: getDefaultByORM("currentTimestamp") },
{ name: "finishedAt", ...getTypeByORM("date", true) },
{ name: "nextInspection", ...getTypeByORM("date", true) },
{ name: "hasNewer", ...getTypeByORM("boolean"), default: getDefaultByORM("boolean", false) },
{ name: "inspectionPlanId", ...getTypeByORM("uuid") },
{ name: "inspectionVersionedPlanId", ...getTypeByORM("uuid") },
{ name: "equipmentId", ...getTypeByORM("uuid", true) },

View file

@ -3,13 +3,23 @@ import PermissionHelper from "../../../helpers/permissionHelper";
import {
createInspection,
deleteInspectionById,
getAllInspectionsSortedNotHavingNewer,
getAllInspectionsForRelated,
getInspectionById,
updateInspectionById,
getAllInspectionsRunning,
} from "../../../controller/admin/unit/inspectionController";
var router = express.Router({ mergeParams: true });
router.get("/next", async (req: Request, res: Response) => {
await getAllInspectionsSortedNotHavingNewer(req, res);
});
router.get("/running", async (req: Request, res: Response) => {
await getAllInspectionsRunning(req, res);
});
router.get(
["/vehicle/:relatedId", "/equipment/:relatedId", "/wearable/:relatedId"],
async (req: Request, res: Response) => {

View file

@ -1,3 +1,4 @@
import { IsNull } from "typeorm";
import { dataSource } from "../../../data-source";
import { inspection } from "../../../entity/unit/inspection/inspection";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -16,6 +17,66 @@ export default abstract class InspectionService {
.leftJoinAndSelect("inspection.vehicle", "vehicle")
.leftJoinAndSelect("inspection.wearable", "wearable");
/**
* @description get all inspections sorted by next inspection not having newer
* @returns {Promise<Array<inspection>>}
*/
static async getAllSortedNotHavingNewer({
offset = 0,
count = 25,
noLimit = false,
}: {
offset?: number;
count?: number;
noLimit?: boolean;
}): Promise<[Array<inspection>, number]> {
let query = this.query().where({ hasNewer: false });
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("inspection.nextInspection", "ASC", "NULLS LAST")
.getManyAndCount()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "inspection", err);
});
}
/**
* @description get all inspections running
* @returns {Promise<Array<inspection>>}
*/
static async getAllRunning({
offset = 0,
count = 25,
noLimit = false,
}: {
offset?: number;
count?: number;
noLimit?: boolean;
}): Promise<[Array<inspection>, number]> {
let query = this.query().where({ finishedAt: IsNull() });
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("inspection.createdAt", "ASC")
.getManyAndCount()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "inspection", err);
});
}
/**
* @description get all inspections for related
* @returns {Promise<Array<inspection>>}