feat: remove defer (#243)

* working on moving the email logic to the backend

* Moved nodemailer to the backend

* uninstall defer

* updated relative import path in tsconfig

* working on the zod validation

* Merge branch 'main' into send-email

* fixed the zod import issue

* fixed the zod import issue

* updated the env example files

* docs: update README.md (#249)

* fix: handle catex error

* chore: remove fly.io github action

* Update README.md

Updated the Readme to reflect changes made to the emailing flow

---------

Co-authored-by: Rotimi Best <[email protected]>

* chore: wrapp up email sending

---------

Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
Em_Dev
2024-04-12 04:57:19 +01:00
committed by GitHub
co-authored by Rotimi Best
parent 75ad73b97f
commit 3fe15e6633
27 changed files with 523 additions and 215 deletions
+59
View File
@@ -0,0 +1,59 @@
const express = require('express');
const zod = require('zod');
const { getTransporter } = require('../utils/getTransporter');
const { withEmailTemplate } = require('../utils/withEmailTemplate');
const router = express.Router();
router.post('/', async (req, res) => {
try {
const mySchema = zod.array(
zod.object({
from: zod.string().optional(),
to: zod.string(),
subject: zod.string(),
content: zod.string(),
isPersonalEmail: zod.boolean().optional(),
replyTo: zod.string().optional()
})
);
mySchema.parse(req.body);
const emailDataArray = req.body;
Promise.all(
emailDataArray.map(async (emailData) => {
try {
const { from, to, subject, content, isPersonalEmail, replyTo } = emailData;
const transporter = await getTransporter(isPersonalEmail);
if (!transporter) {
return;
}
const res = await transporter.sendMail({
from: from || '"Best from ClassroomIO" <[email protected]>',
to,
subject,
replyTo,
html: withEmailTemplate(content)
});
console.log('Email status', res);
} catch (error) {
console.error('Error sending email', error);
}
})
);
console.log('Email sent');
return res.json({ success: true });
} catch (error) {
console.error('Error sending emails:', error);
return res.status(500).json({ error: 'Internal Server Error' });
}
});
module.exports = router;