Skip to content

Commit

Permalink
feat: send notification when user commented to post
Browse files Browse the repository at this point in the history
  • Loading branch information
Seung-o committed Jan 24, 2024
1 parent 31e3903 commit 1422ea0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/notifications/dto/notification.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { NotificationType } from '../enums/notification-type.enum';
import { NotificationDataByTradePost } from '../types/notification-data.type';

export class CreateNotificationDTO<T extends NotificationDataByTradePost = any> {
userId: string;
type: NotificationType;
data: T;
}
18 changes: 13 additions & 5 deletions src/notifications/notification.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateNotificationDTO } from './dto/notification.dto';
import { NotificationType } from './enums/notification-type.enum';
import { NotificationResponse } from './responses/notification.response';
import { Notification, NotificationDocument } from './schemas/notification.schema';
import { NotificationBuilder } from './services/notification-builder.service';
import { NotificationDataByTradePost } from './types/notification-data.type';

@Injectable()
export class NotificationService {
private readonly logger = new Logger(NotificationService.name);

constructor(
@InjectModel(Notification.name) private readonly notificationModel: Model<NotificationDocument>,
private readonly notificaitonBuilder: NotificationBuilder,
) {}

async notifyTradePostComment(dto: CreateNotificationDTO<NotificationDataByTradePost>) {
const { title, body } = this.notificaitonBuilder.getMessage(dto.type, dto.data);
const notification = await this.notificationModel.create({ title, body, ...dto });
return new NotificationResponse(notification);
async notifyTradePostComment({ data, userId }: CreateNotificationDTO<NotificationDataByTradePost>) {
try {
const type = NotificationType.TRADE_POST_COMMENT;
const { title, body } = this.notificaitonBuilder.getMessage(type, data);
const notification = await this.notificationModel.create({ title, body, type, userId });
return new NotificationResponse(notification);
} catch (err) {
this.logger.error(err);
}
}
}
5 changes: 5 additions & 0 deletions src/trade-posts/services/trade-post-comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ export class TradePostCommentService {
const session = await this.tradePostCommentModel.startSession();

const result = await session.withTransaction(async () => {
const tradePost = await this.tradePostModel.findById(tradePostId);
const comment = await this.tradePostCommentModel.create({ ...input, authorId, postId: tradePostId });
await this.tradePostModel.findByIdAndUpdate(tradePostId, { $push: { comments: comment.id } });
this.notificationService.notifyTradePostComment({
userId: tradePost.authorId,
data: { tradePostId, tradePostTitle: tradePost.title },
});
return new TradePostCommentResponse(comment);
});

Expand Down

0 comments on commit 1422ea0

Please sign in to comment.