Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/event-handlers/comment/comment-event-handler.ts
Original file line number Diff line number Diff line change
@@ -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<Notification>,

@InjectRepository(User)
private userRepository: Repository<User>,

@InjectRepository(Funding)
private fundRepository: Repository<Funding>,
) {}

@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);
}
}
68 changes: 68 additions & 0 deletions src/event-handlers/friend/friend-event-handler.ts
Original file line number Diff line number Diff line change
@@ -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<Notification>,

@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}


@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);
}
}
65 changes: 65 additions & 0 deletions src/event-handlers/fund/fund-event-handler.ts
Original file line number Diff line number Diff line change
@@ -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<Notification>,

@InjectRepository(User)
private userRepository: Repository<User>,

@InjectRepository(Funding)
private fundRepository: Repository<Funding>,
) {}

@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);
}
}
55 changes: 55 additions & 0 deletions src/event-handlers/gratitude/gratitude-event-handler.ts
Original file line number Diff line number Diff line change
@@ -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<Notification>,

@InjectRepository(Funding)
private fundRepository: Repository<Funding>,
) {}

@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);
}
}