From 3b0e5f427f651177b7ec64ab034a04853ca90b77 Mon Sep 17 00:00:00 2001
From: Christian Iradukunda <99505626+iChris-tian@users.noreply.github.com>
Date: Fri, 6 Dec 2024 12:55:57 +0200
Subject: [PATCH] fix: improve on notifications (#175)
---
src/models/applicantNotifications.ts | 5 ++-
src/resolvers/applicantNotifications.ts | 3 +-
src/resolvers/applicationStageResolver.ts | 15 ++++---
src/resolvers/ticketResolver.ts | 48 ++++-------------------
src/schema/applicantNotifications.ts | 2 +
5 files changed, 26 insertions(+), 47 deletions(-)
diff --git a/src/models/applicantNotifications.ts b/src/models/applicantNotifications.ts
index de228a5d..473f7d47 100644
--- a/src/models/applicantNotifications.ts
+++ b/src/models/applicantNotifications.ts
@@ -12,9 +12,12 @@ const applicantNotificationsSchema = new Schema({
},
eventType: {
type: String,
- enum: ["jobPost", "applicationUpdate", "general"],
+ enum: ["ticket", "applicationUpdate", "general"],
required: true,
},
+ eventId: {
+ type: String,
+ },
read: {
type: Boolean,
default: false,
diff --git a/src/resolvers/applicantNotifications.ts b/src/resolvers/applicantNotifications.ts
index 994ec880..b2f08c44 100644
--- a/src/resolvers/applicantNotifications.ts
+++ b/src/resolvers/applicantNotifications.ts
@@ -9,13 +9,14 @@ const notificationResolver: any = {
Mutation: {
async createNotification(
_parent: any,
- args: { userId: string; message: string; eventType: string }
+ args: { userId: string; message: string; eventType: string; eventId: string }
) {
try {
const newNotification = new ApplicantNotificationsModel({
userId: args.userId,
message: args.message,
eventType: args.eventType,
+ eventId: args.eventId
});
const savedNotification = await newNotification.save();
diff --git a/src/resolvers/applicationStageResolver.ts b/src/resolvers/applicationStageResolver.ts
index 029c4e16..c1b767c6 100644
--- a/src/resolvers/applicationStageResolver.ts
+++ b/src/resolvers/applicationStageResolver.ts
@@ -308,7 +308,7 @@ export const applicationStageResolvers: any = {
const notification = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
- eventType: "general",
+ eventType: "applicationUpdate",
});
await sendEmailTemplate(
@@ -329,6 +329,7 @@ export const applicationStageResolvers: any = {
id: notification._id,
createdAt: notification.createdAt,
read: notification.read,
+ eventType: "applicationUpdate",
})
.catch((error) => {
console.error("Error with Pusher trigger:", error);
@@ -355,7 +356,7 @@ export const applicationStageResolvers: any = {
const notification1 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
- eventType: "general",
+ eventType: "applicationUpdate",
});
await sendEmailTemplate(
@@ -379,6 +380,7 @@ export const applicationStageResolvers: any = {
id: notification1._id,
createdAt: notification1.createdAt,
read: notification1.read,
+ eventType: "applicationUpdate",
})
.catch((error) => {
console.error("Error with Pusher trigger:", error);
@@ -440,7 +442,7 @@ export const applicationStageResolvers: any = {
const notification2 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
- eventType: "general",
+ eventType: "applicationUpdate",
});
await sendEmailTemplate(
@@ -464,6 +466,7 @@ export const applicationStageResolvers: any = {
id: notification2._id,
createdAt: notification2.createdAt,
read: notification2.read,
+ eventType: "applicationUpdate",
})
.catch((error) => {
console.error("Error with Pusher trigger:", error);
@@ -499,7 +502,7 @@ export const applicationStageResolvers: any = {
const notification3 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
- eventType: "general",
+ eventType: "applicationUpdate",
});
await sendEmailTemplate(
@@ -520,6 +523,7 @@ export const applicationStageResolvers: any = {
id: notification3._id,
createdAt: notification3.createdAt,
read: notification3.read,
+ eventType: "applicationUpdate",
})
.catch((error) => {
console.error("Error with Pusher trigger:", error);
@@ -547,7 +551,7 @@ export const applicationStageResolvers: any = {
const notification4 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
- eventType: "general",
+ eventType: "applicationUpdate",
});
await sendEmailTemplate(
@@ -568,6 +572,7 @@ export const applicationStageResolvers: any = {
id: notification4._id,
createdAt: notification4.createdAt,
read: notification4.read,
+ eventType: "applicationUpdate",
})
.catch((error) => {
console.error("Error with Pusher trigger:", error);
diff --git a/src/resolvers/ticketResolver.ts b/src/resolvers/ticketResolver.ts
index 7bdb97ee..51c5990a 100644
--- a/src/resolvers/ticketResolver.ts
+++ b/src/resolvers/ticketResolver.ts
@@ -88,13 +88,6 @@ export const ticketResolver = {
adminReplies: [],
applicantReplies: []
});
-
- const message = `Your ticket "${args.title}" has been submitted successfully.`;
- const notification = await ApplicantNotificationsModel.create({
- userId: user._id,
- message,
- eventType: "general",
- });
await sendEmailTemplate(
user.email,
@@ -114,17 +107,6 @@ export const ticketResolver = {
"new_Ticket"
);
- await pusher
- .trigger(`notifications-${user._id}`, "new-notification", {
- message: notification.message,
- id: notification._id,
- createdAt: notification.createdAt,
- read: notification.read,
- })
- .catch((error) => {
- console.error("Error with Pusher trigger:", error);
- });
-
return newTicket.populate('author');
} catch(error: any) {
throw new CustomGraphQLError(error.message);
@@ -173,29 +155,11 @@ export const ticketResolver = {
Thank you for your patience.
`
);
- const message = `Your ticket "${updatedTicket.title}" has been updated.`;
- const notification = await ApplicantNotificationsModel.create({
- userId: updatedTicket.author._id,
- message,
- eventType: "general",
- });
-
await publishNotification(
- `${user!.firstname} ${user.lastname} has sent a new ticket.`,
+ `${user!.firstname} ${user.lastname} has sent a new reply to ticket ${updatedTicket.title}.`,
"new_Ticket"
);
-
- await pusher
- .trigger(`notifications-${updatedTicket.author._id}`, "new-notification", {
- message: notification.message,
- id: notification._id,
- createdAt: notification.createdAt,
- read: notification.read,
- })
- .catch((error) => {
- console.error("Error with Pusher trigger:", error);
- });
-
+
return updatedTicket;
} catch(err: any){
throw new CustomGraphQLError(err.message);
@@ -248,13 +212,17 @@ export const ticketResolver = {
const notification = await ApplicantNotificationsModel.create({
userId: resolvedTicket.author._id,
message,
- eventType: "general",
+ eventType: "ticket",
+ eventId: resolvedTicket.id
+
});
await pusher
.trigger(`notifications-${resolvedTicket.author._id}`, "new-notification", {
message: notification.message,
id: notification._id,
+ eventType: notification.eventType,
+ eventId: notification.eventId,
createdAt: notification.createdAt,
read: notification.read,
})
@@ -280,4 +248,4 @@ export const ticketResolver = {
}
}
},
-}
\ No newline at end of file
+}
diff --git a/src/schema/applicantNotifications.ts b/src/schema/applicantNotifications.ts
index 03a0795b..c17be8f4 100644
--- a/src/schema/applicantNotifications.ts
+++ b/src/schema/applicantNotifications.ts
@@ -7,6 +7,8 @@ const Schema = gql`
message: String!
read: Boolean!
createdAt: String!
+ eventType: String!
+ eventId: String
}
type Query {