From 8e53461a70d757fc4626e568c7f559949f4359fe Mon Sep 17 00:00:00 2001 From: Subramani E Date: Sat, 26 Oct 2024 13:50:34 +0530 Subject: [PATCH] Added means to take in complex html templates --- apps/core-mailer/package.json | 4 +++ apps/core-mailer/src/index.ts | 41 ++++++++++++++++++++++------- apps/core-mailer/src/models/Mail.ts | 16 +++++------ 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/apps/core-mailer/package.json b/apps/core-mailer/package.json index 78ccc779..a28db18f 100644 --- a/apps/core-mailer/package.json +++ b/apps/core-mailer/package.json @@ -20,13 +20,17 @@ "database": "workspace:eventsync-core-mailer-database", "dotenv": "^16.0.3", "express": "^4.18.2", + "formidable": "^3.5.2", "ioredis": "^5.4.1", + "multer": "1.4.5-lts.1", "nodemailer": "^6.9.9", "uuid": "^9.0.0" }, "devDependencies": { "@types/amqplib": "^0.10.5", "@types/express": "^4.17.17", + "@types/formidable": "^3.4.5", + "@types/multer": "^1.4.12", "@types/node": "^20.10.5", "@types/nodemailer": "^6.4.14", "@types/pg": "^8.10.1", diff --git a/apps/core-mailer/src/index.ts b/apps/core-mailer/src/index.ts index 536306c0..a8a46b22 100644 --- a/apps/core-mailer/src/index.ts +++ b/apps/core-mailer/src/index.ts @@ -6,8 +6,9 @@ import express, { Express, Request, Response } from 'express'; import { fetchEmailJobStatus, sendEmailController } from './controllers/MailController'; import { MailService } from './services/MailService'; import { authorize } from './middlewares/auth'; +import formidable from 'formidable'; -const bodyParser = require('body-parser'); +// const bodyParser = require('body-parser'); const cors = require('cors'); dotenv.config(); @@ -18,7 +19,7 @@ const app: Express = express(); app.use(cors({ origin: '*' })); -app.use(bodyParser.json()); +// app.use(bodyParser.json()); app.get('/health', (req: Request, res: Response) => { const healthcheck: any = { @@ -39,16 +40,38 @@ app.get('/health', (req: Request, res: Response) => { // Add email to queue app.post('/mail', authorize, async (req: Request, res: Response) => { try { - const { name, to, subject, text, html } = req.body; + // const { name, to, subject, text, html } = req.body; + const form = formidable({ multiples: true }); + form.parse(req, async (err, fields, files) => { + if (err) { + console.error('Form parsing error:', err); + return res.status(500).send({ message: 'Error parsing form data' }); + } + + // Process fields and files as needed + const { name, to, subject, text, html } = fields; + console.log(html); + if (!name || !to || !subject || !text || !html) { + return res.status(400).send({ message: 'Missing required fields' }); + } + + const jobId: UUID = await sendEmailController(name, to, subject, text, html); + + return res.status(200).send({ + jobId: jobId, + }); + }); + // if (!name || !to || !subject || !text || !html) { + // console.log(req.body); - if (!name || !to || !subject || !text || !html) - return res.status(400).send({ message: 'Missing required fields' }); + // return res.status(400).send({ message: 'Missing required fields' }); + // } - const jobId: UUID = await sendEmailController(name, to, subject, text, html); + // const jobId: UUID = await sendEmailController(name, to, subject, text, html); - return res.status(200).send({ - jobId: jobId, - }); + // return res.status(200).send({ + // jobId: jobId, + // }); } catch (error) { console.error(error); return res.status(500).send({ message: 'Internal Server Error' }); diff --git a/apps/core-mailer/src/models/Mail.ts b/apps/core-mailer/src/models/Mail.ts index 155eccc9..af5f60b4 100644 --- a/apps/core-mailer/src/models/Mail.ts +++ b/apps/core-mailer/src/models/Mail.ts @@ -29,20 +29,20 @@ export class Mail { toString(): string { return `{ - jobId: "${this.jobId}", - from: "${this.from}", - name: "${this.name}", - to: "${this.to}", - subject: "${this.subject}", - text: "${this.text}", - html: "${this.html}" + "jobId": "${this.jobId}", + "from": "${this.from}", + "name": "${this.name}", + "to": "${this.to}", + "subject": "${this.subject}", + "text": "${this.text}", + "html": "${this.html}" }`; } } export function parseMail(str: string): Mail { console.log(str); - const parsed = JSON.parse(str.replace(/(\w+):/g, '"$1":')); + const parsed = JSON.parse(str); return new Mail( parsed.from, parsed.name,