2025-05-28 22:51:17 +02:00
|
|
|
import { dataSource } from "../../data-source";
|
|
|
|
import { maintenance } from "../../entity/unit/maintenance";
|
|
|
|
import DatabaseActionException from "../../exceptions/databaseActionException";
|
|
|
|
|
|
|
|
export default abstract class MaintenanceService {
|
2025-06-13 11:31:34 +02:00
|
|
|
private static query = () =>
|
|
|
|
dataSource
|
2025-05-28 22:51:17 +02:00
|
|
|
.getRepository(maintenance)
|
|
|
|
.createQueryBuilder("maintenance")
|
|
|
|
.leftJoinAndSelect("maintenance.equipment", "equipment")
|
|
|
|
.leftJoinAndSelect("maintenance.vehicle", "vehicle")
|
|
|
|
.leftJoinAndSelect("maintenance.wearable", "wearable")
|
2025-06-13 11:31:34 +02:00
|
|
|
.leftJoinAndSelect("maintenance.reports", "reports");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all maintenances
|
|
|
|
* @returns {Promise<[Array<maintenance>, number]>}
|
|
|
|
*/
|
|
|
|
static async getAll(
|
|
|
|
done = false,
|
|
|
|
{
|
|
|
|
offset = 0,
|
|
|
|
count = 25,
|
|
|
|
noLimit = false,
|
|
|
|
}: {
|
|
|
|
offset?: number;
|
|
|
|
count?: number;
|
|
|
|
noLimit?: boolean;
|
|
|
|
}
|
|
|
|
): Promise<[Array<maintenance>, number]> {
|
|
|
|
let query = this.query().where({ done });
|
|
|
|
if (!noLimit) {
|
|
|
|
query = query.offset(offset).limit(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await query
|
|
|
|
.orderBy("maintenance.type", "ASC")
|
|
|
|
.getManyAndCount()
|
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new DatabaseActionException("SELECT", "maintenance", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get all maintenances By related
|
|
|
|
* @returns {Promise<[Array<maintenance>, number]>}
|
|
|
|
*/
|
|
|
|
static async getAllForRelated(
|
|
|
|
where: { equipmentId: string } | { vehicleId: string } | { wearableId: string },
|
|
|
|
{
|
|
|
|
offset = 0,
|
|
|
|
count = 25,
|
|
|
|
noLimit = false,
|
|
|
|
}: {
|
|
|
|
offset?: number;
|
|
|
|
count?: number;
|
|
|
|
noLimit?: boolean;
|
|
|
|
}
|
|
|
|
): Promise<[Array<maintenance>, number]> {
|
|
|
|
let query = this.query().where(where);
|
|
|
|
|
|
|
|
if (!noLimit) {
|
|
|
|
query = query.offset(offset).limit(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await query
|
|
|
|
.orderBy("maintenance.createdAt", "ASC")
|
|
|
|
.getManyAndCount()
|
2025-05-28 22:51:17 +02:00
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new DatabaseActionException("SELECT", "maintenance", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get maintenance by id
|
|
|
|
* @returns {Promise<maintenance>}
|
|
|
|
*/
|
|
|
|
static async getById(id: string): Promise<maintenance> {
|
2025-06-13 11:31:34 +02:00
|
|
|
return await this.query()
|
2025-05-28 22:51:17 +02:00
|
|
|
.where({ id })
|
|
|
|
.getOneOrFail()
|
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new DatabaseActionException("SELECT", "maintenance", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|