85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
import { dataSource } from "../../data-source";
|
|
import { mission } from "../../entity/operation/mission";
|
|
import DatabaseActionException from "../../exceptions/databaseActionException";
|
|
|
|
export default abstract class MissionService {
|
|
/**
|
|
* @description get all missions
|
|
* @returns {Promise<[Array<mission>, number]>}
|
|
*/
|
|
static async getAll({
|
|
offset = 0,
|
|
count = 25,
|
|
noLimit = false,
|
|
}: {
|
|
offset?: number;
|
|
count?: number;
|
|
noLimit?: boolean;
|
|
}): Promise<[Array<mission>, number]> {
|
|
let query = dataSource.getRepository(mission).createQueryBuilder("mission");
|
|
|
|
if (!noLimit) {
|
|
query = query.offset(offset).limit(count);
|
|
}
|
|
|
|
return await query
|
|
.orderBy("mission.mission_start")
|
|
.addOrderBy("mission.createdAt")
|
|
.getManyAndCount()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "mission", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get mission by id
|
|
* @param {string} id
|
|
* @returns {Promise<mission>}
|
|
*/
|
|
static async getById(id: string): Promise<mission> {
|
|
return dataSource
|
|
.getRepository(mission)
|
|
.createQueryBuilder("mission")
|
|
.where("mission.id = :id", { id: id })
|
|
.getOneOrFail()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "mission", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get full mission by id
|
|
* @param {string} id
|
|
* @returns {Promise<mission>}
|
|
*/
|
|
static async getFullById(id: string): Promise<mission> {
|
|
return dataSource
|
|
.getRepository(mission)
|
|
.createQueryBuilder("mission")
|
|
.leftJoinAndSelect("mission.command", "command")
|
|
.leftJoinAndSelect("mission.secretary", "secretary")
|
|
.leftJoinAndSelect("mission.presence", "presence")
|
|
.leftJoinAndSelect("presence.force", "force")
|
|
.leftJoinAndSelect("mission.vehicles", "vehicles")
|
|
.leftJoinAndSelect("vehicles.driver", "driver")
|
|
.leftJoinAndSelect("vehicles.leader", "leader")
|
|
.leftJoinAndSelect("vehicles.vehicle", "vehicle")
|
|
.leftJoinAndSelect("mission.equipments", "equipments")
|
|
.leftJoinAndSelect("equipments.equipment", "equipment")
|
|
.leftJoinAndSelect("mission.contacts", "contacts")
|
|
.where("mission.id = :id", { id: id })
|
|
.getOneOrFail()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "mission", err);
|
|
});
|
|
}
|
|
}
|