99 lines
2.5 KiB
TypeScript
99 lines
2.5 KiB
TypeScript
import { In, Like } from "typeorm";
|
|
import { dataSource } from "../../../data-source";
|
|
import { equipment } from "../../../entity/unit/equipment/equipment";
|
|
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
|
|
|
export default abstract class EquipmentService {
|
|
/**
|
|
* @description get all equipment
|
|
* @returns {Promise<[Array<equipment>, number]>}
|
|
*/
|
|
static async getAll({
|
|
offset = 0,
|
|
count = 25,
|
|
search = "",
|
|
noLimit = false,
|
|
ids = [],
|
|
}: {
|
|
offset?: number;
|
|
count?: number;
|
|
search?: string;
|
|
noLimit?: boolean;
|
|
ids?: Array<string>;
|
|
}): Promise<[Array<equipment>, number]> {
|
|
let query = dataSource
|
|
.getRepository(equipment)
|
|
.createQueryBuilder("equipment")
|
|
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype");
|
|
|
|
if (search != "") {
|
|
query = query
|
|
.where({
|
|
code: Like(`%${search}%`),
|
|
})
|
|
.orWhere({
|
|
name: Like(`%${search}%`),
|
|
})
|
|
.orWhere({
|
|
location: Like(`%${search}%`),
|
|
});
|
|
}
|
|
|
|
if (ids.length != 0) {
|
|
query = query.where({ id: In(ids) });
|
|
}
|
|
|
|
if (!noLimit) {
|
|
query = query.offset(offset).limit(count);
|
|
}
|
|
|
|
return await query
|
|
.orderBy("name", "ASC")
|
|
.getManyAndCount()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "equipment", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get equipment by code
|
|
* @returns {Promise<Array<equipment>>}
|
|
*/
|
|
static async getAllByCode(code: string): Promise<Array<equipment>> {
|
|
return await dataSource
|
|
.getRepository(equipment)
|
|
.createQueryBuilder("equipment")
|
|
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype")
|
|
.where({ code: Like(`%${code}%`) })
|
|
.orderBy("name", "ASC")
|
|
.getMany()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "equipment", err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get equipment by id
|
|
* @returns {Promise<equipment>}
|
|
*/
|
|
static async getById(id: string): Promise<equipment> {
|
|
return await dataSource
|
|
.getRepository(equipment)
|
|
.createQueryBuilder("equipment")
|
|
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype")
|
|
.where({ id })
|
|
.getOneOrFail()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new DatabaseActionException("SELECT", "equipment", err);
|
|
});
|
|
}
|
|
}
|