Skip to content

Commit

Permalink
Added means to take in complex html templates
Browse files Browse the repository at this point in the history
  • Loading branch information
subru-37 committed Oct 26, 2024
1 parent 78a2f16 commit 8e53461
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
4 changes: 4 additions & 0 deletions apps/core-mailer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 32 additions & 9 deletions apps/core-mailer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 = {
Expand All @@ -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' });
Expand Down
16 changes: 8 additions & 8 deletions apps/core-mailer/src/models/Mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8e53461

Please sign in to comment.