import { dataSource } from "../data-source"; import { communication } from "../entity/communication"; import InternalException from "../exceptions/internalException"; export default abstract class CommunicationService { /** * @description get all by member id * @param {number} memberId * @returns {Promise>} */ static async getAll(memberId: number): Promise> { return await dataSource .getRepository(communication) .createQueryBuilder("communication") .leftJoinAndSelect("communication.type", "communicationType") .leftJoinAndSelect("communication.member", "member") .leftJoinAndSelect("member.sendNewsletter", "sendNewsletter") .where("communication.memberId = :memberId", { memberId: memberId }) .getMany() .then((res) => { return res; }) .catch((err) => { throw new InternalException("member communications not found", err); }); } /** * @description get all by memberId and recordId * @param {number} memberId * @param {number} recordId * @returns {Promise} */ static async getById(memberId: number, recordId: number): Promise { return await dataSource .getRepository(communication) .createQueryBuilder("communication") .leftJoinAndSelect("communication.type", "communicationType") .leftJoinAndSelect("communication.member", "member") .leftJoinAndSelect("member.sendNewsletter", "sendNewsletter") .where("communication.memberId = :memberId", { memberId: memberId }) .andWhere("communication.id = :recordId", { recordId: recordId }) .getOneOrFail() .then((res) => { return res; }) .catch((err) => { throw new InternalException("member communication not found by id", err); }); } /** * @description get available columns for self made com type * @returns {Array} */ static getAvailableColumnsForCommunication(): Array { let metadata = dataSource.getMetadata(communication); let columns = metadata.columns.map((c) => c.propertyName); return columns.filter((c) => !["id", "preferred", "isSMSAlarming", "type", "member"].includes(c)); } }