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
27 changes: 27 additions & 0 deletions src/event-handlers/donation/donation-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ import { DonationRefundCancelledEvent } from 'src/features/donation/domain/event
import { AdminAssignedForDonationRefundEvent } from 'src/features/donation/domain/events/admin-assigned-for-refune.event';
import { DonationRefundCompletedEvent } from 'src/features/donation/domain/events/donation-refund-completed.event';
import { DonationDeletedEvent } from 'src/features/donation/domain/events/donation-deleted.event';
import { Notification } from 'src/entities/notification.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { User } from 'src/entities/user.entity';

@Injectable()
export class DonationEventHandler {
constructor(
private readonly notificationService: NotificationService,
private readonly findAllAdmins: FindAllAdminsUseCase,

@InjectRepository(Notification)
private notiRepository: Repository<Notification>,

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

/**
Expand Down Expand Up @@ -123,4 +133,21 @@ export class DonationEventHandler {

await this.notificationService.createNoti(createNotificationDtoForDonor);
}

/**
* 새로운 후원이 추가되었을 때, 펀딩 게시시자에게 알림을 보냅니다.
*/
@OnEvent('NewDonate')
async handleNewDonate(data: {recvId: number, sendId: number, subId: string}) {
const noti = new Notification();
const receiver = await this.userRepository.findOneBy({ userId: data.recvId })
const sender = await this.userRepository.findOneBy({ userId: data.sendId })

noti.sendId = sender;
noti.recvId = receiver;
noti.notiType = NotiType.NewDonate;
noti.subId = data.subId;

return await this.notiRepository.save(noti);
}
}
8 changes: 8 additions & 0 deletions src/event-handlers/event-handlers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import { NotificationService } from 'src/features/notification/notification.serv
import { ProvisionalDonationFsmService } from 'src/features/donation/domain/services/provisional-donation-fsm.service';
import { DepositFsmService } from 'src/features/deposit/domain/deposit-fsm.service';
import { DonationFsmService } from 'src/features/donation/domain/services/donation-fsm.service';
import { FriendEventHandler } from './friend/friend-event-handler';
import { FundEventHandler } from './fund/fund-event-handler';
import { GratitudeEventHandler } from './gratitude/gratitude-event-handler';
import { CommentEventHandler } from './comment/comment-event-handler';

const usecases = [
DeleteDonationUseCase,
Expand Down Expand Up @@ -58,6 +62,10 @@ const services = [
DepositEventHandler,
DonationEventHandler,
ProvisionalDonationEventHandler,
CommentEventHandler,
FriendEventHandler,
FundEventHandler,
GratitudeEventHandler,
...usecases,
...services,
],
Expand Down
4 changes: 2 additions & 2 deletions src/features/funding/funding.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ export class FundingService {

if (!friendship) {
queryBuilder.andWhere(
'fund.fundPubl = :publ',
'funding.fundPubl = :publ',
{ publ: true }
)
}
}
} else {
queryBuilder.where('funding.fundUser != :userId', { userId });
queryBuilder.where('funding.fundUser != :userId', { userId: user.userId });

const friendIds = await this.friendRepository
.createQueryBuilder('friend')
Expand Down
160 changes: 0 additions & 160 deletions src/features/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { NotiType } from 'src/enums/noti-type.enum';
import { Donation } from 'src/entities/donation.entity';
import { Funding } from 'src/entities/funding.entity';
import { OnEvent } from '@nestjs/event-emitter';
import { FriendDto } from '../friend/dto/friend.dto';
import { ImageType } from 'src/enums/image-type.enum';
import { Image } from 'src/entities/image.entity';

Expand Down Expand Up @@ -106,165 +105,6 @@ export class NotificationService {
};
}

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

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

@OnEvent('NewDonate')
async handleNewDonate(data: {recvId: number, sendId: number, subId: string}) {
const noti = new Notification();
const receiver = await this.userRepository.findOneBy({ userId: data.recvId })
const sender = await this.userRepository.findOneBy({ userId: data.sendId })

noti.sendId = sender;
noti.recvId = receiver;
noti.notiType = NotiType.NewDonate;
noti.subId = data.subId;

return await this.notiRepository.save(noti);
}

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

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

async createNoti(
createNotiDto: CreateNotificationDto,
): Promise<Notification> {
Expand Down
Loading