Skip to content

Commit 3b0e5f4

Browse files
authored
fix: improve on notifications (#175)
1 parent 11bb4ba commit 3b0e5f4

File tree

5 files changed

+26
-47
lines changed

5 files changed

+26
-47
lines changed

Diff for: src/models/applicantNotifications.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ const applicantNotificationsSchema = new Schema({
1212
},
1313
eventType: {
1414
type: String,
15-
enum: ["jobPost", "applicationUpdate", "general"],
15+
enum: ["ticket", "applicationUpdate", "general"],
1616
required: true,
1717
},
18+
eventId: {
19+
type: String,
20+
},
1821
read: {
1922
type: Boolean,
2023
default: false,

Diff for: src/resolvers/applicantNotifications.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ const notificationResolver: any = {
99
Mutation: {
1010
async createNotification(
1111
_parent: any,
12-
args: { userId: string; message: string; eventType: string }
12+
args: { userId: string; message: string; eventType: string; eventId: string }
1313
) {
1414
try {
1515
const newNotification = new ApplicantNotificationsModel({
1616
userId: args.userId,
1717
message: args.message,
1818
eventType: args.eventType,
19+
eventId: args.eventId
1920
});
2021
const savedNotification = await newNotification.save();
2122

Diff for: src/resolvers/applicationStageResolver.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export const applicationStageResolvers: any = {
308308
const notification = await ApplicantNotificationsModel.create({
309309
userId: user!._id,
310310
message,
311-
eventType: "general",
311+
eventType: "applicationUpdate",
312312
});
313313

314314
await sendEmailTemplate(
@@ -329,6 +329,7 @@ export const applicationStageResolvers: any = {
329329
id: notification._id,
330330
createdAt: notification.createdAt,
331331
read: notification.read,
332+
eventType: "applicationUpdate",
332333
})
333334
.catch((error) => {
334335
console.error("Error with Pusher trigger:", error);
@@ -355,7 +356,7 @@ export const applicationStageResolvers: any = {
355356
const notification1 = await ApplicantNotificationsModel.create({
356357
userId: user!._id,
357358
message,
358-
eventType: "general",
359+
eventType: "applicationUpdate",
359360
});
360361

361362
await sendEmailTemplate(
@@ -379,6 +380,7 @@ export const applicationStageResolvers: any = {
379380
id: notification1._id,
380381
createdAt: notification1.createdAt,
381382
read: notification1.read,
383+
eventType: "applicationUpdate",
382384
})
383385
.catch((error) => {
384386
console.error("Error with Pusher trigger:", error);
@@ -440,7 +442,7 @@ export const applicationStageResolvers: any = {
440442
const notification2 = await ApplicantNotificationsModel.create({
441443
userId: user!._id,
442444
message,
443-
eventType: "general",
445+
eventType: "applicationUpdate",
444446
});
445447

446448
await sendEmailTemplate(
@@ -464,6 +466,7 @@ export const applicationStageResolvers: any = {
464466
id: notification2._id,
465467
createdAt: notification2.createdAt,
466468
read: notification2.read,
469+
eventType: "applicationUpdate",
467470
})
468471
.catch((error) => {
469472
console.error("Error with Pusher trigger:", error);
@@ -499,7 +502,7 @@ export const applicationStageResolvers: any = {
499502
const notification3 = await ApplicantNotificationsModel.create({
500503
userId: user!._id,
501504
message,
502-
eventType: "general",
505+
eventType: "applicationUpdate",
503506
});
504507

505508
await sendEmailTemplate(
@@ -520,6 +523,7 @@ export const applicationStageResolvers: any = {
520523
id: notification3._id,
521524
createdAt: notification3.createdAt,
522525
read: notification3.read,
526+
eventType: "applicationUpdate",
523527
})
524528
.catch((error) => {
525529
console.error("Error with Pusher trigger:", error);
@@ -547,7 +551,7 @@ export const applicationStageResolvers: any = {
547551
const notification4 = await ApplicantNotificationsModel.create({
548552
userId: user!._id,
549553
message,
550-
eventType: "general",
554+
eventType: "applicationUpdate",
551555
});
552556

553557
await sendEmailTemplate(
@@ -568,6 +572,7 @@ export const applicationStageResolvers: any = {
568572
id: notification4._id,
569573
createdAt: notification4.createdAt,
570574
read: notification4.read,
575+
eventType: "applicationUpdate",
571576
})
572577
.catch((error) => {
573578
console.error("Error with Pusher trigger:", error);

Diff for: src/resolvers/ticketResolver.ts

+8-40
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ export const ticketResolver = {
8888
adminReplies: [],
8989
applicantReplies: []
9090
});
91-
92-
const message = `Your ticket "${args.title}" has been submitted successfully.`;
93-
const notification = await ApplicantNotificationsModel.create({
94-
userId: user._id,
95-
message,
96-
eventType: "general",
97-
});
9891

9992
await sendEmailTemplate(
10093
user.email,
@@ -114,17 +107,6 @@ export const ticketResolver = {
114107
"new_Ticket"
115108
);
116109

117-
await pusher
118-
.trigger(`notifications-${user._id}`, "new-notification", {
119-
message: notification.message,
120-
id: notification._id,
121-
createdAt: notification.createdAt,
122-
read: notification.read,
123-
})
124-
.catch((error) => {
125-
console.error("Error with Pusher trigger:", error);
126-
});
127-
128110
return newTicket.populate('author');
129111
} catch(error: any) {
130112
throw new CustomGraphQLError(error.message);
@@ -173,29 +155,11 @@ export const ticketResolver = {
173155
<br/><br/>Thank you for your patience.<br/>`
174156
);
175157

176-
const message = `Your ticket "${updatedTicket.title}" has been updated.`;
177-
const notification = await ApplicantNotificationsModel.create({
178-
userId: updatedTicket.author._id,
179-
message,
180-
eventType: "general",
181-
});
182-
183158
await publishNotification(
184-
`${user!.firstname} ${user.lastname} has sent a new ticket.`,
159+
`${user!.firstname} ${user.lastname} has sent a new reply to ticket ${updatedTicket.title}.`,
185160
"new_Ticket"
186161
);
187-
188-
await pusher
189-
.trigger(`notifications-${updatedTicket.author._id}`, "new-notification", {
190-
message: notification.message,
191-
id: notification._id,
192-
createdAt: notification.createdAt,
193-
read: notification.read,
194-
})
195-
.catch((error) => {
196-
console.error("Error with Pusher trigger:", error);
197-
});
198-
162+
199163
return updatedTicket;
200164
} catch(err: any){
201165
throw new CustomGraphQLError(err.message);
@@ -248,13 +212,17 @@ export const ticketResolver = {
248212
const notification = await ApplicantNotificationsModel.create({
249213
userId: resolvedTicket.author._id,
250214
message,
251-
eventType: "general",
215+
eventType: "ticket",
216+
eventId: resolvedTicket.id
217+
252218
});
253219

254220
await pusher
255221
.trigger(`notifications-${resolvedTicket.author._id}`, "new-notification", {
256222
message: notification.message,
257223
id: notification._id,
224+
eventType: notification.eventType,
225+
eventId: notification.eventId,
258226
createdAt: notification.createdAt,
259227
read: notification.read,
260228
})
@@ -280,4 +248,4 @@ export const ticketResolver = {
280248
}
281249
}
282250
},
283-
}
251+
}

Diff for: src/schema/applicantNotifications.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const Schema = gql`
77
message: String!
88
read: Boolean!
99
createdAt: String!
10+
eventType: String!
11+
eventId: String
1012
}
1113
1214
type Query {

0 commit comments

Comments
 (0)