import { dataSource } from "../../../data-source"; import { inspection } from "../../../entity/unit/inspection/inspection"; import DatabaseActionException from "../../../exceptions/databaseActionException"; export default abstract class InspectionService { private static query = () => 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"); /** * @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") .getManyAndCount() .then((res) => { return res; }) .catch((err) => { throw new DatabaseActionException("SELECT", "inspection", err); }); } /** * @description get inspection by id * @returns {Promise} */ static async getById(id: string): Promise { return await this.query() .where({ id }) .getOneOrFail() .then((res) => { return res; }) .catch((err) => { throw new DatabaseActionException("SELECT", "inspection", err); }); } }