ff-admin-server/src/command/newsletterCommandHandler.ts

73 lines
2.2 KiB
TypeScript

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<number>}
*/
static async create(createNewsletter: CreateNewsletterCommand): Promise<number> {
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<void>}
*/
static async sync(syncNewsletter: SynchronizeNewsletterCommand): Promise<void> {
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<void>}
*/
static async send(syncNewsletter: SendNewsletterCommand): Promise<void> {
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);
});
}
}