From d51e13e74047c47ffe9c8d1b4b7cde6bc1d282a1 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 9 Mar 2025 15:52:51 +0900 Subject: [PATCH] feat: add handler --- .../comment/comment-event-handler.ts | 39 +++++++++++ .../friend/friend-event-handler.ts | 68 +++++++++++++++++++ src/event-handlers/fund/fund-event-handler.ts | 65 ++++++++++++++++++ .../gratitude/gratitude-event-handler.ts | 55 +++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 src/event-handlers/comment/comment-event-handler.ts create mode 100644 src/event-handlers/friend/friend-event-handler.ts create mode 100644 src/event-handlers/fund/fund-event-handler.ts create mode 100644 src/event-handlers/gratitude/gratitude-event-handler.ts diff --git a/src/event-handlers/comment/comment-event-handler.ts b/src/event-handlers/comment/comment-event-handler.ts new file mode 100644 index 0000000..bde220a --- /dev/null +++ b/src/event-handlers/comment/comment-event-handler.ts @@ -0,0 +1,39 @@ +import { Injectable } from "@nestjs/common"; +import { OnEvent } from "@nestjs/event-emitter"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Funding } from "src/entities/funding.entity"; +import { Notification } from "src/entities/notification.entity"; +import { User } from "src/entities/user.entity"; +import { NotiType } from "src/enums/noti-type.enum"; +import { Repository } from "typeorm"; + +@Injectable() +export class CommentEventHandler { + constructor( + @InjectRepository(Notification) + private notiRepository: Repository, + + @InjectRepository(User) + private userRepository: Repository, + + @InjectRepository(Funding) + private fundRepository: Repository, + ) {} + + @OnEvent('NewComment') + async handleNewComment(data: { fundId: number, authorId: number }) { + const noti = new Notification(); + const funding = await this.fundRepository.findOne({ + where: { fundId: data.fundId }, + relations: ['fundUser'], + }); + const sender = await this.userRepository.findOne({ where: { userId: data.authorId }}); + + noti.recvId = funding.fundUser; + noti.sendId = sender; + noti.notiType = NotiType.NewComment; + noti.subId = funding.fundUuid; + + return this.notiRepository.save(noti); + } +} \ No newline at end of file diff --git a/src/event-handlers/friend/friend-event-handler.ts b/src/event-handlers/friend/friend-event-handler.ts new file mode 100644 index 0000000..002da5b --- /dev/null +++ b/src/event-handlers/friend/friend-event-handler.ts @@ -0,0 +1,68 @@ +import { Injectable } from "@nestjs/common"; +import { OnEvent } from "@nestjs/event-emitter"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Notification } from "src/entities/notification.entity"; +import { User } from "src/entities/user.entity"; +import { NotiType } from "src/enums/noti-type.enum"; +import { FriendDto } from "src/features/friend/dto/friend.dto"; +import { Repository } from "typeorm"; + +@Injectable() +export class FriendEventHandler { + constructor( + @InjectRepository(Notification) + private readonly notiRepository: Repository, + + @InjectRepository(User) + private readonly userRepository: Repository, + ) {} + + + @OnEvent('AcceptFollow') + async handleAcceptFollow(userId: number, friendDto: FriendDto) { + const noti1 = new Notification(); + const noti2 = new Notification(); + const receiver = await this.userRepository.findOneBy({ userId: friendDto.friendId }) + const sender = await this.userRepository.findOneBy({ userId: userId }) + + noti1.sendId = sender; + noti1.recvId = receiver; + noti2.sendId = receiver; + noti2.recvId = sender; + noti1.notiType = NotiType.NewFriend; + noti2.notiType = NotiType.NewFriend; + + if (!friendDto.notiId) { + const deleteNoti = await this.notiRepository.createQueryBuilder("notification") + .leftJoinAndSelect("notification.recvId", "receiver") + .where("notification.recvId = :recvId", { recvId: userId }) + .andWhere("notification.sendId = :sendId", { sendId: friendDto.friendId }) // 여기서 senderId는 숫자 타입이어야 합니다. + .andWhere("notification.notiType = :notiType", { notiType: NotiType.IncomingFollow }) + .getOne(); + + if (deleteNoti) { + await this.notiRepository.delete(deleteNoti.notiId); + }; + } else { + await this.notiRepository.delete(friendDto.notiId); + } + + await this.notiRepository.save(noti1); + await this.notiRepository.save(noti2); + + return; + } + + @OnEvent('IncomingFollow') + async handleIncomingFollow(userId: number, friendDto: FriendDto) { + const noti = new Notification(); + const receiver = await this.userRepository.findOneBy({ userId: friendDto.friendId }) + const sender = await this.userRepository.findOneBy({ userId: userId }) + + noti.sendId = sender; + noti.recvId = receiver; + noti.notiType = NotiType.IncomingFollow; + + return await this.notiRepository.save(noti); + } +} \ No newline at end of file diff --git a/src/event-handlers/fund/fund-event-handler.ts b/src/event-handlers/fund/fund-event-handler.ts new file mode 100644 index 0000000..8b0fac0 --- /dev/null +++ b/src/event-handlers/fund/fund-event-handler.ts @@ -0,0 +1,65 @@ +import { Injectable } from "@nestjs/common"; +import { OnEvent } from "@nestjs/event-emitter"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Funding } from "src/entities/funding.entity"; +import { Notification } from "src/entities/notification.entity"; +import { User } from "src/entities/user.entity"; +import { NotiType } from "src/enums/noti-type.enum"; +import { Repository } from "typeorm"; + +@Injectable() +export class FundEventHandler { + constructor( + @InjectRepository(Notification) + private notiRepository: Repository, + + @InjectRepository(User) + private userRepository: Repository, + + @InjectRepository(Funding) + private fundRepository: Repository, + ) {} + + @OnEvent('FundClose') + async handleFundClose(fundId: number) { + const funding = await this.fundRepository.findOne({ + where: { fundId }, + relations: ['fundUser', 'donations', 'donations.user'] + }); + + // FundClose 알림 생성 및 저장 + const fundCloseNoti = new Notification(); + fundCloseNoti.recvId = funding.fundUser; + fundCloseNoti.notiType = NotiType.FundClose; + fundCloseNoti.subId = funding.fundUuid; + await this.notiRepository.save(fundCloseNoti); + + // DonatedFundClose 이벤트 처리, 기부자들에게 알림 생성 + if (funding.donations && funding.donations.length > 0) { + const notifications = funding.donations.map(donation => { + const noti = new Notification(); + noti.recvId = donation.user; // 받는 사람은 기부자 + noti.sendId = funding.fundUser; // 보내는 사람은 펀딩 주최자 + noti.notiType = NotiType.DonatedFundClose; // 알림 타입 설정 + noti.subId = funding.fundUuid; + + return this.notiRepository.save(noti); + }); + + // 모든 알림 저장 + await Promise.all(notifications); + } + } + + @OnEvent('FundAchieve') + async handleFundAchieve(data: {recvId: number, subId: string}) { + const noti = new Notification(); + const receiver = await this.userRepository.findOneBy({ userId: data.recvId }); + + noti.recvId = receiver; + noti.notiType = NotiType.FundAchieve; + noti.subId = data.subId; + + return await this.notiRepository.save(noti); + } +} \ No newline at end of file diff --git a/src/event-handlers/gratitude/gratitude-event-handler.ts b/src/event-handlers/gratitude/gratitude-event-handler.ts new file mode 100644 index 0000000..15ddb45 --- /dev/null +++ b/src/event-handlers/gratitude/gratitude-event-handler.ts @@ -0,0 +1,55 @@ +import { Injectable } from "@nestjs/common"; +import { OnEvent } from "@nestjs/event-emitter"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Funding } from "src/entities/funding.entity"; +import { Notification } from "src/entities/notification.entity"; +import { NotiType } from "src/enums/noti-type.enum"; +import { Repository } from "typeorm"; + +@Injectable() +export class GratitudeEventHandler { + constructor( + @InjectRepository(Notification) + private notiRepository: Repository, + + @InjectRepository(Funding) + private fundRepository: Repository, + ) {} + + @OnEvent('WriteGratitude') + async handleWriteGratitude(fundId: number) { + const noti = new Notification(); + const funding = await this.fundRepository.findOne({ + where: { fundId }, + relations: ['fundUser'], + }); + + noti.recvId = funding.fundUser; + noti.notiType = NotiType.WriteGratitude; + noti.subId = funding.fundUuid; + + return this.notiRepository.save(noti); + } + + @OnEvent('CheckGratitude') + async handleCheckGratitude(fundId: number) { + const funding = await this.fundRepository.findOne({ + where: { fundId }, + relations: ['fundUser', 'donations', 'donations.user'] + }); + + // 해당 펀딩에 기여한 모든 사용자들에게 알림을 생성 + const notifications = funding.donations.map(donation => { + const noti = new Notification(); + noti.recvId = donation.user; // 받는 사람은 기부자 + noti.sendId = funding.fundUser; // 보내는 사람은 펀딩 주최자 + noti.notiType = NotiType.CheckGratitude; // 알림 타입 설정 + noti.subId = funding.fundUuid; + + return this.notiRepository.save(noti); + }); + + // 모든 알림을 비동기적으로 저장 + await Promise.all(notifications); + } +} \ No newline at end of file