Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
[Update] 알림 응답값을 수정하라 (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
runasy-koonta authored Feb 18, 2024
2 parents 0ca5d59 + a7c68e5 commit d957410
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
20 changes: 19 additions & 1 deletion src/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { CreateCommentDto } from './dto/create-comment.dto';
import { CommentEntity } from './domain/comment.entity';
import {RuleMainEntity} from "../rule/domain/rule.main.entity";
import {UserEntity} from "../user/user.entity";
import { NotificationEntity } from '../notification/notification.entity';
import { NotificationService } from '../notification/notification.service';

@Injectable()
export class CommentService {
Expand All @@ -13,7 +15,7 @@ export class CommentService {
const comment = new CommentEntity();

const user = await UserEntity.findOneOrFail({ where: { id: userId } });
const rule = await RuleMainEntity.findOneOrFail({ where: { id: ruleId } });
const rule = await RuleMainEntity.findOneOrFail({ where: { id: ruleId }, relations: { invitations: { member: true } } });

if(!user || !rule){
throw new Error('Data not found');
Expand All @@ -25,6 +27,22 @@ export class CommentService {
comment.rule = rule;
comment.content = dto.content;
await comment.save();

// 댓글 알림
for (const invitation of rule.invitations) {
const receiver = invitation.member;
if (receiver.id === user.id) {
continue;
}

const notification = new NotificationEntity();
notification.notificationReceiver = invitation.member;
notification.notificationSender = user;
notification.notificationTargetType = 'RULE';
notification.notificationTargetId = rule.id;
notification.notificationAction = 'COMMENT';
await notification.save();
}
}
return comment.id;
}
Expand Down
14 changes: 9 additions & 5 deletions src/notification/notification.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ export class NotificationEntity extends BaseEntity {
@ManyToOne(() => UserEntity)
notificationReceiver: UserEntity;

@Column({ type: 'enum', enum: ['LIKE', 'COMMENT'] })
notificationType: 'LIKE' | 'COMMENT';
@JoinColumn()
@ManyToOne(() => UserEntity)
notificationSender: UserEntity;

@Column()
notificationContent: string;
@Column({ type: 'enum', enum: ['SIGNATURE', 'RULE'] })
notificationTargetType: 'SIGNATURE' | 'RULE';

@Column()
notificationItemId: number;
notificationTargetId: number;

@Column({ type: 'enum', enum: ['LIKE', 'COMMENT'] })
notificationAction: 'LIKE' | 'COMMENT';

@Column({ default: false })
notificationRead: boolean;
Expand Down
18 changes: 9 additions & 9 deletions src/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import { ResponseCode } from '../response/response-code.enum';
export class NotificationService {
private readonly logger = new Logger(NotificationService.name);

static createNotificationContent(type: 'LIKE' | 'COMMENT', nickname: string) {
return `${nickname}님이 내 시그니처에 ${
type === 'LIKE' ? '좋아요' : '댓글'
}을 남겼습니다.`;
}

async listNotifications(userId: number) {
try {
const notifications = await NotificationEntity.find({
Expand All @@ -21,6 +15,9 @@ export class NotificationService {
id: userId,
},
},
relations: {
notificationSender: true,
},
order: {
created: 'DESC',
},
Expand All @@ -45,9 +42,12 @@ export class NotificationService {
'알림 조회 성공',
notifications.map((notification) => ({
id: notification.id,
type: notification.notificationType,
content: notification.notificationContent,
itemId: notification.notificationItemId,
content: {
actionUserNickname: notification.notificationSender.nickname,
type: notification.notificationTargetType,
action: notification.notificationAction,
},
itemId: notification.notificationTargetId,
isRead: notification.notificationRead,
created: notification.created,
})),
Expand Down
13 changes: 13 additions & 0 deletions src/signature/signature.comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { GetSignatureCommentDto } from './dto/comment/get-signature-comment.dto'
import { GetCommentWriterDto } from './dto/comment/get-comment-writer.dto';
import { CursorPageMetaDto } from '../mate/cursor-page/cursor-page.meta.dto';
import { CursorPageDto } from '../mate/cursor-page/cursor-page.dto';
import { NotificationEntity } from '../notification/notification.entity';

@Injectable()
export class SignatureCommentService{
Expand Down Expand Up @@ -45,6 +46,9 @@ export class SignatureCommentService{
comment.signature = signature;
comment.content = createCommentDto.content;

// 알림 생성
const notification = new NotificationEntity();

// parentCommentId가 존재할 경우 -> 답글 / 존재하지 않을 경우 -> 댓글
if(parentCommentId){ // 대댓글: parentId는 파라미터로 받은 parentCommentId로 설정

Expand All @@ -58,13 +62,22 @@ export class SignatureCommentService{
await comment.save();
}

notification.notificationReceiver = parentComment.user;
}
else{ // 댓글: parentId는 본인으로 설정
const savedComment = await comment.save();
savedComment.parentComment = savedComment;
await savedComment.save();

notification.notificationReceiver = signature.user;
}

notification.notificationSender = user;
notification.notificationTargetType = 'SIGNATURE';
notification.notificationTargetId = signature.id;
notification.notificationAction = 'COMMENT';
await notification.save();

return comment.id;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/signature/signature.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ export class SignatureService {
// Todo: 좋아요를 했다가 해제한 경우에 알림을 어떻게 처리할 것인가?
const notification = new NotificationEntity();
notification.notificationReceiver = signature.user;
notification.notificationType = 'LIKE';
notification.notificationContent =
NotificationService.createNotificationContent('LIKE', loginUser.nickname);
notification.notificationItemId = signature.id;
notification.notificationSender = loginUser;
notification.notificationTargetType = 'SIGNATURE';
notification.notificationTargetId = signature.id;
notification.notificationAction = 'LIKE';
await notification.save();

return signature;
Expand Down

0 comments on commit d957410

Please sign in to comment.