ff-admin-server/src/service/unit/equipment/equipmentService.ts

80 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-06-02 13:14:09 +02:00
import { In, Like } from "typeorm";
2025-05-28 17:06:56 +02:00
import { dataSource } from "../../../data-source";
import { equipment } from "../../../entity/unit/equipment/equipment";
import DatabaseActionException from "../../../exceptions/databaseActionException";
export default abstract class EquipmentService {
/**
2025-05-28 17:32:07 +02:00
* @description get all equipment
2025-06-02 13:14:09 +02:00
* @returns {Promise<[Array<equipment>, number]>}
2025-05-28 17:06:56 +02:00
*/
2025-06-02 13:14:09 +02:00
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
2025-05-28 17:06:56 +02:00
.getRepository(equipment)
.createQueryBuilder("equipment")
2025-06-02 13:14:09 +02:00
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype");
if (search != "") {
query = query
.where({
code: Like(`%${search}%`),
})
.orWhere({
name: Like(`%${search}%`),
})
.orWhere({
location: Like(`%${search}%`),
});
2025-06-02 13:14:09 +02:00
}
if (ids.length != 0) {
query = query.where({ id: In(ids) });
}
if (!noLimit) {
query = query.offset(offset).limit(count);
}
return await query
2025-05-28 17:32:07 +02:00
.orderBy("name", "ASC")
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", "equipment", err);
});
}
/**
* @description get equipment by id
* @returns {Promise<equipment>}
*/
static async getById(id: string): Promise<equipment> {
return await dataSource
.getRepository(equipment)
.createQueryBuilder("equipment")
2025-05-28 17:32:07 +02:00
.leftJoinAndSelect("equipment.equipmentType", "equipmenttype")
2025-05-28 17:06:56 +02:00
.where({ id })
.getOneOrFail()
.then((res) => {
return res;
})
.catch((err) => {
throw new DatabaseActionException("SELECT", "equipment", err);
});
}
}