change according to connection from frontend

This commit is contained in:
Julian Krauser 2025-06-04 14:30:57 +02:00
parent 2609ecc1bf
commit e3db523a0e
36 changed files with 611 additions and 173 deletions

View file

@ -22,8 +22,8 @@ export default abstract class InspectionPlanCommandHandler {
title: createInspectionPlan.title,
inspectionInterval: createInspectionPlan.inspectionInterval,
remindTime: createInspectionPlan.remindTime,
equipmentId: createInspectionPlan.assigned == "equipment" ? createInspectionPlan.relatedId : null,
vehicleId: createInspectionPlan.assigned == "vehicle" ? createInspectionPlan.relatedId : null,
equipmentTypeId: createInspectionPlan.assigned == "equipment" ? createInspectionPlan.relatedId : null,
vehicleTypeId: createInspectionPlan.assigned == "vehicle" ? createInspectionPlan.relatedId : null,
})
.execute()
.then((result) => {

View file

@ -0,0 +1,113 @@
import { Request, Response } from "express";
import DamageReportService from "../../../service/unit/damageReportService";
import DamageReportFactory from "../../../factory/admin/unit/damageReport";
import { CreateDamageReportCommand, UpdateDamageReportCommand } from "../../../command/unit/damageReportCommand";
import DamageReportCommandHandler from "../../../command/unit/damageReportCommandHandler";
import BadRequestException from "../../../exceptions/badRequestException";
/**
* @description get all damageReports by status
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getAllDamageReportsByStatus(req: Request, res: Response): Promise<any> {
let done = req.query.done === "true";
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 [damageReports, total] = await DamageReportService.getAll(done, { offset, count, noLimit });
res.json({
damageReports: DamageReportFactory.mapToBase(damageReports),
total: total,
offset: offset,
count: count,
});
}
/**
* @description get all damageReports for related id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getAllDamageReportsForRelated(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 [damageReports, total] = await DamageReportService.getAllForRelated(where, { offset, count, noLimit });
res.json({
damageReports: DamageReportFactory.mapToBase(damageReports),
total: total,
offset: offset,
count: count,
});
}
/**
* @description get damageReport by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getDamageReportById(req: Request, res: Response): Promise<any> {
const damageReportId = req.params.id;
let damageReport = await DamageReportService.getById(damageReportId);
res.json(DamageReportFactory.mapToSingle(damageReport));
}
/**
* @description create damageReport
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function createDamageReport(req: Request, res: Response): Promise<any> {
const description = req.body.description;
const reportedBy = req.body.reportedBy;
const affectedId = req.body.affectedId;
const affected = req.body.affected;
if (affected != "equipment" && affected != "vehicle" && affected != "wearable")
throw new BadRequestException("set assigned to equipment or vehicle or wearable");
let createDamageReport: CreateDamageReportCommand = {
description,
reportedBy,
imageCount: 0,
affectedId,
affected,
};
let damageReportId = await DamageReportCommandHandler.create(createDamageReport);
res.status(200).send(damageReportId);
}
/**
* @description update damageReport by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function updateDamageReportById(req: Request, res: Response): Promise<any> {
const damageReportId = req.params.id;
const status = req.body.status;
const done = req.body.done;
let updateDamageReport: UpdateDamageReportCommand = {
id: damageReportId,
status,
done,
};
await DamageReportCommandHandler.update(updateDamageReport);
res.sendStatus(204);
}

View file

@ -24,7 +24,7 @@ export async function getAllEquipments(req: Request, res: Response): Promise<any
let [equipments, total] = await EquipmentService.getAll({ offset, count, search, noLimit, ids });
res.json({
equipments: equipments,
equipments: EquipmentFactory.mapToBase(equipments),
total: total,
offset: offset,
count: count,
@ -70,13 +70,18 @@ export async function getEquipmentsByIds(req: Request, res: Response): Promise<a
* @returns {Promise<*>}
*/
export async function createEquipment(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const equipmentTypeId = req.body.equipmentTypeId;
let createEquipment: CreateEquipmentCommand = {
name: "",
location: "",
commissioned: undefined,
equipmentTypeId: "",
code,
name,
location,
commissioned,
equipmentTypeId,
};
let equipmentId = await EquipmentCommandHandler.create(createEquipment);
@ -91,13 +96,19 @@ export async function createEquipment(req: Request, res: Response): Promise<any>
*/
export async function updateEquipmentById(req: Request, res: Response): Promise<any> {
const equipmentId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const decommissioned = req.body.decommissioned || null;
let updateEquipment: UpdateEquipmentCommand = {
id: equipmentId,
name: "",
location: "",
commissioned: undefined,
name,
code,
location,
commissioned,
decommissioned,
};
await EquipmentCommandHandler.update(updateEquipment);

View file

@ -19,12 +19,11 @@ export async function getAllEquipmentTypes(req: Request, res: Response): Promise
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 [equipmentTypes, total] = await EquipmentTypeService.getAll({ offset, count, search, noLimit, ids });
let [equipmentTypes, total] = await EquipmentTypeService.getAll({ offset, count, search, noLimit });
res.json({
equipmentTypes: equipmentTypes,
equipmentTypes: EquipmentTypeFactory.mapToBase(equipmentTypes),
total: total,
offset: offset,
count: count,
@ -51,11 +50,12 @@ export async function getEquipmentTypeById(req: Request, res: Response): Promise
* @returns {Promise<*>}
*/
export async function createEquipmentType(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const type = req.body.type;
const description = req.body.description;
let createEquipmentType: CreateEquipmentTypeCommand = {
type: "",
description: "",
type,
description,
};
let equipmentTypeId = await EquipmentTypeCommandHandler.create(createEquipmentType);
@ -70,12 +70,13 @@ export async function createEquipmentType(req: Request, res: Response): Promise<
*/
export async function updateEquipmentTypeById(req: Request, res: Response): Promise<any> {
const equipmentTypeId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const type = req.body.type;
const description = req.body.description;
let updateEquipmentType: UpdateEquipmentTypeCommand = {
id: equipmentTypeId,
type: "",
description: "",
type,
description,
};
await EquipmentTypeCommandHandler.update(updateEquipmentType);

View file

@ -7,6 +7,7 @@ import {
UpdateInspectionCommand,
} from "../../../command/unit/inspection/inspectionCommand";
import InspectionCommandHandler from "../../../command/unit/inspection/inspectionCommandHandler";
import BadRequestException from "../../../exceptions/badRequestException";
/**
* @description get all inspections for related id
@ -25,7 +26,7 @@ export async function getAllInspectionsForRelated(req: Request, res: Response):
let [inspections, total] = await InspectionService.getAllForRelated(where, { offset, count, noLimit });
res.json({
inspections: inspections,
inspections: InspectionFactory.mapToBase(inspections),
total: total,
offset: offset,
count: count,
@ -52,13 +53,21 @@ export async function getInspectionById(req: Request, res: Response): Promise<an
* @returns {Promise<*>}
*/
export async function createInspection(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
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;
if (assigned != "equipment" && assigned != "vehicle")
throw new BadRequestException("set assigned to equipment or vehicle");
let createInspection: CreateInspectionCommand = {
context: "",
inspectionPlanId: "",
relatedId: "",
assigned: "equipment",
context,
nextInspection,
inspectionPlanId,
relatedId,
assigned,
};
let inspectionId = await InspectionCommandHandler.create(createInspection);
@ -73,11 +82,13 @@ export async function createInspection(req: Request, res: Response): Promise<any
*/
export async function updateInspectionById(req: Request, res: Response): Promise<any> {
const inspectionId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const context = req.body.context;
const nextInspection = req.body.nextInspection || null;
let updateInspection: UpdateInspectionCommand = {
id: inspectionId,
context: "",
context,
nextInspection,
};
await InspectionCommandHandler.update(updateInspection);

View file

@ -7,6 +7,37 @@ import {
UpdateInspectionPlanCommand,
} from "../../../command/unit/inspection/inspectionPlanCommand";
import InspectionPlanCommandHandler from "../../../command/unit/inspection/inspectionPlanCommandHandler";
import BadRequestException from "../../../exceptions/badRequestException";
import TypeTester from "../../../helpers/typeTester";
/**
* @description get all inspectionPlans
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getAllInspectionPlans(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 search = (req.query.search as string) ?? "";
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
let [inspectionPlans, total] = await InspectionPlanService.getAll({
offset,
count,
search,
noLimit,
ids,
});
res.json({
inspectionPlans: InspectionPlanFactory.mapToBase(inspectionPlans),
total: total,
offset: offset,
count: count,
});
}
/**
* @description get all inspectionPlans
@ -23,7 +54,7 @@ export async function getAllInspectionPlansForRelated(req: Request, res: Respons
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 where = relation === "equipment" ? { equipmentTypeId: relationId } : { vehicleTypeId: relationId };
let [inspectionPlans, total] = await InspectionPlanService.getAllForRelated(where, {
offset,
count,
@ -60,14 +91,24 @@ export async function getInspectionPlanById(req: Request, res: Response): Promis
* @returns {Promise<*>}
*/
export async function createInspectionPlan(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const title = req.body.title;
const inspectionInterval = req.body.inspectionInterval;
const remindTime = req.body.remindTime;
const relatedId = req.body.relatedId;
const assigned = req.body.assigned;
TypeTester.testPlanTimeDefinition(inspectionInterval, "inspectionInterval", true);
TypeTester.testPlanTimeDefinition(remindTime, "remindTime", true);
if (assigned != "equipment" && assigned != "vehicle")
throw new BadRequestException("set assigned to equipment or vehicle");
let createInspectionPlan: CreateInspectionPlanCommand = {
title: "",
inspectionInterval: "1-m",
remindTime: "1-m",
relatedId: "",
assigned: "equipment",
title,
inspectionInterval,
remindTime,
relatedId,
assigned,
};
let inspectionPlanId = await InspectionPlanCommandHandler.create(createInspectionPlan);
@ -82,12 +123,18 @@ export async function createInspectionPlan(req: Request, res: Response): Promise
*/
export async function updateInspectionPlanById(req: Request, res: Response): Promise<any> {
const inspectionPlanId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const title = req.body.title;
const inspectionInterval = req.body.inspectionInterval;
const remindTime = req.body.remindTime;
TypeTester.testPlanTimeDefinition(inspectionInterval, "inspectionInterval", true);
TypeTester.testPlanTimeDefinition(remindTime, "remindTime", true);
let updateInspectionPlan: UpdateInspectionPlanCommand = {
id: inspectionPlanId,
title: "",
inspectionInterval: "1-m",
title,
inspectionInterval,
remindTime,
};
await InspectionPlanCommandHandler.update(updateInspectionPlan);

View file

@ -24,7 +24,7 @@ export async function getAllVehicles(req: Request, res: Response): Promise<any>
let [vehicles, total] = await VehicleService.getAll({ offset, count, search, noLimit, ids });
res.json({
vehicles: vehicles,
vehicles: VehicleFactory.mapToBase(vehicles),
total: total,
offset: offset,
count: count,
@ -44,6 +44,25 @@ export async function getVehicleById(req: Request, res: Response): Promise<any>
res.json(VehicleFactory.mapToSingle(vehicle));
}
/**
* @description get vehicle by Ids
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getVehiclesByIds(req: Request, res: Response): Promise<any> {
let ids = req.body.ids as Array<string>;
let [vehicles, total] = await VehicleService.getAll({ noLimit: true, ids });
res.json({
vehicles: VehicleFactory.mapToBase(vehicles),
total: total,
offset: 0,
count: total,
});
}
/**
* @description create vehicle
* @param req {Request} Express req object
@ -51,13 +70,18 @@ export async function getVehicleById(req: Request, res: Response): Promise<any>
* @returns {Promise<*>}
*/
export async function createVehicle(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const vehicleTypeId = req.body.vehicleTypeId;
let createVehicle: CreateVehicleCommand = {
name: "",
location: "",
commissioned: undefined,
vehicleTypeId: "",
code,
name,
location,
commissioned,
vehicleTypeId,
};
let vehicleId = await VehicleCommandHandler.create(createVehicle);
@ -72,13 +96,19 @@ export async function createVehicle(req: Request, res: Response): Promise<any> {
*/
export async function updateVehicleById(req: Request, res: Response): Promise<any> {
const vehicleId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const decommissioned = req.body.decommissioned || null;
let updateVehicle: UpdateVehicleCommand = {
id: vehicleId,
name: "",
location: "",
commissioned: undefined,
code,
name,
location,
commissioned,
decommissioned,
};
await VehicleCommandHandler.update(updateVehicle);

View file

@ -19,12 +19,11 @@ export async function getAllVehicleTypes(req: Request, res: Response): Promise<a
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 [vehicleTypes, total] = await VehicleTypeService.getAll({ offset, count, search, noLimit, ids });
let [vehicleTypes, total] = await VehicleTypeService.getAll({ offset, count, search, noLimit });
res.json({
vehicleTypes: vehicleTypes,
vehicleTypes: VehicleTypeFactory.mapToBase(vehicleTypes),
total: total,
offset: offset,
count: count,
@ -51,11 +50,12 @@ export async function getVehicleTypeById(req: Request, res: Response): Promise<a
* @returns {Promise<*>}
*/
export async function createVehicleType(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const type = req.body.type;
const description = req.body.description;
let createVehicleType: CreateVehicleTypeCommand = {
type: "",
description: "",
type,
description,
};
let vehicleTypeId = await VehicleTypeCommandHandler.create(createVehicleType);
@ -70,12 +70,13 @@ export async function createVehicleType(req: Request, res: Response): Promise<an
*/
export async function updateVehicleTypeById(req: Request, res: Response): Promise<any> {
const vehicleTypeId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const type = req.body.type;
const description = req.body.description;
let updateVehicleType: UpdateVehicleTypeCommand = {
id: vehicleTypeId,
type: "",
description: "",
type,
description,
};
await VehicleTypeCommandHandler.update(updateVehicleType);

View file

@ -24,7 +24,7 @@ export async function getAllWearables(req: Request, res: Response): Promise<any>
let [wearables, total] = await WearableService.getAll({ offset, count, search, noLimit, ids });
res.json({
wearables: wearables,
wearables: WearableFactory.mapToBase(wearables),
total: total,
offset: offset,
count: count,
@ -44,6 +44,25 @@ export async function getWearableById(req: Request, res: Response): Promise<any>
res.json(WearableFactory.mapToSingle(wearable));
}
/**
* @description get wearable by Ids
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getWearablesByIds(req: Request, res: Response): Promise<any> {
let ids = req.body.ids as Array<string>;
let [wearables, total] = await WearableService.getAll({ noLimit: true, ids });
res.json({
wearables: WearableFactory.mapToBase(wearables),
total: total,
offset: 0,
count: total,
});
}
/**
* @description create wearable
* @param req {Request} Express req object
@ -51,13 +70,20 @@ export async function getWearableById(req: Request, res: Response): Promise<any>
* @returns {Promise<*>}
*/
export async function createWearable(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const wearableTypeId = req.body.wearableTypeId;
const wearerId = req.body.wearerId || null;
let createWearable: CreateWearableCommand = {
name: "",
location: "",
commissioned: undefined,
wearableTypeId: "",
code,
name,
location,
commissioned,
wearableTypeId,
wearerId,
};
let wearableId = await WearableCommandHandler.create(createWearable);
@ -72,13 +98,21 @@ export async function createWearable(req: Request, res: Response): Promise<any>
*/
export async function updateWearableById(req: Request, res: Response): Promise<any> {
const wearableId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const decommissioned = req.body.decommissioned || null;
const wearerId = req.body.wearerId || null;
let updateWearable: UpdateWearableCommand = {
id: wearableId,
name: "",
location: "",
commissioned: undefined,
code,
name,
location,
commissioned,
decommissioned,
wearerId,
};
await WearableCommandHandler.update(updateWearable);

View file

@ -19,12 +19,11 @@ export async function getAllWearableTypes(req: Request, res: Response): Promise<
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 [wearableTypes, total] = await WearableTypeService.getAll({ offset, count, search, noLimit, ids });
let [wearableTypes, total] = await WearableTypeService.getAll({ offset, count, search, noLimit });
res.json({
wearableTypes: wearableTypes,
wearableTypes: WearableTypeFactory.mapToBase(wearableTypes),
total: total,
offset: offset,
count: count,
@ -51,11 +50,12 @@ export async function getWearableTypeById(req: Request, res: Response): Promise<
* @returns {Promise<*>}
*/
export async function createWearableType(req: Request, res: Response): Promise<any> {
const salutationId = parseInt(req.body.salutationId);
const type = req.body.type;
const description = req.body.description;
let createWearableType: CreateWearableTypeCommand = {
type: "",
description: "",
type,
description,
};
let wearableTypeId = await WearableTypeCommandHandler.create(createWearableType);
@ -70,12 +70,13 @@ export async function createWearableType(req: Request, res: Response): Promise<a
*/
export async function updateWearableTypeById(req: Request, res: Response): Promise<any> {
const wearableTypeId = req.params.id;
const salutationId = parseInt(req.body.salutationId);
const type = req.body.type;
const description = req.body.description;
let updateWearableType: UpdateWearableTypeCommand = {
id: wearableTypeId,
type: "",
description: "",
type,
description,
};
await WearableTypeCommandHandler.update(updateWearableType);

View file

@ -18,10 +18,10 @@ export class equipment {
@Column({ type: "varchar", length: 255 })
location: string;
@Column({ type: getTypeByORM("datetime").type as ColumnType })
@Column({ type: getTypeByORM("date").type as ColumnType })
commissioned: Date;
@Column({ type: getTypeByORM("datetime").type as ColumnType, nullable: true, default: null })
@Column({ type: getTypeByORM("date").type as ColumnType, nullable: true, default: null })
decommissioned?: Date;
@Column()

View file

@ -16,6 +16,6 @@ export class equipmentType {
@OneToMany(() => equipment, (e) => e.equipmentType, { cascade: ["insert"] })
equipment: equipment[];
@OneToMany(() => inspectionPlan, (ip) => ip.equipment)
@OneToMany(() => inspectionPlan, (ip) => ip.equipmentType)
inspectionPlans: inspectionPlan[];
}

View file

@ -1,8 +1,8 @@
import { Column, CreateDateColumn, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { equipment } from "../equipment/equipment";
import { vehicle } from "../vehicle/vehicle";
import { PlanTimeDefinition } from "../../../viewmodel/admin/unit/inspection/inspectionPlan.models";
import { inspectionVersionedPlan } from "./inspectionVersionedPlan";
import { equipmentType } from "../equipment/equipmentType";
import { vehicleType } from "../vehicle/vehicleType";
@Entity()
export class inspectionPlan {
@ -22,24 +22,24 @@ export class inspectionPlan {
createdAt: Date;
@Column()
equipmentId?: string;
equipmentTypeId?: string;
@Column()
vehicleId?: string;
vehicleTypeId?: string;
@ManyToOne(() => equipment, {
@ManyToOne(() => equipmentType, {
nullable: true,
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
equipment?: equipment;
equipmentType?: equipmentType;
@ManyToOne(() => vehicle, {
@ManyToOne(() => vehicleType, {
nullable: true,
onDelete: "CASCADE",
onUpdate: "RESTRICT",
})
vehicle?: vehicle;
vehicleType?: vehicleType;
@OneToMany(() => inspectionVersionedPlan, (ivp) => ivp.inspectionPlan, {
cascade: ["insert"],

View file

@ -18,10 +18,10 @@ export class vehicle {
@Column({ type: "varchar", length: 255 })
location: string;
@Column({ type: getTypeByORM("datetime").type as ColumnType })
@Column({ type: getTypeByORM("date").type as ColumnType })
commissioned: Date;
@Column({ type: getTypeByORM("datetime").type as ColumnType, nullable: true, default: null })
@Column({ type: getTypeByORM("date").type as ColumnType, nullable: true, default: null })
decommissioned?: Date;
@Column()

View file

@ -16,6 +16,6 @@ export class vehicleType {
@OneToMany(() => vehicle, (e) => e.vehicleType, { cascade: ["insert"] })
vehicle: vehicle[];
@OneToMany(() => inspectionPlan, (ip) => ip.vehicle)
@OneToMany(() => inspectionPlan, (ip) => ip.vehicleType)
inspectionPlans: inspectionPlan[];
}

View file

@ -19,10 +19,10 @@ export class wearable {
@Column({ type: "varchar", length: 255 })
location: string;
@Column({ type: getTypeByORM("datetime").type as ColumnType })
@Column({ type: getTypeByORM("date").type as ColumnType })
commissioned: Date;
@Column({ type: getTypeByORM("datetime").type as ColumnType, nullable: true, default: null })
@Column({ type: getTypeByORM("date").type as ColumnType, nullable: true, default: null })
decommissioned?: Date;
@Column()

View file

@ -15,7 +15,7 @@ export default abstract class MemberFactory {
public static mapToSingle(record: member): MemberViewModel {
return {
id: record?.id,
salutation: SalutationFactory.mapToSingle(record?.salutation),
salutation: record?.salutation ? SalutationFactory.mapToSingle(record?.salutation) : null,
firstname: record?.firstname,
lastname: record?.lastname,
nameaffix: record?.nameaffix,

View file

@ -1,7 +1,9 @@
import { inspectionPlan } from "../../../../entity/unit/inspection/inspectionPlan";
import { InspectionPlanViewModel } from "../../../../viewmodel/admin/unit/inspection/inspectionPlan.models";
import EquipmentFactory from "../equipment/equipment";
import EquipmentTypeFactory from "../equipment/equipmentType";
import VehicleFactory from "../vehicle/vehicle";
import VehicleTypeFactory from "../vehicle/vehicleType";
import InspectionPointFactory from "./inspectionPoint";
export default abstract class InspectionPlanFactory {
@ -16,19 +18,21 @@ export default abstract class InspectionPlanFactory {
title: record.title,
inspectionInterval: record.inspectionInterval,
remindTime: record.remindTime,
version: record.latestVersionedPlan.version,
version: record?.latestVersionedPlan?.version ?? 0,
created: record.createdAt,
inspectionPoints: InspectionPointFactory.mapToBase(record.latestVersionedPlan.inspectionPoints),
...(record.equipmentId
inspectionPoints: record.latestVersionedPlan
? InspectionPointFactory.mapToBase(record.latestVersionedPlan.inspectionPoints)
: [],
...(record.equipmentTypeId
? {
relatedId: record.equipmentId,
relatedId: record.equipmentTypeId,
assigned: "equipment",
related: EquipmentFactory.mapToSingle(record.equipment),
related: EquipmentTypeFactory.mapToSingle(record.equipmentType),
}
: {
relatedId: record.vehicleId,
relatedId: record.vehicleTypeId,
assigned: "vehicle",
related: VehicleFactory.mapToSingle(record.vehicle),
related: VehicleTypeFactory.mapToSingle(record.vehicleType),
}),
};
}

18
src/helpers/typeTester.ts Normal file
View file

@ -0,0 +1,18 @@
import { PlanTimeDefinition } from "../viewmodel/admin/unit/inspection/inspectionPlan.models";
export default abstract class TypeTester {
static testPlanTimeDefinition(val: string, key: string = "", throwErr: boolean = false): PlanTimeDefinition | null {
if (/^(\d+-(d|m|y)|\d+\/(\d+|\*))$/.test(val)) {
return val as PlanTimeDefinition;
} else if (throwErr) {
throw Error(
[
key,
"provided String does not match PlanTimeDefinition Format: ${number}-${'d'|'m'|'y'} or ${number}/${number|'*'}",
].join(": ")
);
} else {
return null;
}
}
}

View file

@ -9,21 +9,21 @@ export const inspection_plan_table = new Table({
{ name: "inspectionInterval", ...getTypeByORM("varchar") },
{ name: "remindTime", ...getTypeByORM("varchar") },
{ name: "createdAt", ...getTypeByORM("date"), default: getDefaultByORM("currentTimestamp") },
{ name: "equipmentId", ...getTypeByORM("uuid", true), default: getDefaultByORM("null") },
{ name: "vehicleId", ...getTypeByORM("uuid", true), default: getDefaultByORM("null") },
{ name: "equipmentTypeId", ...getTypeByORM("uuid", true), default: getDefaultByORM("null") },
{ name: "vehicleTypeId", ...getTypeByORM("uuid", true), default: getDefaultByORM("null") },
],
foreignKeys: [
new TableForeignKey({
columnNames: ["equipmentId"],
columnNames: ["equipmentTypeId"],
referencedColumnNames: ["id"],
referencedTableName: "equipment",
referencedTableName: "equipment_type",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
}),
new TableForeignKey({
columnNames: ["vehicleId"],
columnNames: ["vehicleTypeId"],
referencedColumnNames: ["id"],
referencedTableName: "vehicle",
referencedTableName: "vehicle_type",
onDelete: "CASCADE",
onUpdate: "RESTRICT",
}),

View file

@ -40,6 +40,7 @@ import wearable from "./unit/wearable";
import wearableType from "./unit/wearableType";
import inspection from "./unit/inspection";
import inspectionPlan from "./unit/inspectionPlan";
import damageReport from "./unit/damageReport";
var router = express.Router({ mergeParams: true });
@ -215,5 +216,15 @@ router.use(
]),
inspectionPlan
);
router.use(
"/damagereport",
PermissionHelper.passCheckSomeMiddleware([
{ requiredPermission: "read", section: "unit", module: "damage_report" },
{ requiredPermission: "read", section: "unit", module: "equipment" },
{ requiredPermission: "read", section: "unit", module: "vehicle" },
{ requiredPermission: "read", section: "unit", module: "wearable" },
]),
damageReport
);
export default router;

View file

@ -0,0 +1,50 @@
import express, { Request, Response } from "express";
import PermissionHelper from "../../../helpers/permissionHelper";
import {
createInspection,
deleteInspectionById,
getAllInspectionsForRelated,
getInspectionById,
updateInspectionById,
} from "../../../controller/admin/unit/inspectionController";
import {
getAllDamageReportsByStatus,
getAllDamageReportsForRelated,
getDamageReportById,
updateDamageReportById,
} from "../../../controller/admin/unit/damageReportController";
var router = express.Router({ mergeParams: true });
router.get("/", async (req: Request, res: Response) => {
await getAllDamageReportsByStatus(req, res);
});
router.get(
["/vehicle/:relatedId", "/equipment/:relatedId", "/wearable/:relatedId"],
async (req: Request, res: Response) => {
if (req.path.startsWith("/vehicle")) {
req.params.related = "vehicle";
} else if (req.path.startsWith("/equipment")) {
req.params.related = "equipment";
} else {
req.params.related = "wearable";
}
await getAllDamageReportsForRelated(req, res);
}
);
router.get("/:id", async (req: Request, res: Response) => {
await getDamageReportById(req, res);
});
router.patch(
"/:id",
PermissionHelper.passCheckMiddleware("update", "unit", "inspection"),
async (req: Request, res: Response) => {
await updateDamageReportById(req, res);
}
);
export default router;

View file

@ -3,6 +3,7 @@ import PermissionHelper from "../../../helpers/permissionHelper";
import {
createInspectionPlan,
deleteInspectionPlanById,
getAllInspectionPlans,
getAllInspectionPlansForRelated,
getInspectionPlanById,
updateInspectionPlanById,
@ -10,6 +11,10 @@ import {
var router = express.Router({ mergeParams: true });
router.get("/", async (req: Request, res: Response) => {
await getAllInspectionPlans(req, res);
});
router.get(["/vehicle/:relatedId", "/equipment/:relatedId"], async (req: Request, res: Response) => {
if (req.path.startsWith("/vehicle")) {
req.params.related = "vehicle";

View file

@ -5,6 +5,7 @@ import {
deleteVehicleById,
getAllVehicles,
getVehicleById,
getVehiclesByIds,
updateVehicleById,
} from "../../../controller/admin/unit/vehicleController";
@ -18,6 +19,10 @@ router.get("/:id", async (req: Request, res: Response) => {
await getVehicleById(req, res);
});
router.post("/ids", async (req: Request, res: Response) => {
await getVehiclesByIds(req, res);
});
router.post(
"/",
PermissionHelper.passCheckMiddleware("create", "unit", "vehicle"),

View file

@ -5,6 +5,7 @@ import {
deleteWearableById,
getAllWearables,
getWearableById,
getWearablesByIds,
updateWearableById,
} from "../../../controller/admin/unit/wearableController";
@ -18,6 +19,10 @@ router.get("/:id", async (req: Request, res: Response) => {
await getWearableById(req, res);
});
router.post("/ids", async (req: Request, res: Response) => {
await getWearablesByIds(req, res);
});
router.post(
"/",
PermissionHelper.passCheckMiddleware("create", "unit", "wearable"),

View file

@ -2,11 +2,11 @@ import express, { Request, Response } from "express";
import PermissionHelper from "../../../helpers/permissionHelper";
import {
createWearableType,
deleteWearableTypeById,
getAllWearableTypes,
getWearableTypeById,
updateWearableTypeById,
} from "../../../controller/admin/unit/wearableTypeController";
import { deleteWearableById } from "../../../controller/admin/unit/wearableController";
var router = express.Router({ mergeParams: true });
@ -38,7 +38,7 @@ router.delete(
"/:id",
PermissionHelper.passCheckMiddleware("delete", "unit", "wearable_type"),
async (req: Request, res: Response) => {
await deleteWearableById(req, res);
await deleteWearableTypeById(req, res);
}
);

View file

@ -3,20 +3,73 @@ import { damageReport } from "../../entity/unit/damageReport";
import DatabaseActionException from "../../exceptions/databaseActionException";
export default abstract class DamageReportService {
/**
* @description get all damageReports
* @returns {Promise<Array<damageReport>>}
*/
static async getAll(): Promise<Array<damageReport>> {
return await dataSource
private static query = () =>
dataSource
.getRepository(damageReport)
.createQueryBuilder("damageReport")
.leftJoinAndSelect("damageReport.equipment", "equipment")
.leftJoinAndSelect("damageReport.vehicle", "vehicle")
.leftJoinAndSelect("damageReport.wearable", "wearable")
.leftJoinAndSelect("damageReport.maintenance", "maintenance")
.orderBy("type", "ASC")
.getMany()
.leftJoinAndSelect("damageReport.maintenance", "maintenance");
/**
* @description get all damageReports By done
* @returns {Promise<[Array<damageReport>, number]>}
*/
static async getAll(
done = false,
{
offset = 0,
count = 25,
noLimit = false,
}: {
offset?: number;
count?: number;
noLimit?: boolean;
}
): Promise<[Array<damageReport>, number]> {
let query = this.query().where({ done });
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("damageReport.reportedAt", "ASC")
.getManyAndCount()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "damageReport", err);
});
}
/**
* @description get all damageReports By related
* @returns {Promise<[Array<damageReport>, number]>}
*/
static async getAllForRelated(
where: { equipmentId: string } | { vehicleId: string } | { wearableId: string },
{
offset = 0,
count = 25,
noLimit = false,
}: {
offset?: number;
count?: number;
noLimit?: boolean;
}
): Promise<[Array<damageReport>, number]> {
let query = this.query().where(where);
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("reportedAt", "ASC")
.getManyAndCount()
.then((res) => {
return res;
})
@ -30,13 +83,7 @@ export default abstract class DamageReportService {
* @returns {Promise<damageReport>}
*/
static async getById(id: string): Promise<damageReport> {
return await dataSource
.getRepository(damageReport)
.createQueryBuilder("damageReport")
.leftJoinAndSelect("damageReport.equipment", "equipment")
.leftJoinAndSelect("damageReport.vehicle", "vehicle")
.leftJoinAndSelect("damageReport.wearable", "wearable")
.leftJoinAndSelect("damageReport.maintenance", "maintenance")
return await this.query()
.where({ id })
.getOneOrFail()
.then((res) => {

View file

@ -27,11 +27,16 @@ export default abstract class EquipmentService {
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype");
if (search != "") {
query = query.where({
code: Like(search),
name: Like(search),
location: Like(search),
});
query = query
.where({
code: Like(`%${search}%`),
})
.orWhere({
name: Like(`%${search}%`),
})
.orWhere({
location: Like(`%${search}%`),
});
}
if (ids.length != 0) {

View file

@ -13,26 +13,20 @@ export default abstract class EquipmentTypeService {
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<equipmentType>, number]> {
let query = dataSource.getRepository(equipmentType).createQueryBuilder("equipmentType");
if (search != "") {
query = query.where({
type: Like(search),
type: Like(`%${search}%`),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}

View file

@ -14,19 +14,63 @@ export default abstract class InspectionPlanService {
"inspectionPlan.versionedPlans",
"latestVersionedPlan",
DB_TYPE == "postgres"
? 'latestVersionedPlan.inspectionPlanId = inspectionPlan.id AND latestVersionedPlan.version = (SELECT MAX("ivp"."start") FROM "inspection_versioned_plan" "ivp" WHERE "ivp"."inspectionPlanId" = "inspectionPlan"."id")'
: "latestVersionedPlan.inspectionPlanId = inspectionPlan.id AND latestVersionedPlan.version = (SELECT MAX(ivp.start) FROM inspection_versioned_plan ivp WHERE ivp.inspectionPlanId = inspectionPlan.id)"
? 'latestVersionedPlan.inspectionPlanId = inspectionPlan.id AND latestVersionedPlan.version = (SELECT MAX("ivp"."createdAt") FROM "inspection_versioned_plan" "ivp" WHERE "ivp"."inspectionPlanId" = "inspectionPlan"."id")'
: "latestVersionedPlan.inspectionPlanId = inspectionPlan.id AND latestVersionedPlan.version = (SELECT MAX(ivp.createdAt) FROM inspection_versioned_plan ivp WHERE ivp.inspectionPlanId = inspectionPlan.id)"
)
.leftJoinAndSelect("latestVersionedPlan.inspectionPoints", "inspectionPoints")
.leftJoinAndSelect("inspectionPlan.equipment", "equipment")
.leftJoinAndSelect("inspectionPlan.vehicle", "vehicle");
.leftJoinAndSelect("inspectionPlan.equipmentType", "equipmentType")
.leftJoinAndSelect("inspectionPlan.vehicleType", "vehicleType");
/**
* @description get all inspectionPlans for related
* @returns {Promise<[Array<inspectionPlan>, number]>}
*/
static async getAll({
offset = 0,
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<inspectionPlan>, number]> {
let query = this.query();
if (search != "") {
query = query.where({
title: Like(`%${search}%`),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("inspectionPlan.title", "ASC")
.getManyAndCount()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "inspectionPlan", err);
});
}
/**
* @description get all inspectionPlans for related
* @returns {Promise<[Array<inspectionPlan>, number]>}
*/
static async getAllForRelated(
where: { equipmentId: string } | { vehicleId: string },
where: { equipmentTypeId: string } | { vehicleTypeId: string },
{
offset = 0,
count = 25,
@ -44,13 +88,13 @@ export default abstract class InspectionPlanService {
let query = this.query().where(where);
if (search != "") {
query = query.where({
title: Like(search),
query = query.andWhere({
title: Like(`%${search}%`),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
query = query.andWhere({ id: In(ids) });
}
if (!noLimit) {
@ -58,7 +102,7 @@ export default abstract class InspectionPlanService {
}
return await query
.orderBy("title", "ASC")
.orderBy("inspectionPlan.title", "ASC")
.getManyAndCount()
.then((res) => {
return res;

View file

@ -38,7 +38,7 @@ export default abstract class InspectionService {
}
return await query
.orderBy("createdAt", "DESC")
.orderBy("inspection.createdAt", "DESC")
.getManyAndCount()
.then((res) => {
return res;

View file

@ -27,11 +27,16 @@ export default abstract class VehicleService {
.leftJoinAndSelect("vehicle.vehicleType", "vehicletype");
if (search != "") {
query = query.where({
code: Like(search),
name: Like(search),
location: Like(search),
});
query = query
.where({
code: Like(`%${search}%`),
})
.orWhere({
name: Like(`%${search}%`),
})
.orWhere({
location: Like(`%${search}%`),
});
}
if (ids.length != 0) {

View file

@ -13,26 +13,20 @@ export default abstract class VehicleTypeService {
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<vehicleType>, number]> {
let query = dataSource.getRepository(vehicleType).createQueryBuilder("vehicleType");
if (search != "") {
query = query.where({
type: Like(search),
type: Like(`%${search}%`),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}

View file

@ -28,11 +28,16 @@ export default abstract class WearableService {
.leftJoinAndSelect("wearable.wearer", "wearer");
if (search != "") {
query = query.where({
code: Like(search),
name: Like(search),
location: Like(search),
});
query = query
.where({
code: Like(`%${search}%`),
})
.orWhere({
name: Like(`%${search}%`),
})
.orWhere({
location: Like(`%${search}%`),
});
}
if (ids.length != 0) {
@ -63,6 +68,7 @@ export default abstract class WearableService {
.getRepository(wearable)
.createQueryBuilder("wearable")
.leftJoinAndSelect("wearable.wearableType", "wearabletype")
.leftJoinAndSelect("wearable.wearer", "wearer")
.where({ id })
.getOneOrFail()
.then((res) => {

View file

@ -13,26 +13,20 @@ export default abstract class WearableTypeService {
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<wearableType>, number]> {
let query = dataSource.getRepository(wearableType).createQueryBuilder("wearableType");
if (search != "") {
query = query.where({
type: Like(search),
type: Like(`%${search}%`),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}

View file

@ -1,6 +1,8 @@
import { InspectionPointEnum } from "../../../../enums/inspectionEnum";
import type { EquipmentViewModel } from "../equipment/equipment.models";
import { EquipmentTypeViewModel } from "../equipment/equipmentType.models";
import type { VehicleViewModel } from "../vehicle/vehicle.models";
import { VehicleTypeViewModel } from "../vehicle/vehicleType.models";
export type PlanTimeDefinition = `${number}-${"d" | "m" | "y"}` | `${number}/${number | "*"}`;
@ -16,11 +18,11 @@ export type InspectionPlanViewModel = {
} & (
| {
assigned: "equipment";
related: EquipmentViewModel;
related: EquipmentTypeViewModel;
}
| {
assigned: "vehicle";
related: VehicleViewModel;
related: VehicleTypeViewModel;
}
);