unit/#102-base-management #121
6 changed files with 189 additions and 21 deletions
|
@ -1,6 +1,8 @@
|
||||||
|
import { Not } from "typeorm";
|
||||||
import { dataSource } from "../../../data-source";
|
import { dataSource } from "../../../data-source";
|
||||||
import { inspection } from "../../../entity/unit/inspection/inspection";
|
import { inspection } from "../../../entity/unit/inspection/inspection";
|
||||||
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
||||||
|
import InspectionService from "../../../service/unit/inspection/inspectionService";
|
||||||
import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService";
|
import InspectionVersionedPlanService from "../../../service/unit/inspection/inspectionVersionedPlanService";
|
||||||
import { CreateInspectionCommand, UpdateInspectionCommand, DeleteInspectionCommand } from "./inspectionCommand";
|
import { CreateInspectionCommand, UpdateInspectionCommand, DeleteInspectionCommand } from "./inspectionCommand";
|
||||||
|
|
||||||
|
@ -8,13 +10,24 @@ export default abstract class InspectionCommandHandler {
|
||||||
/**
|
/**
|
||||||
* @description create inspection
|
* @description create inspection
|
||||||
* @param {CreateInspectionCommand} createInspection
|
* @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(
|
let latestVersionedPlan = await InspectionVersionedPlanService.getLatestForInspectionPlan(
|
||||||
createInspection.inspectionPlanId
|
createInspection.inspectionPlanId
|
||||||
);
|
);
|
||||||
|
let insertId = "";
|
||||||
return await dataSource
|
return await dataSource
|
||||||
|
.transaction(async (manager) => {
|
||||||
|
await manager
|
||||||
|
.createQueryBuilder()
|
||||||
|
.update(inspection)
|
||||||
|
.set({
|
||||||
|
hasNewer: true,
|
||||||
|
})
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await manager
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into(inspection)
|
.into(inspection)
|
||||||
|
@ -28,7 +41,11 @@ export default abstract class InspectionCommandHandler {
|
||||||
})
|
})
|
||||||
.execute()
|
.execute()
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
return result.identifiers[0].id;
|
insertId = result.identifiers[0].id;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
return insertId;
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw new DatabaseActionException("CREATE", "inspection", err);
|
throw new DatabaseActionException("CREATE", "inspection", err);
|
||||||
|
@ -62,12 +79,42 @@ export default abstract class InspectionCommandHandler {
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
static async delete(deleteInspection: DeleteInspectionCommand): Promise<void> {
|
static async delete(deleteInspection: DeleteInspectionCommand): Promise<void> {
|
||||||
|
let deleteInspectionData = await InspectionService.getById(deleteInspection.id);
|
||||||
return await dataSource
|
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()
|
.createQueryBuilder()
|
||||||
.delete()
|
.delete()
|
||||||
.from(inspection)
|
.from(inspection)
|
||||||
.where("id = :id", { id: deleteInspection.id })
|
.where("id = :id", { id: deleteInspection.id })
|
||||||
.execute()
|
.execute();
|
||||||
|
})
|
||||||
.then(() => {})
|
.then(() => {})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw new DatabaseActionException("DELETE", "inspection", err);
|
throw new DatabaseActionException("DELETE", "inspection", err);
|
||||||
|
|
|
@ -8,6 +8,49 @@ import {
|
||||||
} from "../../../command/unit/inspection/inspectionCommand";
|
} from "../../../command/unit/inspection/inspectionCommand";
|
||||||
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
|
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
|
||||||
import BadRequestException from "../../../exceptions/badRequestException";
|
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
|
* @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> {
|
export async function deleteInspectionById(req: Request, res: Response): Promise<any> {
|
||||||
const inspectionId = req.params.id;
|
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 = {
|
let deleteInspection: DeleteInspectionCommand = {
|
||||||
id: inspectionId,
|
id: inspectionId,
|
||||||
|
|
|
@ -24,6 +24,9 @@ export class inspection {
|
||||||
@Column({ type: getTypeByORM("date").type as ColumnType, nullable: true, default: null })
|
@Column({ type: getTypeByORM("date").type as ColumnType, nullable: true, default: null })
|
||||||
nextInspection?: Date;
|
nextInspection?: Date;
|
||||||
|
|
||||||
|
@Column({ type: "boolean", default: false })
|
||||||
|
hasNewer: boolean;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
inspectionPlanId: string;
|
inspectionPlanId: string;
|
||||||
|
|
||||||
|
|
|
@ -94,6 +94,7 @@ export const inspection_table = new Table({
|
||||||
{ name: "createdAt", ...getTypeByORM("datetime"), default: getDefaultByORM("currentTimestamp") },
|
{ name: "createdAt", ...getTypeByORM("datetime"), default: getDefaultByORM("currentTimestamp") },
|
||||||
{ name: "finishedAt", ...getTypeByORM("date", true) },
|
{ name: "finishedAt", ...getTypeByORM("date", true) },
|
||||||
{ name: "nextInspection", ...getTypeByORM("date", true) },
|
{ name: "nextInspection", ...getTypeByORM("date", true) },
|
||||||
|
{ name: "hasNewer", ...getTypeByORM("boolean"), default: getDefaultByORM("boolean", false) },
|
||||||
{ name: "inspectionPlanId", ...getTypeByORM("uuid") },
|
{ name: "inspectionPlanId", ...getTypeByORM("uuid") },
|
||||||
{ name: "inspectionVersionedPlanId", ...getTypeByORM("uuid") },
|
{ name: "inspectionVersionedPlanId", ...getTypeByORM("uuid") },
|
||||||
{ name: "equipmentId", ...getTypeByORM("uuid", true) },
|
{ name: "equipmentId", ...getTypeByORM("uuid", true) },
|
||||||
|
|
|
@ -3,13 +3,23 @@ import PermissionHelper from "../../../helpers/permissionHelper";
|
||||||
import {
|
import {
|
||||||
createInspection,
|
createInspection,
|
||||||
deleteInspectionById,
|
deleteInspectionById,
|
||||||
|
getAllInspectionsSortedNotHavingNewer,
|
||||||
getAllInspectionsForRelated,
|
getAllInspectionsForRelated,
|
||||||
getInspectionById,
|
getInspectionById,
|
||||||
updateInspectionById,
|
updateInspectionById,
|
||||||
|
getAllInspectionsRunning,
|
||||||
} from "../../../controller/admin/unit/inspectionController";
|
} from "../../../controller/admin/unit/inspectionController";
|
||||||
|
|
||||||
var router = express.Router({ mergeParams: true });
|
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(
|
router.get(
|
||||||
["/vehicle/:relatedId", "/equipment/:relatedId", "/wearable/:relatedId"],
|
["/vehicle/:relatedId", "/equipment/:relatedId", "/wearable/:relatedId"],
|
||||||
async (req: Request, res: Response) => {
|
async (req: Request, res: Response) => {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { IsNull } from "typeorm";
|
||||||
import { dataSource } from "../../../data-source";
|
import { dataSource } from "../../../data-source";
|
||||||
import { inspection } from "../../../entity/unit/inspection/inspection";
|
import { inspection } from "../../../entity/unit/inspection/inspection";
|
||||||
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
||||||
|
@ -16,6 +17,66 @@ export default abstract class InspectionService {
|
||||||
.leftJoinAndSelect("inspection.vehicle", "vehicle")
|
.leftJoinAndSelect("inspection.vehicle", "vehicle")
|
||||||
.leftJoinAndSelect("inspection.wearable", "wearable");
|
.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
|
* @description get all inspections for related
|
||||||
* @returns {Promise<Array<inspection>>}
|
* @returns {Promise<Array<inspection>>}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue