This commit is contained in:
Marco Pedone 2026-03-04 20:45:21 +01:00
parent b500c7133f
commit c423a7bdb7
2 changed files with 99 additions and 19 deletions

View file

@ -1,17 +1,49 @@
import type { Override } from "TypeHelpers.type"; import type { Override } from "TypeHelpers.type";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { add, set } from "date-fns"; 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 { NewEventQueue } from "~/schemas/public/EventQueue";
import type { OrdiniOrdineId } from "~/schemas/public/Ordini";
import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe";
import { import {
type MinimalSMSData, type MinimalSMSData,
SendMessage, SendMessage,
} from "~/server/controllers/skebby.controller"; } from "~/server/controllers/skebby.controller";
import { db } from "~/server/db"; import { db } from "~/server/db";
import { NewMail, type NewMailProps } from "~/server/services/mailer"; 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 = { type Email_EventData = {
tipo: "email"; 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 = { 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 () => { export const processEvents = async () => {
const events = await getEventsFromQueue(); const events = await getEventsFromQueue();
if (events.length === 0) { if (events.length === 0) {
@ -77,9 +139,7 @@ export const processEvents = async () => {
switch (data.tipo) { switch (data.tipo) {
case "email": { case "email": {
await NewMail({ await processEmailEvent(data);
...data.data,
});
break; break;
} }

View file

@ -183,17 +183,25 @@ export const whIntentSucceededHandler = async ({
{ {
filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`, filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`,
//ATTENTION: hardcoded condizioni key //ATTENTION: hardcoded condizioni key
content: await genPdfCondizioniBase64( // content: await genPdfCondizioniBase64(
"CONDIZIONI_CERCHI" as TestiEStringheStingaId, // "CONDIZIONI_CERCHI" as TestiEStringheStingaId,
), // ),
generate: {
type: "condizioni",
stringId: "CONDIZIONI_CERCHI" as TestiEStringheStingaId,
},
encoding: "base64", encoding: "base64",
contentType: "application/pdf", contentType: "application/pdf",
}, },
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
content: await genPdfRicevutaCortesiaBase64( // content: await genPdfRicevutaCortesiaBase64(
ordine.ordine_id, // ordine.ordine_id,
), // ),
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64", encoding: "base64",
contentType: "application/pdf", contentType: "application/pdf",
}, },
@ -231,9 +239,13 @@ export const whIntentSucceededHandler = async ({
attachments: [ attachments: [
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
content: await genPdfRicevutaCortesiaBase64( generate: {
ordine.ordine_id, type: "ricevuta_cortesia",
), ordineId: ordine.ordine_id,
},
// content: await genPdfRicevutaCortesiaBase64(
// ordine.ordine_id,
// ),
encoding: "base64", encoding: "base64",
contentType: "application/pdf", contentType: "application/pdf",
}, },
@ -267,9 +279,13 @@ export const whIntentSucceededHandler = async ({
attachments: [ attachments: [
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
content: await genPdfRicevutaCortesiaBase64( // content: await genPdfRicevutaCortesiaBase64(
ordine.ordine_id, // ordine.ordine_id,
), // ),
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64", encoding: "base64",
contentType: "application/pdf", contentType: "application/pdf",
}, },
@ -297,9 +313,13 @@ export const whIntentSucceededHandler = async ({
attachments: [ attachments: [
{ {
filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`,
content: await genPdfRicevutaCortesiaBase64( // content: await genPdfRicevutaCortesiaBase64(
ordine.ordine_id, // ordine.ordine_id,
), // ),
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64", encoding: "base64",
contentType: "application/pdf", contentType: "application/pdf",
}, },