Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@


import life.mosu.mosuserver.application.virtualaccount.VirtualAccountLogService;
import life.mosu.mosuserver.domain.examapplication.repository.ExamApplicationJpaRepository;
import life.mosu.mosuserver.domain.virtualaccount.DepositStatus;
import life.mosu.mosuserver.domain.virtualaccount.VirtualAccountLogJpaEntity;
import life.mosu.mosuserver.global.exception.CustomRuntimeException;
import life.mosu.mosuserver.global.exception.ErrorCode;
import life.mosu.mosuserver.infra.notify.MailNotifier;
import life.mosu.mosuserver.infra.notify.dto.luna.LunaNotificationEvent;
import life.mosu.mosuserver.infra.notify.dto.luna.LunaNotificationStatus;
import life.mosu.mosuserver.infra.notify.dto.mail.DepositSuccessMailRequest;
import life.mosu.mosuserver.infra.notify.support.NotifyEventPublisher;
import life.mosu.mosuserver.presentation.virtualaccount.dto.event.DepositSuccessEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -17,6 +23,8 @@ public class DepositSuccessEventHandler implements DepositEventHandler<DepositSu

private final VirtualAccountLogService virtualAccountLogService;
private final MailNotifier<DepositSuccessMailRequest> mailer;
private final NotifyEventPublisher notifier;
private final ExamApplicationJpaRepository examApplicationJpaRepository;

@Override
@Transactional
Expand All @@ -25,6 +33,16 @@ public void handle(DepositSuccessEvent event) {
event.getOrderId(),
DepositStatus.DONE
);
var exam = examApplicationJpaRepository.findByApplicationId(log.getApplicationId())
.stream().findFirst()
.orElseThrow(
() -> new CustomRuntimeException(ErrorCode.EXAM_APPLICATION_NOT_FOUND));

LunaNotificationEvent lunaNotificationEvent = LunaNotificationEvent.create(
LunaNotificationStatus.APPLICATION_SUCCESS,
exam.getUserId(), exam.getId());

notifier.notify(lunaNotificationEvent);
Comment on lines +36 to +45

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The repository method findByApplicationId returns a List<ExamApplicationJpaEntity>, which implies that one applicationId can be associated with multiple exam applications. By using .stream().findFirst(), you are only processing the first exam application found and silently ignoring any others. This could lead to missed notifications if a single payment is intended to cover multiple exam applications.

To ensure all applications related to the payment are processed, you should iterate over the entire list of exam applications and send a notification for each one.

        var exams = examApplicationJpaRepository.findByApplicationId(log.getApplicationId());
        if (exams.isEmpty()) {
            throw new CustomRuntimeException(ErrorCode.EXAM_APPLICATION_NOT_FOUND);
        }

        exams.forEach(exam -> {
            var lunaNotificationEvent = LunaNotificationEvent.create(
                    LunaNotificationStatus.APPLICATION_SUCCESS,
                    exam.getUserId(),
                    exam.getId()
            );
            notifier.notify(lunaNotificationEvent);
        });

sendMail(log, event.getFormattedCreatedAt());
}

Expand Down