package com.sinata.appointment.service.event; import com.sinata.appointment.entity.AppointmentOrder; import com.sinata.appointment.mapper.AppointmentOrderMapper; import com.sinata.appointment.service.PostApprovalOrchestrationService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.transaction.event.TransactionPhase; import org.springframework.transaction.event.TransactionalEventListener; @Slf4j @Component @RequiredArgsConstructor public class AppointmentLifecycleEventListener { private final AppointmentOrderMapper appointmentOrderMapper; private final PostApprovalOrchestrationService postApprovalOrchestrationService; @Async("applicationTaskExecutor") @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void handleApproved(AppointmentApprovedEvent event) { AppointmentOrder order = appointmentOrderMapper.selectById(event.orderId()); if (order == null) { log.warn("Approved event skipped, order not found orderId={}", event.orderId()); return; } postApprovalOrchestrationService.onOrderFullyApproved(order); } @Async("applicationTaskExecutor") @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void handleRejected(AppointmentRejectedEvent event) { AppointmentOrder order = appointmentOrderMapper.selectById(event.orderId()); if (order == null) { log.warn("Rejected event skipped, order not found orderId={}", event.orderId()); return; } postApprovalOrchestrationService.onOrderRejected(order, event.reason()); } }