From 63618405bcd8fd5c44ddc87a3df56252d016f601 Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Mon, 12 Feb 2024 22:37:16 +0000 Subject: [PATCH] Added an email notification when the ticket is submitted --- .../ticketStatusCompleted.handlebars | 35 +++++++++++++++++++ src/pages/api/tickets/[id].ts | 34 ++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/email/templates/ticketStatusCompleted.handlebars diff --git a/src/email/templates/ticketStatusCompleted.handlebars b/src/email/templates/ticketStatusCompleted.handlebars new file mode 100644 index 00000000..8b53d5e2 --- /dev/null +++ b/src/email/templates/ticketStatusCompleted.handlebars @@ -0,0 +1,35 @@ + + + + + + + + +
+ +
+ Your ticket has been completed! +
+ Here is the ticket's information: +
+
+ ID: {{id}}
+ Subject: {{subject}}
+ Reporter: {{reporter.name}} - {{reporter.email}}
+ Date: {{date}}
+ Type: {{type}}
+ Page: {{reportedFrom}} +
+
+ Description: {{description}}
+
+
+
+
+ Thanks,
Your EnCoach team
+
+
+ + diff --git a/src/pages/api/tickets/[id].ts b/src/pages/api/tickets/[id].ts index 223ae689..29e35e45 100644 --- a/src/pages/api/tickets/[id].ts +++ b/src/pages/api/tickets/[id].ts @@ -10,7 +10,9 @@ import { } from "firebase/firestore"; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; -import { Ticket } from "@/interfaces/ticket"; +import { Ticket, TicketTypeLabel, TicketStatusLabel } from "@/interfaces/ticket"; +import moment from "moment"; +import { sendEmail } from "@/email"; const db = getFirestore(app); @@ -69,12 +71,38 @@ async function patch(req: NextApiRequest, res: NextApiResponse) { } const { id } = req.query as { id: string }; + const body = req.body as Ticket; + const snapshot = await getDoc(doc(db, "tickets", id)); const user = req.session.user; if (user.type === "admin" || user.type === "developer") { - await setDoc(snapshot.ref, req.body, { merge: true }); - return res.status(200).json({ ok: true }); + const data = snapshot.data() as Ticket; + await setDoc(snapshot.ref, body, { merge: true }); + try { + // send email if the status actually changed to completed + if(data.status !== req.body.status && req.body.status === 'completed') { + await sendEmail( + "ticketStatusCompleted", + { + id, + subject: body.subject, + reporter: body.reporter, + date: moment(body.date).format("DD/MM/YYYY - HH:mm"), + type: TicketTypeLabel[body.type], + reportedFrom: body.reportedFrom, + description: body.description, + }, + [data.reporter.email], + `Ticket ${id}: ${data.subject}`, + ); + } + } catch(err) { + console.error(err); + // doesnt matter if the email fails + } + res.status(200).json({ ok: true }); + return; } res.status(403).json({ ok: false });