import { dataSource } from "../data-source"; import { newsletter } from "../entity/newsletter"; import InternalException from "../exceptions/internalException"; import { CreateNewsletterCommand, SendNewsletterCommand, SynchronizeNewsletterCommand } from "./newsletterCommand"; export default abstract class NewsletterCommandHandler { /** * @description create newsletter * @param CreateNewsletterCommand * @returns {Promise} */ static async create(createNewsletter: CreateNewsletterCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(newsletter) .values({ title: createNewsletter.title, }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed creating newsletter", err); }); } /** * @description sync newsletter * @param SynchronizeNewsletterCommand * @returns {Promise} */ static async sync(syncNewsletter: SynchronizeNewsletterCommand): Promise { return await dataSource .createQueryBuilder() .update(newsletter) .set({ title: syncNewsletter.title, description: syncNewsletter.description, newsletterTitle: syncNewsletter.newsletterTitle, newsletterText: syncNewsletter.newsletterText, newsletterSignatur: syncNewsletter.newsletterSignatur, recipientsByQueryId: syncNewsletter.recipientsByQueryId, }) .where("id = :id", { id: syncNewsletter.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed synching newsletter", err); }); } /** * @description send newsletter * @param SendNewsletterCommand * @returns {Promise} */ static async send(syncNewsletter: SendNewsletterCommand): Promise { return await dataSource .createQueryBuilder() .update(newsletter) .set({ isSent: syncNewsletter.isSent, }) .where("id = :id", { id: syncNewsletter.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed setting newsletter send state", err); }); } }