ff-admin-server/src/service/unit/inspection/inspectionService.ts

67 lines
2 KiB
TypeScript
Raw Normal View History

2025-05-28 17:06:56 +02:00
import { dataSource } from "../../../data-source";
import { inspection } from "../../../entity/unit/inspection/inspection";
import DatabaseActionException from "../../../exceptions/databaseActionException";
export default abstract class InspectionService {
2025-06-02 13:14:09 +02:00
private static query = () =>
dataSource
2025-05-28 17:06:56 +02:00
.getRepository(inspection)
.createQueryBuilder("inspection")
2025-05-28 17:32:07 +02:00
.leftJoinAndSelect("inspection.inspectionPlan", "inspectionPlan")
.leftJoinAndSelect("inspection.inspectionVersionedPlan", "inspectionVersionedPlan")
.leftJoinAndSelect("inspectionVersionedPlan.inspectionPoints", "inspectionPoints")
.leftJoinAndSelect("inspection.pointResults", "pointResults")
.leftJoinAndSelect("pointResults.inspectionPoint", "inspectionPoint")
2025-05-28 18:30:00 +02:00
.leftJoinAndSelect("inspection.equipment", "equipment")
2025-06-02 13:14:09 +02:00
.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
2025-05-28 17:32:07 +02:00
.orderBy("createdAt", "DESC")
2025-06-02 13:14:09 +02:00
.getManyAndCount()
2025-05-28 17:06:56 +02:00
.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> {
2025-06-02 13:14:09 +02:00
return await this.query()
2025-05-28 17:06:56 +02:00
.where({ id })
.getOneOrFail()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "inspection", err);
});
}
}