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, number]>} */ static async getAll({ offset = 0, count = 25, search = "", noLimit = false, ids = [], }: { offset?: number; count?: number; search?: string; noLimit?: boolean; ids?: Array; }): Promise<[Array, 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>} */ static async getAllByCode(code: string): Promise> { 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} */ static async getById(id: string): Promise { 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); }); } }