Skip to content

Commit

Permalink
fix: improve on notifications (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
iChris-tian authored Dec 6, 2024
1 parent 11bb4ba commit 3b0e5f4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 47 deletions.
5 changes: 4 additions & 1 deletion src/models/applicantNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/resolvers/applicantNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
15 changes: 10 additions & 5 deletions src/resolvers/applicationStageResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export const applicationStageResolvers: any = {
const notification = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
eventType: "general",
eventType: "applicationUpdate",
});

await sendEmailTemplate(
Expand All @@ -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);
Expand All @@ -355,7 +356,7 @@ export const applicationStageResolvers: any = {
const notification1 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
eventType: "general",
eventType: "applicationUpdate",
});

await sendEmailTemplate(
Expand All @@ -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);
Expand Down Expand Up @@ -440,7 +442,7 @@ export const applicationStageResolvers: any = {
const notification2 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
eventType: "general",
eventType: "applicationUpdate",
});

await sendEmailTemplate(
Expand All @@ -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);
Expand Down Expand Up @@ -499,7 +502,7 @@ export const applicationStageResolvers: any = {
const notification3 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
eventType: "general",
eventType: "applicationUpdate",
});

await sendEmailTemplate(
Expand All @@ -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);
Expand Down Expand Up @@ -547,7 +551,7 @@ export const applicationStageResolvers: any = {
const notification4 = await ApplicantNotificationsModel.create({
userId: user!._id,
message,
eventType: "general",
eventType: "applicationUpdate",
});

await sendEmailTemplate(
Expand All @@ -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);
Expand Down
48 changes: 8 additions & 40 deletions src/resolvers/ticketResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -173,29 +155,11 @@ export const ticketResolver = {
<br/><br/>Thank you for your patience.<br/>`
);

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);
Expand Down Expand Up @@ -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,
})
Expand All @@ -280,4 +248,4 @@ export const ticketResolver = {
}
}
},
}
}
2 changes: 2 additions & 0 deletions src/schema/applicantNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const Schema = gql`
message: String!
read: Boolean!
createdAt: String!
eventType: String!
eventId: String
}
type Query {
Expand Down

0 comments on commit 3b0e5f4

Please sign in to comment.