ff-admin-server/src/service/club/calendarService.ts
2025-01-05 14:14:00 +01:00

86 lines
2.4 KiB
TypeScript

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