66 lines
2 KiB
TypeScript
66 lines
2 KiB
TypeScript
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<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")
|
|
.getManyAndCount()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "inspection", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get inspection by id
|
|
* @returns {Promise<inspection>}
|
|
*/
|
|
static async getById(id: string): Promise<inspection> {
|
|
return await this.query()
|
|
.where({ id })
|
|
.getOneOrFail()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "inspection", err);
|
|
});
|
|
}
|
|
}
|