import { dataSource } from "../data-source"; import { communication } from "../entity/communication"; import { member } from "../entity/member"; import InternalException from "../exceptions/internalException"; import CommunicationService from "../service/communicationService"; import { CreateMemberCommand, DeleteMemberCommand, UpdateMemberCommand, UpdateMemberNewsletterCommand, } from "./memberCommand"; export default abstract class MemberCommandHandler { /** * @description create member * @param CreateMemberCommand * @returns {Promise} */ static async create(createMember: CreateMemberCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(member) .values({ salutation: createMember.salutation, firstname: createMember.firstname, lastname: createMember.lastname, nameaffix: createMember.nameaffix, birthdate: createMember.birthdate, }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed creating member", err); }); } /** * @description update member * @param UpdateMemberCommand * @returns {Promise} */ static async update(updateMember: UpdateMemberCommand): Promise { console.log(updateMember); return await dataSource .createQueryBuilder() .update(member) .set({ salutation: updateMember.salutation, firstname: updateMember.firstname, lastname: updateMember.lastname, nameaffix: updateMember.nameaffix, birthdate: updateMember.birthdate, }) .where("id = :id", { id: updateMember.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating member", err); }); } /** * @description update member newsletter * @param UpdateMemberCommand * @returns {Promise} */ static async updateNewsletter(updateMember: UpdateMemberNewsletterCommand): Promise { console.log(updateMember); return await dataSource .createQueryBuilder() .update(member) .set({ sendNewsletter: await dataSource .getRepository(communication) .createQueryBuilder("communication") .where("id = :id", { id: updateMember.communicationId }) .andWhere("memberId = :memberId", { memberId: updateMember.id }) .getOneOrFail(), }) .where("id = :id", { id: updateMember.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating member", err); }); } /** * @description delete member * @param DeleteMemberCommand * @returns {Promise} */ static async delete(deleteMember: DeleteMemberCommand): Promise { return await dataSource .createQueryBuilder() .delete() .from(member) .where("id = :id", { id: deleteMember.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed deleting member", err); }); } }