diff --git a/src/controller/admin/unit/equipmentController.ts b/src/controller/admin/unit/equipmentController.ts index 8546df7..8d7e200 100644 --- a/src/controller/admin/unit/equipmentController.ts +++ b/src/controller/admin/unit/equipmentController.ts @@ -21,8 +21,7 @@ export async function getAllEquipments(req: Request, res: Response): Promise 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} + */ +export async function getEquipmentsByIds(req: Request, res: Response): Promise { + let ids = req.body.ids as Array; + + 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 diff --git a/src/controller/admin/unit/equipmentTypeController.ts b/src/controller/admin/unit/equipmentTypeController.ts index 27e387f..dce7b77 100644 --- a/src/controller/admin/unit/equipmentTypeController.ts +++ b/src/controller/admin/unit/equipmentTypeController.ts @@ -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, diff --git a/src/controller/admin/unit/inspectionController.ts b/src/controller/admin/unit/inspectionController.ts index c1f183b..8c4b782 100644 --- a/src/controller/admin/unit/inspectionController.ts +++ b/src/controller/admin/unit/inspectionController.ts @@ -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, diff --git a/src/controller/admin/unit/inspectionPlanController.ts b/src/controller/admin/unit/inspectionPlanController.ts index 4b60588..358ac66 100644 --- a/src/controller/admin/unit/inspectionPlanController.ts +++ b/src/controller/admin/unit/inspectionPlanController.ts @@ -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, diff --git a/src/controller/admin/unit/vehicleController.ts b/src/controller/admin/unit/vehicleController.ts index 55c1902..02b3606 100644 --- a/src/controller/admin/unit/vehicleController.ts +++ b/src/controller/admin/unit/vehicleController.ts @@ -21,8 +21,7 @@ export async function getAllVehicles(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 [vehicles, total] = await VehicleService.getAll(); + let [vehicles, total] = await VehicleService.getAll({ offset, count, search, noLimit, ids }); res.json({ vehicles: vehicles, diff --git a/src/controller/admin/unit/vehicleTypeController.ts b/src/controller/admin/unit/vehicleTypeController.ts index c11d1f1..7cf5552 100644 --- a/src/controller/admin/unit/vehicleTypeController.ts +++ b/src/controller/admin/unit/vehicleTypeController.ts @@ -21,8 +21,7 @@ export async function getAllVehicleTypes(req: Request, res: Response): Promise 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, diff --git a/src/controller/admin/unit/wearableController.ts b/src/controller/admin/unit/wearableController.ts index 209eaa5..7775c36 100644 --- a/src/controller/admin/unit/wearableController.ts +++ b/src/controller/admin/unit/wearableController.ts @@ -21,8 +21,7 @@ export async function getAllWearables(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 [wearables, total] = await WearableService.getAll(); + let [wearables, total] = await WearableService.getAll({ offset, count, search, noLimit, ids }); res.json({ wearables: wearables, diff --git a/src/controller/admin/unit/wearableTypeController.ts b/src/controller/admin/unit/wearableTypeController.ts index fffc969..40def8b 100644 --- a/src/controller/admin/unit/wearableTypeController.ts +++ b/src/controller/admin/unit/wearableTypeController.ts @@ -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, diff --git a/src/routes/admin/unit/equipment.ts b/src/routes/admin/unit/equipment.ts index 96f432c..4e45a52 100644 --- a/src/routes/admin/unit/equipment.ts +++ b/src/routes/admin/unit/equipment.ts @@ -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"), diff --git a/src/service/unit/equipment/equipmentService.ts b/src/service/unit/equipment/equipmentService.ts index a2e1823..7e2267b 100644 --- a/src/service/unit/equipment/equipmentService.ts +++ b/src/service/unit/equipment/equipmentService.ts @@ -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>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { - return await dataSource + static async getAll({ + offset = 0, + count = 25, + search = "", + noLimit = false, + ids = [], + }: { + offset?: number; + count?: number; + search?: string; + noLimit?: boolean; + ids?: Array; + }): Promise<[Array, 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; }) diff --git a/src/service/unit/equipment/equipmentTypeService.ts b/src/service/unit/equipment/equipmentTypeService.ts index 237b0cd..8789fe7 100644 --- a/src/service/unit/equipment/equipmentTypeService.ts +++ b/src/service/unit/equipment/equipmentTypeService.ts @@ -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>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { - 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; + }): Promise<[Array, 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; }) diff --git a/src/service/unit/inspection/inspectionPlanService.ts b/src/service/unit/inspection/inspectionPlanService.ts index e9495fe..2514ad1 100644 --- a/src/service/unit/inspection/inspectionPlanService.ts +++ b/src/service/unit/inspection/inspectionPlanService.ts @@ -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>} - */ - static async getAllForRelated( - where: { equipmentId: string } | { vehicleId: string } - ): Promise> { - 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, 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; + } + ): Promise<[Array, 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} */ static async getById(id: string): Promise { - 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) => { diff --git a/src/service/unit/inspection/inspectionService.ts b/src/service/unit/inspection/inspectionService.ts index 0fdf490..709b335 100644 --- a/src/service/unit/inspection/inspectionService.ts +++ b/src/service/unit/inspection/inspectionService.ts @@ -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>} - */ - static async getAllForRelated(where: { equipmentId: string } | { vehicleId: string }): Promise> { - 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>} + */ + static async getAllForRelated( + where: { equipmentId: string } | { vehicleId: string }, + { + offset = 0, + count = 25, + noLimit = false, + }: { + offset?: number; + count?: number; + noLimit?: boolean; + } + ): Promise<[Array, 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} */ static async getById(id: string): Promise { - 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) => { diff --git a/src/service/unit/vehicle/vehicleService.ts b/src/service/unit/vehicle/vehicleService.ts index 39d7b06..921a981 100644 --- a/src/service/unit/vehicle/vehicleService.ts +++ b/src/service/unit/vehicle/vehicleService.ts @@ -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>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { - return await dataSource + static async getAll({ + offset = 0, + count = 25, + search = "", + noLimit = false, + ids = [], + }: { + offset?: number; + count?: number; + search?: string; + noLimit?: boolean; + ids?: Array; + }): Promise<[Array, 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; }) diff --git a/src/service/unit/vehicle/vehicleTypeService.ts b/src/service/unit/vehicle/vehicleTypeService.ts index 8ba8104..6ecb703 100644 --- a/src/service/unit/vehicle/vehicleTypeService.ts +++ b/src/service/unit/vehicle/vehicleTypeService.ts @@ -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>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { - 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; + }): Promise<[Array, 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; }) diff --git a/src/service/unit/wearable/wearableService.ts b/src/service/unit/wearable/wearableService.ts index 7733d4a..b369f47 100644 --- a/src/service/unit/wearable/wearableService.ts +++ b/src/service/unit/wearable/wearableService.ts @@ -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>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { - return await dataSource + static async getAll({ + offset = 0, + count = 25, + search = "", + noLimit = false, + ids = [], + }: { + offset?: number; + count?: number; + search?: string; + noLimit?: boolean; + ids?: Array; + }): Promise<[Array, 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; }) diff --git a/src/service/unit/wearable/wearableTypeService.ts b/src/service/unit/wearable/wearableTypeService.ts index 63a696b..d6f167f 100644 --- a/src/service/unit/wearable/wearableTypeService.ts +++ b/src/service/unit/wearable/wearableTypeService.ts @@ -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>} + * @returns {Promise<[Array, number]>} */ - static async getAll(): Promise> { - 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; + }): Promise<[Array, 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; })