diff --git a/apps/infoalloggi/src/server/controllers/event_queue.controller.ts b/apps/infoalloggi/src/server/controllers/event_queue.controller.ts index fd93ec9..148ce35 100644 --- a/apps/infoalloggi/src/server/controllers/event_queue.controller.ts +++ b/apps/infoalloggi/src/server/controllers/event_queue.controller.ts @@ -1,17 +1,49 @@ import type { Override } from "TypeHelpers.type"; import { TRPCError } from "@trpc/server"; import { add, set } from "date-fns"; +import type { Attachment } from "nodemailer/lib/mailer"; +import type { AnnunciId } from "~/schemas/public/Annunci"; import type { NewEventQueue } from "~/schemas/public/EventQueue"; +import type { OrdiniOrdineId } from "~/schemas/public/Ordini"; +import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe"; import { type MinimalSMSData, SendMessage, } from "~/server/controllers/skebby.controller"; import { db } from "~/server/db"; import { NewMail, type NewMailProps } from "~/server/services/mailer"; +import { + genPdfCondizioniBase64, + genPdfRicevutaCortesiaBase64, + genPdfSchedaAnnuncioBase64, +} from "../services/puppeteer.service"; +type GeneratedAttachmentTypes = + | { type: "scheda_annuncio"; annuncioId: AnnunciId } + | { type: "ricevuta_cortesia"; ordineId: OrdiniOrdineId } + | { type: "condizioni"; stringId: TestiEStringheStingaId }; + +type CustomAttachment = Attachment & { generate?: GeneratedAttachmentTypes }; type Email_EventData = { tipo: "email"; - data: NewMailProps; + data: NewMailProps & { + mail: { + attachments?: CustomAttachment[]; + }; + }; +}; + +const generatePdfContent = async (params: GeneratedAttachmentTypes) => { + switch (params.type) { + case "ricevuta_cortesia": + return await genPdfRicevutaCortesiaBase64(params.ordineId); + case "condizioni": + return await genPdfCondizioniBase64(params.stringId); + case "scheda_annuncio": + return await genPdfSchedaAnnuncioBase64(params.annuncioId); + default: + throw new Error(`Unknown PDF type: `); + } }; type SMS_EventData = { @@ -67,6 +99,36 @@ const getEventsFromQueue = async () => { } }; +const processEmailEvent = async (event: Email_EventData) => { + const { template, mail, userId } = event.data; + + // Generate attachments on-demand + if (mail.attachments && mail.attachments.length > 0) { + mail.attachments = await Promise.all( + mail.attachments.map(async (attachment: CustomAttachment) => { + // If attachment has generate metadata, generate it now + if (attachment.generate) { + const content = await generatePdfContent(attachment.generate); + return { + filename: attachment.filename, + content, + encoding: attachment.encoding, + contentType: attachment.contentType, + }; + } + // If already has content, return as-is + return attachment; + }), + ); + } + + await NewMail({ + template, + mail, + userId, + }); +}; + export const processEvents = async () => { const events = await getEventsFromQueue(); if (events.length === 0) { @@ -77,9 +139,7 @@ export const processEvents = async () => { switch (data.tipo) { case "email": { - await NewMail({ - ...data.data, - }); + await processEmailEvent(data); break; } diff --git a/apps/infoalloggi/src/server/controllers/stripe.controller.ts b/apps/infoalloggi/src/server/controllers/stripe.controller.ts index be27bd0..d15c8e0 100644 --- a/apps/infoalloggi/src/server/controllers/stripe.controller.ts +++ b/apps/infoalloggi/src/server/controllers/stripe.controller.ts @@ -183,17 +183,25 @@ export const whIntentSucceededHandler = async ({ { filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`, //ATTENTION: hardcoded condizioni key - content: await genPdfCondizioniBase64( - "CONDIZIONI_CERCHI" as TestiEStringheStingaId, - ), + // content: await genPdfCondizioniBase64( + // "CONDIZIONI_CERCHI" as TestiEStringheStingaId, + // ), + generate: { + type: "condizioni", + stringId: "CONDIZIONI_CERCHI" as TestiEStringheStingaId, + }, encoding: "base64", contentType: "application/pdf", }, { filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64( - ordine.ordine_id, - ), + // content: await genPdfRicevutaCortesiaBase64( + // ordine.ordine_id, + // ), + generate: { + type: "ricevuta_cortesia", + ordineId: ordine.ordine_id, + }, encoding: "base64", contentType: "application/pdf", }, @@ -231,9 +239,13 @@ export const whIntentSucceededHandler = async ({ attachments: [ { filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64( - ordine.ordine_id, - ), + generate: { + type: "ricevuta_cortesia", + ordineId: ordine.ordine_id, + }, + // content: await genPdfRicevutaCortesiaBase64( + // ordine.ordine_id, + // ), encoding: "base64", contentType: "application/pdf", }, @@ -267,9 +279,13 @@ export const whIntentSucceededHandler = async ({ attachments: [ { filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64( - ordine.ordine_id, - ), + // content: await genPdfRicevutaCortesiaBase64( + // ordine.ordine_id, + // ), + generate: { + type: "ricevuta_cortesia", + ordineId: ordine.ordine_id, + }, encoding: "base64", contentType: "application/pdf", }, @@ -297,9 +313,13 @@ export const whIntentSucceededHandler = async ({ attachments: [ { filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64( - ordine.ordine_id, - ), + // content: await genPdfRicevutaCortesiaBase64( + // ordine.ordine_id, + // ), + generate: { + type: "ricevuta_cortesia", + ordineId: ordine.ordine_id, + }, encoding: "base64", contentType: "application/pdf", },