import { dataSource } from "../data-source"; import { communication } from "../entity/communication"; import { communicationType } from "../entity/communicationType"; import { user } from "../entity/user"; import InternalException from "../exceptions/internalException"; import { CreateCommunicationCommand, DeleteCommunicationCommand, UpdateCommunicationCommand, } from "./communicationCommand"; export default abstract class CommunicationCommandHandler { /** * @description create communication * @param CreateCommunicationCommand * @returns {Promise} */ static async create(createCommunication: CreateCommunicationCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(communication) .values({ preferred: createCommunication.preferred, mobile: createCommunication.mobile, email: createCommunication.email, city: createCommunication.city, street: createCommunication.street, streetNumber: createCommunication.streetNumber, streetNumberAddition: createCommunication.streetNumberAddition, member: await dataSource .getRepository(user) .createQueryBuilder("user") .where("id = :id", { id: createCommunication.memberId }) .getOneOrFail(), type: await dataSource .getRepository(communicationType) .createQueryBuilder("communication") .where("id = :id", { id: createCommunication.typeId }) .getOneOrFail(), }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed creating communication", err); }); } /** * @description update communication * @param UpdateCommunicationCommand * @returns {Promise} */ static async update(updateCommunication: UpdateCommunicationCommand): Promise { return await dataSource .createQueryBuilder() .update(communication) .set({ preferred: updateCommunication.preferred, mobile: updateCommunication.mobile, email: updateCommunication.email, city: updateCommunication.city, street: updateCommunication.street, streetNumber: updateCommunication.streetNumber, streetNumberAddition: updateCommunication.streetNumberAddition, }) .where("id = :id", { id: updateCommunication.id }) .andWhere("memberId = :memberId", { memberId: updateCommunication.memberId }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating communication", err); }); } /** * @description delete communication * @param DeleteCommunicationCommand * @returns {Promise} */ static async delete(deletCommunication: DeleteCommunicationCommand): Promise { return await dataSource .createQueryBuilder() .delete() .from(communication) .where("id = :id", { id: deletCommunication.id }) .andWhere("memberId = :memberId", { memberId: deletCommunication.memberId }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed deleting communication", err); }); } }