diff --git a/apps/infoalloggi/src/server/controllers/stripe.controller.ts b/apps/infoalloggi/src/server/controllers/stripe.controller.ts index efd261e..be27bd0 100644 --- a/apps/infoalloggi/src/server/controllers/stripe.controller.ts +++ b/apps/infoalloggi/src/server/controllers/stripe.controller.ts @@ -13,6 +13,7 @@ import { genPdfCondizioniBase64, genPdfRicevutaCortesiaBase64, } from "../services/puppeteer.service"; +import { addEventToQueue } from "./event_queue.controller"; export const stripeHealthCheck = async () => { try { @@ -167,34 +168,44 @@ export const whIntentSucceededHandler = async ({ }) .execute(); - //SERVIZIO ATTIVATO - await NewMail({ - template: { - mailType: "servizioAttivato", - }, - mail: { - subject: "Pagamento Confermato - Acconto Infoalloggi.it", - to: utente.email, - attachments: [ - { - filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`, - //ATTENTION: hardcoded condizioni key - content: await genPdfCondizioniBase64( - "CONDIZIONI_CERCHI" as TestiEStringheStingaId, - ), - encoding: "base64", - contentType: "application/pdf", + //TODO inserire gli allegati solo se si ha il pdf generato + await addEventToQueue({ + data: { + tipo: "email", + data: { + template: { + mailType: "servizioAttivato", }, - { - filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64(ordine.ordine_id), - encoding: "base64", - contentType: "application/pdf", + mail: { + subject: "Pagamento Confermato - Acconto Infoalloggi.it", + to: utente.email, + attachments: [ + { + filename: `condizioni_contrattuali_${ordine.created_at.toISOString()}.pdf`, + //ATTENTION: hardcoded condizioni key + content: await genPdfCondizioniBase64( + "CONDIZIONI_CERCHI" as TestiEStringheStingaId, + ), + encoding: "base64", + contentType: "application/pdf", + }, + { + filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, + content: await genPdfRicevutaCortesiaBase64( + ordine.ordine_id, + ), + encoding: "base64", + contentType: "application/pdf", + }, + ], }, - ], + userId: utente.id, + }, }, - userId: utente.id, + lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute }); + //SERVIZIO ATTIVATO + break; case OrderTypeEnum.Saldo: @@ -207,24 +218,33 @@ export const whIntentSucceededHandler = async ({ isOkSaldo: true, }) .execute(); - await NewMail({ - template: { - mailType: "pagamentoConferma", - }, - mail: { - subject: "Pagamento Confermato - Saldo Infoalloggi.it", - to: utente.email, - attachments: [ - { - filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64(ordine.ordine_id), - encoding: "base64", - contentType: "application/pdf", + await addEventToQueue({ + data: { + tipo: "email", + data: { + template: { + mailType: "pagamentoConferma", }, - ], + mail: { + subject: "Pagamento Confermato - Saldo Infoalloggi.it", + to: utente.email, + attachments: [ + { + filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, + content: await genPdfRicevutaCortesiaBase64( + ordine.ordine_id, + ), + encoding: "base64", + contentType: "application/pdf", + }, + ], + }, + userId: utente.id, + }, }, - userId: utente.id, + lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute }); + //PAGAMENTO SALDO CONFERMATO break; case OrderTypeEnum.Consulenza: await trx @@ -234,44 +254,61 @@ export const whIntentSucceededHandler = async ({ isOkConsulenza: true, }) .execute(); - await NewMail({ - template: { - mailType: "pagamentoConferma", - }, - mail: { - subject: "Pagamento Confermato - Consulenza Infoalloggi.it", - to: utente.email, - attachments: [ - { - filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64(ordine.ordine_id), - encoding: "base64", - contentType: "application/pdf", + await addEventToQueue({ + data: { + tipo: "email", + data: { + template: { + mailType: "pagamentoConferma", }, - ], + mail: { + subject: "Pagamento Confermato - Consulenza Infoalloggi.it", + to: utente.email, + attachments: [ + { + filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, + content: await genPdfRicevutaCortesiaBase64( + ordine.ordine_id, + ), + encoding: "base64", + contentType: "application/pdf", + }, + ], + }, + userId: utente.id, + }, }, - userId: utente.id, + lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute }); + //CONSULENZA ATTIVATA break; case OrderTypeEnum.Altro: // TODO: Handle other types - await NewMail({ - template: { - mailType: "pagamentoConferma", - }, - mail: { - subject: "Pagamento Confermato - Infoalloggi.it", - to: utente.email, - attachments: [ - { - filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, - content: await genPdfRicevutaCortesiaBase64(ordine.ordine_id), - encoding: "base64", - contentType: "application/pdf", + await addEventToQueue({ + data: { + tipo: "email", + data: { + template: { + mailType: "pagamentoConferma", }, - ], + mail: { + subject: "Pagamento Confermato - Infoalloggi.it", + to: utente.email, + attachments: [ + { + filename: `ricevuta_cortesia_${ordine.ordine_id}.pdf`, + content: await genPdfRicevutaCortesiaBase64( + ordine.ordine_id, + ), + encoding: "base64", + contentType: "application/pdf", + }, + ], + }, + userId: utente.id, + }, }, - userId: utente.id, + lock_expires: new Date(Date.now() + 60 * 1000), // Lock for 1 minute }); break; }