import { dataSource } from "../data-source"; import { calendar } from "../entity/calendar"; import InternalException from "../exceptions/internalException"; export default abstract class CalendarService { /** * @description get all calendars * @returns {Promise>} */ static async getAll(): Promise> { return await dataSource .getRepository(calendar) .createQueryBuilder("calendar") .leftJoinAndSelect("calendar.type", "type") .getMany() .then((res) => { return res; }) .catch((err) => { throw new InternalException("calendars not found", err); }); } /** * @description get calendar by id * @returns {Promise} */ static async getById(id: number): Promise { return await dataSource .getRepository(calendar) .createQueryBuilder("calendar") .leftJoinAndSelect("calendar.type", "type") .where("calendar.id = :id", { id: id }) .getOneOrFail() .then((res) => { return res; }) .catch((err) => { throw new InternalException("calendar not found by id", err); }); } /** * @description get calendar by types * @returns {Promise>} */ static async getByTypes(types: Array): Promise> { return await dataSource .getRepository(calendar) .createQueryBuilder("calendar") .leftJoinAndSelect("calendar.type", "type") .where("type.id IN (:...types)", { types: types }) .getMany() .then((res) => { return res; }) .catch((err) => { throw new InternalException("calendars not found by types", err); }); } /** * @description get calendar by types nscdr * @returns {Promise>} */ static async getByTypeNSCDR(): Promise> { return await dataSource .getRepository(calendar) .createQueryBuilder("calendar") .leftJoinAndSelect("calendar.type", "type") .where("type.nscdr = :nscdr)", { nscdr: true }) .getMany() .then((res) => { return res; }) .catch((err) => { throw new InternalException("calendars not found by type nscdr", err); }); } }