package se.bilhalsning.service; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import se.bilhalsning.entity.Order; import se.bilhalsning.entity.User; import se.bilhalsning.repository.UserRepository; import java.util.UUID; @Service @RequiredArgsConstructor public class OrderNotificationService { private final UserRepository userRepository; private final EmailService emailService; @Value("${app.public-base-url:http://localhost:3000}") private String publicBaseUrl; public void notifyOrderProcessing(Order order) { String email = resolveCustomerEmail(order); if (email.isBlank()) { return; } emailService.sendOrderProcessingEmail( email, order.getPlate(), ordersPageUrl()); } public void notifyOrderSent(Order order, String trackingId) { String email = resolveCustomerEmail(order); if (email.isBlank()) { return; } String trackingUrl = "https://www.postnord.se/verktyg/spara/?id=" + trackingId; emailService.sendOrderSentEmail(email, order.getPlate(), trackingId, trackingUrl); } public void notifyOrderFailed(Order order) { String email = resolveCustomerEmail(order); if (email.isBlank()) { return; } emailService.sendOrderFailedEmail(email, order.getPlate(), ordersPageUrl()); } private String resolveCustomerEmail(Order order) { if (order.getUser() != null && order.getUser().getEmail() != null) { return order.getUser().getEmail(); } UUID userId = order.getUserId(); if (userId == null) { return ""; } return userRepository.findById(userId) .map(User::getEmail) .orElse(""); } private String ordersPageUrl() { String base = publicBaseUrl.endsWith("/") ? publicBaseUrl.substring(0, publicBaseUrl.length() - 1) : publicBaseUrl; return base + "/mina-bestallningar"; } }