ff-admin-server/src/service/communicationService.ts
2024-11-27 10:07:59 +01:00

61 lines
2.2 KiB
TypeScript

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<Array<communication>>}
*/
static async getAll(memberId: number): Promise<Array<communication>> {
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<communication>}
*/
static async getById(memberId: number, recordId: number): Promise<communication> {
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<string>}
*/
static getAvailableColumnsForCommunication(): Array<string> {
let metadata = dataSource.getMetadata(communication);
let columns = metadata.columns.map((c) => c.propertyName);
return columns.filter((c) => !["id", "preferred", "isSMSAlarming", "type", "member"].includes(c));
}
}