controller

This commit is contained in:
Julian Krauser 2025-06-02 13:14:09 +02:00
parent 9f2a08ccc9
commit 2609ecc1bf
17 changed files with 320 additions and 95 deletions

View file

@ -21,8 +21,7 @@ export async function getAllEquipments(req: Request, res: Response): Promise<any
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [equipments, total] = await EquipmentService.getAll();
let [equipments, total] = await EquipmentService.getAll({ offset, count, search, noLimit, ids });
res.json({
equipments: equipments,
@ -45,6 +44,25 @@ export async function getEquipmentById(req: Request, res: Response): Promise<any
res.json(EquipmentFactory.mapToSingle(equipment));
}
/**
* @description get equipment by Ids
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getEquipmentsByIds(req: Request, res: Response): Promise<any> {
let ids = req.body.ids as Array<string>;
let [equipments, total] = await EquipmentService.getAll({ noLimit: true, ids });
res.json({
equipments: EquipmentFactory.mapToBase(equipments),
total: total,
offset: 0,
count: total,
});
}
/**
* @description create equipment
* @param req {Request} Express req object

View file

@ -21,8 +21,7 @@ export async function getAllEquipmentTypes(req: Request, res: Response): Promise
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [equipmentTypes, total] = await EquipmentTypeService.getAll();
let [equipmentTypes, total] = await EquipmentTypeService.getAll({ offset, count, search, noLimit, ids });
res.json({
equipmentTypes: equipmentTypes,

View file

@ -19,12 +19,10 @@ export async function getAllInspectionsForRelated(req: Request, res: Response):
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 search = (req.query.search as string) ?? "";
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [inspections, total] = await InspectionService.getAllForRelated({ equipmentId: relationId });
let where = relation === "equipment" ? { equipmentId: relationId } : { vehicleId: relationId };
let [inspections, total] = await InspectionService.getAllForRelated(where, { offset, count, noLimit });
res.json({
inspections: inspections,

View file

@ -23,8 +23,14 @@ 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);
//{ offset, count, search, noLimit, ids }
let [inspectionPlans, total] = await InspectionPlanService.getAllForRelated({ equipmentId: relationId });
let where = relation === "equipment" ? { equipmentId: relationId } : { vehicleId: relationId };
let [inspectionPlans, total] = await InspectionPlanService.getAllForRelated(where, {
offset,
count,
search,
noLimit,
ids,
});
res.json({
inspectionPlans: inspectionPlans,

View file

@ -21,8 +21,7 @@ export async function getAllVehicles(req: Request, res: Response): Promise<any>
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [vehicles, total] = await VehicleService.getAll();
let [vehicles, total] = await VehicleService.getAll({ offset, count, search, noLimit, ids });
res.json({
vehicles: vehicles,

View file

@ -21,8 +21,7 @@ export async function getAllVehicleTypes(req: Request, res: Response): Promise<a
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [vehicleTypes, total] = await VehicleTypeService.getAll();
let [vehicleTypes, total] = await VehicleTypeService.getAll({ offset, count, search, noLimit, ids });
res.json({
vehicleTypes: vehicleTypes,

View file

@ -21,8 +21,7 @@ export async function getAllWearables(req: Request, res: Response): Promise<any>
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [wearables, total] = await WearableService.getAll();
let [wearables, total] = await WearableService.getAll({ offset, count, search, noLimit, ids });
res.json({
wearables: wearables,

View file

@ -21,8 +21,7 @@ export async function getAllWearableTypes(req: Request, res: Response): Promise<
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
//{ offset, count, search, noLimit, ids }
let [wearableTypes, total] = await WearableTypeService.getAll();
let [wearableTypes, total] = await WearableTypeService.getAll({ offset, count, search, noLimit, ids });
res.json({
wearableTypes: wearableTypes,

View file

@ -5,6 +5,7 @@ import {
deleteEquipmentById,
getAllEquipments,
getEquipmentById,
getEquipmentsByIds,
updateEquipmentById,
} from "../../../controller/admin/unit/equipmentController";
@ -18,6 +19,10 @@ router.get("/:id", async (req: Request, res: Response) => {
await getEquipmentById(req, res);
});
router.post("/ids", async (req: Request, res: Response) => {
await getEquipmentsByIds(req, res);
});
router.post(
"/",
PermissionHelper.passCheckMiddleware("create", "unit", "equipment"),

View file

@ -1,3 +1,4 @@
import { In, Like } from "typeorm";
import { dataSource } from "../../../data-source";
import { equipment } from "../../../entity/unit/equipment/equipment";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -5,15 +6,45 @@ import DatabaseActionException from "../../../exceptions/databaseActionException
export default abstract class EquipmentService {
/**
* @description get all equipment
* @returns {Promise<Array<equipment>>}
* @returns {Promise<[Array<equipment>, number]>}
*/
static async getAll(): Promise<Array<equipment>> {
return await dataSource
static async getAll({
offset = 0,
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<equipment>, number]> {
let query = dataSource
.getRepository(equipment)
.createQueryBuilder("equipment")
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype")
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype");
if (search != "") {
query = query.where({
code: Like(search),
name: Like(search),
location: Like(search),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("name", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})

View file

@ -1,3 +1,4 @@
import { Like, In } from "typeorm";
import { dataSource } from "../../../data-source";
import { equipmentType } from "../../../entity/unit/equipment/equipmentType";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -5,14 +6,40 @@ import DatabaseActionException from "../../../exceptions/databaseActionException
export default abstract class EquipmentTypeService {
/**
* @description get all equipmentTypes
* @returns {Promise<Array<equipmentType>>}
* @returns {Promise<[Array<equipmentType>, number]>}
*/
static async getAll(): Promise<Array<equipmentType>> {
return await dataSource
.getRepository(equipmentType)
.createQueryBuilder("equipmentType")
static async getAll({
offset = 0,
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),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("type", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})

View file

@ -1,17 +1,12 @@
import { Like, In } from "typeorm";
import { dataSource } from "../../../data-source";
import { inspectionPlan } from "../../../entity/unit/inspection/inspectionPlan";
import { DB_TYPE } from "../../../env.defaults";
import DatabaseActionException from "../../../exceptions/databaseActionException";
export default abstract class InspectionPlanService {
/**
* @description get all inspectionPlans for related
* @returns {Promise<Array<inspectionPlan>>}
*/
static async getAllForRelated(
where: { equipmentId: string } | { vehicleId: string }
): Promise<Array<inspectionPlan>> {
return await dataSource
private static query = () =>
dataSource
.getRepository(inspectionPlan)
.createQueryBuilder("inspectionPlan")
.leftJoinAndMapOne(
@ -24,10 +19,47 @@ export default abstract class InspectionPlanService {
)
.leftJoinAndSelect("latestVersionedPlan.inspectionPoints", "inspectionPoints")
.leftJoinAndSelect("inspectionPlan.equipment", "equipment")
.leftJoinAndSelect("inspectionPlan.vehicle", "vehicle")
.where(where)
.leftJoinAndSelect("inspectionPlan.vehicle", "vehicle");
/**
* @description get all inspectionPlans for related
* @returns {Promise<[Array<inspectionPlan>, number]>}
*/
static async getAllForRelated(
where: { equipmentId: string } | { vehicleId: string },
{
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().where(where);
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("title", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})
@ -41,20 +73,7 @@ export default abstract class InspectionPlanService {
* @returns {Promise<inspectionPlan>}
*/
static async getById(id: string): Promise<inspectionPlan> {
return await dataSource
.getRepository(inspectionPlan)
.createQueryBuilder("inspectionPlan")
.leftJoinAndMapOne(
"inspectionPlan.latestVersionedPlan",
"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)"
)
.leftJoinAndSelect("latestVersionedPlan.inspectionPoints", "inspectionPoints")
.leftJoinAndSelect("inspectionPlan.equipment", "equipment")
.leftJoinAndSelect("inspectionPlan.vehicle", "vehicle")
return await this.query()
.where({ id })
.getOneOrFail()
.then((res) => {

View file

@ -3,12 +3,8 @@ import { inspection } from "../../../entity/unit/inspection/inspection";
import DatabaseActionException from "../../../exceptions/databaseActionException";
export default abstract class InspectionService {
/**
* @description get all inspections for related
* @returns {Promise<Array<inspection>>}
*/
static async getAllForRelated(where: { equipmentId: string } | { vehicleId: string }): Promise<Array<inspection>> {
return await dataSource
private static query = () =>
dataSource
.getRepository(inspection)
.createQueryBuilder("inspection")
.leftJoinAndSelect("inspection.inspectionPlan", "inspectionPlan")
@ -17,10 +13,33 @@ export default abstract class InspectionService {
.leftJoinAndSelect("inspection.pointResults", "pointResults")
.leftJoinAndSelect("pointResults.inspectionPoint", "inspectionPoint")
.leftJoinAndSelect("inspection.equipment", "equipment")
.leftJoinAndSelect("inspection.vehicle", "vehicle")
.where(where)
.leftJoinAndSelect("inspection.vehicle", "vehicle");
/**
* @description get all inspections for related
* @returns {Promise<Array<inspection>>}
*/
static async getAllForRelated(
where: { equipmentId: string } | { vehicleId: string },
{
offset = 0,
count = 25,
noLimit = false,
}: {
offset?: number;
count?: number;
noLimit?: boolean;
}
): Promise<[Array<inspection>, number]> {
let query = this.query().where(where);
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("createdAt", "DESC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})
@ -34,16 +53,7 @@ export default abstract class InspectionService {
* @returns {Promise<inspection>}
*/
static async getById(id: string): Promise<inspection> {
return await dataSource
.getRepository(inspection)
.createQueryBuilder("inspection")
.leftJoinAndSelect("inspection.inspectionPlan", "inspectionPlan")
.leftJoinAndSelect("inspection.inspectionVersionedPlan", "inspectionVersionedPlan")
.leftJoinAndSelect("inspectionVersionedPlan.inspectionPoints", "inspectionPoints")
.leftJoinAndSelect("inspection.pointResults", "pointResults")
.leftJoinAndSelect("pointResults.inspectionPoint", "inspectionPoint")
.leftJoinAndSelect("inspection.equipment", "equipment")
.leftJoinAndSelect("inspection.vehicle", "vehicle")
return await this.query()
.where({ id })
.getOneOrFail()
.then((res) => {

View file

@ -1,3 +1,4 @@
import { Like, In } from "typeorm";
import { dataSource } from "../../../data-source";
import { vehicle } from "../../../entity/unit/vehicle/vehicle";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -5,15 +6,45 @@ import DatabaseActionException from "../../../exceptions/databaseActionException
export default abstract class VehicleService {
/**
* @description get all vehicles
* @returns {Promise<Array<vehicle>>}
* @returns {Promise<[Array<vehicle>, number]>}
*/
static async getAll(): Promise<Array<vehicle>> {
return await dataSource
static async getAll({
offset = 0,
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<vehicle>, number]> {
let query = dataSource
.getRepository(vehicle)
.createQueryBuilder("vehicle")
.leftJoinAndSelect("vehicle.vehicleType", "vehicletype")
.leftJoinAndSelect("vehicle.vehicleType", "vehicletype");
if (search != "") {
query = query.where({
code: Like(search),
name: Like(search),
location: Like(search),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("name", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})

View file

@ -1,3 +1,4 @@
import { Like, In } from "typeorm";
import { dataSource } from "../../../data-source";
import { vehicleType } from "../../../entity/unit/vehicle/vehicleType";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -5,14 +6,40 @@ import DatabaseActionException from "../../../exceptions/databaseActionException
export default abstract class VehicleTypeService {
/**
* @description get all vehicleTypes
* @returns {Promise<Array<vehicleType>>}
* @returns {Promise<[Array<vehicleType>, number]>}
*/
static async getAll(): Promise<Array<vehicleType>> {
return await dataSource
.getRepository(vehicleType)
.createQueryBuilder("vehicleType")
static async getAll({
offset = 0,
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),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("type", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})

View file

@ -1,3 +1,4 @@
import { Like, In } from "typeorm";
import { dataSource } from "../../../data-source";
import { wearable } from "../../../entity/unit/wearable/wearable";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -5,16 +6,46 @@ import DatabaseActionException from "../../../exceptions/databaseActionException
export default abstract class WearableService {
/**
* @description get all wearables
* @returns {Promise<Array<wearable>>}
* @returns {Promise<[Array<wearable>, number]>}
*/
static async getAll(): Promise<Array<wearable>> {
return await dataSource
static async getAll({
offset = 0,
count = 25,
search = "",
noLimit = false,
ids = [],
}: {
offset?: number;
count?: number;
search?: string;
noLimit?: boolean;
ids?: Array<string>;
}): Promise<[Array<wearable>, number]> {
let query = dataSource
.getRepository(wearable)
.createQueryBuilder("wearable")
.leftJoinAndSelect("wearable.wearableType", "wearabletype")
.leftJoinAndSelect("wearable.wearer", "wearer")
.leftJoinAndSelect("wearable.wearer", "wearer");
if (search != "") {
query = query.where({
code: Like(search),
name: Like(search),
location: Like(search),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("name", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})

View file

@ -1,3 +1,4 @@
import { In, Like } from "typeorm";
import { dataSource } from "../../../data-source";
import { wearableType } from "../../../entity/unit/wearable/wearableType";
import DatabaseActionException from "../../../exceptions/databaseActionException";
@ -5,14 +6,40 @@ import DatabaseActionException from "../../../exceptions/databaseActionException
export default abstract class WearableTypeService {
/**
* @description get all wearableTypes
* @returns {Promise<Array<wearableType>>}
* @returns {Promise<[Array<wearableType>, number]>}
*/
static async getAll(): Promise<Array<wearableType>> {
return await dataSource
.getRepository(wearableType)
.createQueryBuilder("wearableType")
static async getAll({
offset = 0,
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),
});
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
.orderBy("type", "ASC")
.getMany()
.getManyAndCount()
.then((res) => {
return res;
})