From 583104f726ecf5277f3bd7e40c9067d2e3f65077 Mon Sep 17 00:00:00 2001 From: Jack Andrews Date: Sun, 27 Oct 2024 00:39:04 +0100 Subject: [PATCH] fix: comment notification --- apps/api/src/controllers/ticket.ts | 7 ++-- apps/api/src/lib/notifications.ts | 32 ------------------- .../src/lib/notifications/issue/assigned.ts | 18 +++++++++++ .../src/lib/notifications/issue/comment.ts | 24 ++++++++++++++ .../api/src/lib/notifications/issue/status.ts | 1 + apps/client/package.json | 2 ++ yarn.lock | 4 ++- 7 files changed, 53 insertions(+), 35 deletions(-) delete mode 100644 apps/api/src/lib/notifications.ts create mode 100644 apps/api/src/lib/notifications/issue/assigned.ts create mode 100644 apps/api/src/lib/notifications/issue/comment.ts create mode 100644 apps/api/src/lib/notifications/issue/status.ts diff --git a/apps/api/src/controllers/ticket.ts b/apps/api/src/controllers/ticket.ts index ca66e19bb..ac137a02e 100644 --- a/apps/api/src/controllers/ticket.ts +++ b/apps/api/src/controllers/ticket.ts @@ -9,9 +9,10 @@ import { sendAssignedEmail } from "../lib/nodemailer/ticket/assigned"; import { sendComment } from "../lib/nodemailer/ticket/comment"; import { sendTicketCreate } from "../lib/nodemailer/ticket/create"; import { sendTicketStatus } from "../lib/nodemailer/ticket/status"; -import { createNotification } from "../lib/notifications"; import { checkSession } from "../lib/session"; import { prisma } from "../prisma"; +import { assignedNotification } from "../lib/notifications/issue/assigned"; +import { commentNotification } from "../lib/notifications/issue/comment"; const validateEmail = (email: string) => { return String(email) @@ -83,7 +84,7 @@ export function ticketRoutes(fastify: FastifyInstance) { await sendAssignedEmail(assgined!.email); - await createNotification("ticket_assigned", engineer.id, ticket); + await assignedNotification(engineer.id, ticket); } const webhook = await prisma.webhooks.findMany({ @@ -542,6 +543,8 @@ export function ticketRoutes(fastify: FastifyInstance) { sendComment(text, title, email); } + await commentNotification(user!.id, ticket, user!.name); + const hog = track(); hog.capture({ diff --git a/apps/api/src/lib/notifications.ts b/apps/api/src/lib/notifications.ts deleted file mode 100644 index 1ff6110c9..000000000 --- a/apps/api/src/lib/notifications.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { prisma } from "../prisma"; - -export async function createNotification( - type: string, - userId: string, - ticket: any -) { - try { - let text = ""; - - console.log("ticket", ticket); - - switch (type) { - case "ticket_assigned": - text = `Assigned Ticket #${ticket.Number}`; - break; - case "ticket_comment": - text = `New comment on #${ticket.Number}`; - break; - } - - await prisma.notifications.create({ - data: { - text, - userId, - ticketId: ticket.id, - }, - }); - } catch (error) { - console.log(error); - } -} diff --git a/apps/api/src/lib/notifications/issue/assigned.ts b/apps/api/src/lib/notifications/issue/assigned.ts new file mode 100644 index 000000000..7f51c3662 --- /dev/null +++ b/apps/api/src/lib/notifications/issue/assigned.ts @@ -0,0 +1,18 @@ +import { prisma } from "../../../prisma"; + +export async function assignedNotification( + userId: string, + ticket: any +) { + try { + return await prisma.notifications.create({ + data: { + text: `Assigned Ticket #${ticket.Number}`, + userId, + ticketId: ticket.id, + }, + }); + } catch (error) { + console.error("Error creating notification:", error); + } +} diff --git a/apps/api/src/lib/notifications/issue/comment.ts b/apps/api/src/lib/notifications/issue/comment.ts new file mode 100644 index 000000000..4266f9654 --- /dev/null +++ b/apps/api/src/lib/notifications/issue/comment.ts @@ -0,0 +1,24 @@ +import { prisma } from "../../../prisma"; + +/** + * Creates a new comment notification. + * + * @param {string} userId - The ID of the user to notify. + * @param {object} ticket - The ticket object related to the comment. + * @param {string} comment - The content of the comment. + * @returns {Promise} + */ +export async function commentNotification(userId: string, ticket: any, user: string) { + try { + const text = `New comment on #${ticket.Number} by ${user}`; + return await prisma.notifications.create({ + data: { + text, + userId, + ticketId: ticket.id, + }, + }); + } catch (error) { + console.error("Error creating comment notification:", error); + } +} diff --git a/apps/api/src/lib/notifications/issue/status.ts b/apps/api/src/lib/notifications/issue/status.ts new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/apps/api/src/lib/notifications/issue/status.ts @@ -0,0 +1 @@ + diff --git a/apps/client/package.json b/apps/client/package.json index 42daa8a9a..b945b1998 100644 --- a/apps/client/package.json +++ b/apps/client/package.json @@ -46,6 +46,7 @@ "next-translate": "^1.3.4", "posthog-js": "1.93.2", "prismjs": "^1.29.0", + "prop-types": "^15.8.1", "react": "^18.3.1", "react-dom": "^18.3.1", "react-frame-component": "^5.2.6", @@ -63,6 +64,7 @@ "@types/next": "^9.0.0", "@types/node": "17.0.4", "@types/prismjs": "^1", + "@types/prop-types": "^15", "@types/react": "18.2.38", "@types/react-table": "^7.7.15", "autoprefixer": "^10.4.0", diff --git a/yarn.lock b/yarn.lock index 556fee1a3..1769795ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4363,7 +4363,7 @@ __metadata: languageName: node linkType: hard -"@types/prop-types@npm:*": +"@types/prop-types@npm:*, @types/prop-types@npm:^15": version: 15.7.13 resolution: "@types/prop-types@npm:15.7.13" checksum: 10c0/1b20fc67281902c6743379960247bc161f3f0406ffc0df8e7058745a85ea1538612109db0406290512947f9632fe9e10e7337bf0ce6338a91d6c948df16a7c61 @@ -5741,6 +5741,7 @@ __metadata: "@types/next": "npm:^9.0.0" "@types/node": "npm:17.0.4" "@types/prismjs": "npm:^1" + "@types/prop-types": "npm:^15" "@types/react": "npm:18.2.38" "@types/react-table": "npm:^7.7.15" autoprefixer: "npm:^10.4.0" @@ -5760,6 +5761,7 @@ __metadata: postcss: "npm:^8.4.5" posthog-js: "npm:1.93.2" prismjs: "npm:^1.29.0" + prop-types: "npm:^15.8.1" react: "npm:^18.3.1" react-dom: "npm:^18.3.1" react-frame-component: "npm:^5.2.6"