diff --git a/src/notifications/dto/notification.dto.ts b/src/notifications/dto/notification.dto.ts index c1c8860..ef6ffd1 100644 --- a/src/notifications/dto/notification.dto.ts +++ b/src/notifications/dto/notification.dto.ts @@ -1,8 +1,6 @@ -import { NotificationType } from '../enums/notification-type.enum'; import { NotificationDataByTradePost } from '../types/notification-data.type'; export class CreateNotificationDTO { userId: string; - type: NotificationType; data: T; } diff --git a/src/notifications/notification.service.ts b/src/notifications/notification.service.ts index 9d34557..ae2121e 100644 --- a/src/notifications/notification.service.ts +++ b/src/notifications/notification.service.ts @@ -1,7 +1,8 @@ -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'; @@ -9,14 +10,21 @@ 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, private readonly notificaitonBuilder: NotificationBuilder, ) {} - async notifyTradePostComment(dto: CreateNotificationDTO) { - 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) { + 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); + } } } diff --git a/src/trade-posts/services/trade-post-comment.service.ts b/src/trade-posts/services/trade-post-comment.service.ts index eb57a54..beaf8c6 100644 --- a/src/trade-posts/services/trade-post-comment.service.ts +++ b/src/trade-posts/services/trade-post-comment.service.ts @@ -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); });