Skip to content

Commit 3b55a3e

Browse files
committed
refactor: code refactor for notifcaiton service and etc..
1 parent a95e3ff commit 3b55a3e

File tree

4 files changed

+186
-90
lines changed

4 files changed

+186
-90
lines changed

src/graphql/comment.ts

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import checkUnscore from '../etc/checkUnscore';
1616
import { purgePost } from '../lib/graphcdn';
1717
import {
1818
CommentNotificationActionInput,
19+
CommentReplyNotificationActionInput,
1920
notificationService,
2021
} from '../services/notificationService';
2122
import db from '../lib/db';
@@ -164,7 +165,9 @@ export const resolvers: IResolvers<any, ApolloContext> = {
164165
}
165166

166167
if (comment_id) {
167-
const commentTarget = await commentRepo.findOne(comment_id);
168+
const commentTarget = await commentRepo.findOne(comment_id, {
169+
relations: ['user'],
170+
});
168171
if (!commentTarget) {
169172
throw new ApolloError('Target comment is not found', 'NOT_FOUND');
170173
}
@@ -287,45 +290,60 @@ export const resolvers: IResolvers<any, ApolloContext> = {
287290
await purgePost(post.id);
288291
} catch (e) {}
289292

290-
// create notification
293+
// create comment notification
291294
if (post.user.id !== ctx.user_id) {
292-
const notification = await notificationService.findByUniqueKey({
293-
fkUserId: post.user.id,
294-
actionId: comment.id,
295-
actorId: ctx.user_id,
296-
type: 'comment',
297-
});
298-
299-
if (notification) {
300-
await db.notification.update({
301-
where: {
302-
id: notification.id,
303-
},
304-
data: {
305-
is_deleted: false,
295+
try {
296+
await notificationService.createNotification({
297+
type: 'comment',
298+
fk_user_id: post.user.id,
299+
action_id: comment.id,
300+
actor_id: ctx.user_id,
301+
action: {
302+
comment: {
303+
actor_display_name: user.profile.display_name,
304+
actor_thumbnail: user.profile.thumbnail || '',
305+
actor_username: user.username,
306+
comment_id: comment.id,
307+
comment_text: comment.text,
308+
post_id: post.id,
309+
post_title: post.title,
310+
post_url_slug: post.url_slug,
311+
post_writer_username: post.user.username,
312+
type: 'comment',
313+
},
306314
},
315+
cookies: ctx.cookies,
307316
});
317+
} catch (error) {
318+
console.log('err', error);
308319
}
320+
}
321+
322+
// create comment reply notification
323+
if (comment_id) {
324+
const commentTarget = await commentRepo.findOne(comment_id, {
325+
relations: ['user'],
326+
});
309327

310-
if (!notification) {
328+
if (commentTarget && commentTarget?.user.id !== ctx.user_id) {
311329
try {
312330
await notificationService.createNotification({
313-
type: 'comment',
314-
fk_user_id: post.user.id,
331+
type: 'commentReply',
332+
fk_user_id: commentTarget.user.id,
315333
action_id: comment.id,
316334
actor_id: ctx.user_id,
317335
action: {
318-
comment: {
336+
commentReply: {
337+
type: 'commentReply',
338+
parent_comment_text: commentTarget.text,
339+
reply_comment_text: comment.text,
319340
actor_display_name: user.profile.display_name,
320341
actor_thumbnail: user.profile.thumbnail || '',
321342
actor_username: user.username,
322343
comment_id: comment.id,
323-
comment_text: comment.text,
324344
post_id: post.id,
325-
post_title: post.title,
326345
post_url_slug: post.url_slug,
327346
post_writer_username: post.user.username,
328-
type: 'comment',
329347
},
330348
},
331349
cookies: ctx.cookies,
@@ -382,17 +400,37 @@ export const resolvers: IResolvers<any, ApolloContext> = {
382400
await purgePost(post.id);
383401
} catch (e) {}
384402

385-
const notification = await notificationService.findByUniqueKey({
403+
// remove notification
404+
const comemntNotification = await notificationService.findByUniqueKey({
386405
fkUserId: post.user.id,
387406
actionId: comment.id,
388407
actorId: ctx.user_id,
389408
type: 'comment',
390409
});
391410

392-
if (notification) {
411+
if (comemntNotification) {
412+
await db.notification.update({
413+
where: {
414+
id: comemntNotification.id,
415+
},
416+
data: {
417+
is_deleted: true,
418+
},
419+
});
420+
}
421+
422+
// remove notification
423+
const commentReplyNotificaiton = await notificationService.findByUniqueKey({
424+
fkUserId: post.user.id,
425+
actionId: comment.id,
426+
actorId: ctx.user_id,
427+
type: 'commentReply',
428+
});
429+
430+
if (commentReplyNotificaiton) {
393431
await db.notification.update({
394432
where: {
395-
id: notification.id,
433+
id: commentReplyNotificaiton.id,
396434
},
397435
data: {
398436
is_deleted: true,
@@ -425,22 +463,45 @@ export const resolvers: IResolvers<any, ApolloContext> = {
425463

426464
// update notification
427465
if (post) {
428-
const notification = await notificationService.findByUniqueKey({
466+
const commentNotification = await notificationService.findByUniqueKey({
429467
fkUserId: post.user.id,
430468
actionId: comment.id,
431469
actorId: ctx.user_id,
432470
type: 'comment',
433471
});
434472

435-
if (notification) {
473+
if (commentNotification) {
436474
const action: CommentNotificationActionInput = {
437-
...(notification.action as CommentNotificationActionInput),
475+
...(commentNotification.action as CommentNotificationActionInput),
438476
comment_text: text,
439477
};
440478

441479
await db.notification.update({
442480
where: {
443-
id: notification.id,
481+
id: commentNotification.id,
482+
},
483+
data: {
484+
action,
485+
},
486+
});
487+
}
488+
489+
const commentReplyNotification = await notificationService.findByUniqueKey({
490+
fkUserId: post.user.id,
491+
actionId: comment.id,
492+
actorId: ctx.user_id,
493+
type: 'commentReply',
494+
});
495+
496+
if (commentReplyNotification) {
497+
const action: CommentReplyNotificationActionInput = {
498+
...(commentReplyNotification.action as CommentReplyNotificationActionInput),
499+
reply_comment_text: text,
500+
};
501+
502+
await db.notification.update({
503+
where: {
504+
id: commentReplyNotification.id,
444505
},
445506
data: {
446507
action,

src/services/notificationService.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ export const notificationService = {
9797
},
9898
notificationActionValidate(type: NotificationType, action: any): boolean {
9999
const schema = {
100-
follower: Joi.object().keys({
101-
follower_id: Joi.string().uuid().required(),
102-
follower_user_id: Joi.string().uuid().required(),
103-
actor_display_name: Joi.string().required(),
104-
actor_username: Joi.string().required(),
105-
actor_thumbnail: Joi.string().required().allow(''),
106-
type: Joi.string().valid('follower').required(),
107-
}),
108100
comment: Joi.object().keys({
109101
comment_id: Joi.string().uuid().required(),
110102
post_id: Joi.string().uuid().required(),
@@ -114,25 +106,29 @@ export const notificationService = {
114106
comment_text: Joi.string().required(),
115107
actor_display_name: Joi.string().required(),
116108
actor_username: Joi.string().required(),
117-
actor_thumbnail: Joi.string().required(),
109+
actor_thumbnail: Joi.string().allow('').required(),
118110
type: Joi.string().valid('comment').required(),
119111
}),
120-
postLike: Joi.object().keys({
121-
post_like_id: Joi.string().uuid().required(),
112+
commentReply: Joi.object().keys({
113+
comment_id: Joi.string().uuid().required(),
114+
parent_comment_text: Joi.string().required(),
122115
post_id: Joi.string().uuid().required(),
123-
post_title: Joi.string().required(),
124116
post_url_slug: Joi.string().required(),
125117
post_writer_username: Joi.string().required(),
118+
reply_comment_text: Joi.string().required(),
126119
actor_display_name: Joi.string().required(),
127120
actor_username: Joi.string().required(),
128-
actor_thumbnail: Joi.string().required(),
129-
type: Joi.string().valid('postLike').required(),
121+
actor_thumbnail: Joi.string().allow('').required(),
122+
type: Joi.string().valid('commentReply').required(),
130123
}),
131124
};
132125

133-
const validation = Joi.validate(action, schema[type]);
126+
const validation = Joi.validate(action[type], schema[type]);
134127

135-
if (validation.error) return false;
128+
if (validation.error) {
129+
console.log('notification validation error', validation.error);
130+
return false;
131+
}
136132
return true;
137133
},
138134
};
@@ -148,19 +144,20 @@ type NotificationCountResponse = {
148144
notificationCount: number;
149145
};
150146

151-
export type NotificationType = 'comment' | 'postLike' | 'follower';
147+
type NotificationActionInput = {
148+
comment?: CommentNotificationActionInput;
149+
commentReply?: CommentReplyNotificationActionInput;
150+
};
151+
152+
export type NotificationType = 'comment' | 'commentReply';
152153

153154
export type CreateNotificationArgs = {
154155
type: NotificationType;
155156
fk_user_id: string;
156157
actor_id?: string;
157158
action_id?: string;
158159
cookies: Cookies;
159-
action: {
160-
comment?: CommentNotificationActionInput;
161-
follower?: FollowerNotificationActionInput;
162-
postLike?: PostLikeNotificationActionInput;
163-
};
160+
action: NotificationActionInput;
164161
};
165162

166163
export type CommentNotificationActionInput = {
@@ -176,7 +173,20 @@ export type CommentNotificationActionInput = {
176173
type: 'comment';
177174
};
178175

179-
export type FollowerNotificationActionInput = {
176+
export type CommentReplyNotificationActionInput = {
177+
comment_id: string;
178+
parent_comment_text: string;
179+
post_id: string;
180+
post_url_slug: string;
181+
post_writer_username: string;
182+
reply_comment_text: string;
183+
actor_display_name: string;
184+
actor_username: string;
185+
actor_thumbnail: string;
186+
type: 'commentReply';
187+
};
188+
189+
export type FollowNotificationActionInput = {
180190
actor_display_name: string;
181191
actor_thumbnail: string;
182192
actor_user_id: string;

0 commit comments

Comments
 (0)