diff --git a/src/controller/admin/club/newsletterController.ts b/src/controller/admin/club/newsletterController.ts index 7c322e9..10699a0 100644 --- a/src/controller/admin/club/newsletterController.ts +++ b/src/controller/admin/club/newsletterController.ts @@ -6,7 +6,10 @@ import NewsletterDatesFactory from "../../../factory/admin/club/newsletter/newsl import NewsletterRecipientsService from "../../../service/club/newsletter/newsletterRecipientsService"; import NewsletterRecipientsFactory from "../../../factory/admin/club/newsletter/newsletterRecipients"; import { FileSystemHelper } from "../../../helpers/fileSystemHelper"; -import { CreateNewsletterCommand, SynchronizeNewsletterCommand } from "../../../command/club/newsletter/newsletterCommand"; +import { + CreateNewsletterCommand, + SynchronizeNewsletterCommand, +} from "../../../command/club/newsletter/newsletterCommand"; import NewsletterCommandHandler from "../../../command/club/newsletter/newsletterCommandHandler"; import { SynchronizeNewsletterDatesCommand } from "../../../command/club/newsletter/newsletterDatesCommand"; import NewsletterDatesCommandHandler from "../../../command/club/newsletter/newsletterDatesCommandHandler"; @@ -188,15 +191,14 @@ export async function createNewsletter(req: Request, res: Response): Promise { let newsletterId = parseInt(req.params.newsletterId); - res.setHeader("Content-Type", "text/event-stream"); - res.setHeader("Cache-Control", "no-cache"); - res.setHeader("Connection", "keep-alive"); - - res.flushHeaders(); + res.writeHead(200, { + "Content-Type": "text/plain", + "Transfer-Encoding": "chunked", + }); const progressHandler = (data: NewsletterEventType) => { if (data.newsletterId == newsletterId && data.kind == "pdf") { - res.write(JSON.stringify(data)); + res.write(JSON.stringify(data) + "//"); } }; @@ -287,15 +289,14 @@ export async function sendNewsletterById(req: Request, res: Response): Promise { let newsletterId = parseInt(req.params.newsletterId); - res.setHeader("Content-Type", "text/event-stream"); - res.setHeader("Cache-Control", "no-cache"); - res.setHeader("Connection", "keep-alive"); - - res.flushHeaders(); + res.writeHead(200, { + "Content-Type": "text/plain", + "Transfer-Encoding": "chunked", + }); const progressHandler = (data: NewsletterEventType) => { if (data.newsletterId == newsletterId && data.kind == "mail") { - res.write(JSON.stringify(data)); + res.write(JSON.stringify(data) + "//"); } }; diff --git a/src/helpers/newsletterHelper.ts b/src/helpers/newsletterHelper.ts index b9215c7..eed0fc5 100644 --- a/src/helpers/newsletterHelper.ts +++ b/src/helpers/newsletterHelper.ts @@ -33,12 +33,21 @@ export abstract class NewsletterHelper { private static formatJobEmit( event: "progress" | "complete", kind: "pdf" | "mail", + factor: "success" | "failed" | "info", newsletterId: number, total: number, iteration: number, msg: string ) { - this.jobStatus.emit(event, { kind, newsletterId, total, iteration, msg, date: new Date() }); + this.jobStatus.emit(event, { + kind, + newsletterId, + factor, + total, + iteration, + msg, + date: new Date(), + }); } public static buildData( @@ -169,18 +178,9 @@ export abstract class NewsletterHelper { allowedForMail.includes(m.sendNewsletter?.type?.id) ); - this.formatJobEmit("progress", "mail", newsletterId, mailRecipients.length, 0, "starting sending"); + this.formatJobEmit("progress", "mail", "info", newsletterId, mailRecipients.length, 0, "starting sending"); for (const [index, rec] of mailRecipients.entries()) { - this.formatJobEmit( - "progress", - "mail", - newsletterId, - mailRecipients.length, - index, - `start sending to ${rec.sendNewsletter.email}` - ); - let data = this.buildData(newsletter, dates, rec); const { body } = await TemplateHelper.renderFileForModule({ @@ -195,9 +195,10 @@ export abstract class NewsletterHelper { this.formatJobEmit( "progress", "mail", + "success", newsletterId, mailRecipients.length, - index, + index + 1, `successfully sent to ${rec.sendNewsletter.email}` ); }) @@ -205,9 +206,10 @@ export abstract class NewsletterHelper { this.formatJobEmit( "progress", "mail", + "failed", newsletterId, mailRecipients.length, - index, + index + 1, `failed to send to ${rec.sendNewsletter.email}` ); }); @@ -216,6 +218,7 @@ export abstract class NewsletterHelper { this.formatJobEmit( "complete", "mail", + "info", newsletterId, mailRecipients.length, mailRecipients.length, @@ -246,21 +249,12 @@ export abstract class NewsletterHelper { (m) => !notAllowedForPdf.includes(m.sendNewsletter?.type?.id) || m.sendNewsletter == null ); - this.formatJobEmit("progress", "pdf", newsletterId, pdfRecipients.length, 0, "starting sending"); + this.formatJobEmit("progress", "pdf", "info", newsletterId, pdfRecipients.length + 1, 0, "starting sending"); for (const [index, rec] of [ ...pdfRecipients, { id: 0, firstname: "Alle Mitglieder", lastname: CLUB_NAME } as member, ].entries()) { - this.formatJobEmit( - "progress", - "pdf", - newsletterId, - pdfRecipients.length, - index, - `start print for ${rec.lastname}, ${rec.firstname}` - ); - let data = this.buildData(newsletter, dates, rec, printWithAdress.includes(rec.sendNewsletter?.type?.id)); await PdfExport.renderFile({ @@ -274,9 +268,10 @@ export abstract class NewsletterHelper { this.formatJobEmit( "progress", "pdf", + "success", newsletterId, - pdfRecipients.length, - index, + pdfRecipients.length + 1, + index + 1, `successfully printed for ${rec.lastname}, ${rec.firstname}` ); }) @@ -284,23 +279,15 @@ export abstract class NewsletterHelper { this.formatJobEmit( "progress", "pdf", + "failed", newsletterId, - pdfRecipients.length, - index, + pdfRecipients.length + 1, + index + 1, `failed print for ${rec.lastname}, ${rec.firstname}` ); }); } - this.formatJobEmit( - "progress", - "pdf", - newsletterId, - pdfRecipients.length, - pdfRecipients.length, - "starting pdf combine" - ); - await PdfExport.sqashToSingleFile( `newsletter/${newsletter.id}_${newsletter.title.replace(" ", "")}`, "allPdfsTogether", @@ -310,9 +297,10 @@ export abstract class NewsletterHelper { this.formatJobEmit( "progress", "pdf", + "success", newsletterId, - pdfRecipients.length, - pdfRecipients.length, + pdfRecipients.length + 1, + pdfRecipients.length + 1, "sucessfully combined pdf" ); }) @@ -320,9 +308,10 @@ export abstract class NewsletterHelper { this.formatJobEmit( "progress", "pdf", + "failed", newsletterId, - pdfRecipients.length, - pdfRecipients.length, + pdfRecipients.length + 1, + pdfRecipients.length + 1, "failed combining pdf" ); }); @@ -330,9 +319,10 @@ export abstract class NewsletterHelper { this.formatJobEmit( "complete", "pdf", + "info", newsletterId, - pdfRecipients.length, - pdfRecipients.length, + pdfRecipients.length + 1, + pdfRecipients.length + 1, `completed printing process` ); }