From 077bf85ad8beb20021fd78eb6710e10d896e79a1 Mon Sep 17 00:00:00 2001 From: PxlLoewe <72106766+PxlLoewe@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:01:34 -0700 Subject: [PATCH] Added catch block to main route --- apps/hub-server/routes/mail.ts | 77 ++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/apps/hub-server/routes/mail.ts b/apps/hub-server/routes/mail.ts index 80badfd7..522e35c3 100644 --- a/apps/hub-server/routes/mail.ts +++ b/apps/hub-server/routes/mail.ts @@ -17,45 +17,50 @@ router.post("/send", async (req, res) => { }); router.post("/template/:template", async (req, res) => { - const { template } = req.params; - const { to, data } = req.body; - if (!to || !data) { - res.status(400).json({ error: "Missing required fields" }); + try { + const { template } = req.params; + const { to, data } = req.body; + if (!to || !data) { + res.status(400).json({ error: "Missing required fields" }); - return; - } - - if (!data.user) { - res.status(400).json({ error: "Missing user data" }); - return; - } - console.log("template", template); - switch (template) { - case "password-change": - if (!data.password) { - res.status(400).json({ error: "Missing password data" }); - return; - } - await sendPasswordChanged(to, data.user, data.password); - break; - case "course-completed": - if (!data.event) { - res.status(400).json({ error: "Missing event data" }); - return; - } - await sendCourseCompletedEmail(to, data.user, data.event); - break; - case "email-verification": - if (!data.code) { - res.status(400).json({ error: "Missing verification code" }); - return; - } - await sendEmailVerification(to, data.user, data.code); - default: - res.status(400).json({ error: "Invalid template" }); return; + } + + if (!data.user) { + res.status(400).json({ error: "Missing user data" }); + return; + } + console.log("template", template); + switch (template) { + case "password-change": + if (!data.password) { + res.status(400).json({ error: "Missing password data" }); + return; + } + await sendPasswordChanged(to, data.user, data.password); + break; + case "course-completed": + if (!data.event) { + res.status(400).json({ error: "Missing event data" }); + return; + } + await sendCourseCompletedEmail(to, data.user, data.event); + break; + case "email-verification": + if (!data.code) { + res.status(400).json({ error: "Missing verification code" }); + return; + } + await sendEmailVerification(to, data.user, data.code); + default: + res.status(400).json({ error: "Invalid template" }); + return; + } + res.status(200).json({ message: "Email sent successfully" }); + } catch (error) { + console.error("Error sending email:", error); + res.status(500).json({ error: "Failed to send email" }); } - res.status(200).json({ message: "Email sent successfully" }); }); export default router;