Skip to content

Commit db2a715

Browse files
committed
fix(email): reorganize helper methods in helper and main methods in service; remove old tests
# Conflicts: # src/main/java/com/SleepUp/SU/utils/email/EmailServiceHelper.java # src/test/java/com/SleepUp/SU/utils/EmailServiceHelperTest.java # src/test/java/com/SleepUp/SU/utils/EmailServiceImplTest.java # src/test/java/com/SleepUp/SU/utils/EmailServiceImplTestEmail.java
1 parent 1b1a4fb commit db2a715

4 files changed

Lines changed: 55 additions & 782 deletions

File tree

Lines changed: 55 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,23 @@
11
package com.SleepUp.SU.utils.email;
22

33
import com.SleepUp.SU.accommodation.entity.Accommodation;
4-
import com.SleepUp.SU.auth.AuthService;
5-
import com.SleepUp.SU.config.properties.MailProperties;
64
import com.SleepUp.SU.reservation.entity.Reservation;
75
import com.SleepUp.SU.user.entity.User;
8-
import jakarta.annotation.PostConstruct;
9-
import lombok.RequiredArgsConstructor;
106
import org.slf4j.Logger;
117
import org.slf4j.LoggerFactory;
12-
import org.springframework.scheduling.annotation.Async;
13-
import org.springframework.stereotype.Service;
8+
import org.springframework.stereotype.Component;
9+
import org.thymeleaf.context.Context;
1410

1511
import java.math.BigDecimal;
1612

17-
@Service
18-
@RequiredArgsConstructor
13+
@Component
1914
public class EmailServiceHelper {
2015

21-
private final EmailService emailService;
22-
public Logger logger = LoggerFactory.getLogger(AuthService.class);
16+
private static final Logger logger = LoggerFactory.getLogger(EmailServiceHelper.class);
2317

24-
public MailProperties mailProperties;
25-
private boolean emailEnabled;
26-
27-
@PostConstruct
28-
public void validateEmailConfig() {
29-
emailEnabled = mailProperties != null
30-
&& mailProperties.getFrom() != null && !mailProperties.getFrom().isBlank()
31-
&& mailProperties.getUsername() != null && !mailProperties.getUsername().isBlank()
32-
&& mailProperties.getPassword() != null && !mailProperties.getPassword().isBlank();
33-
34-
if (!emailEnabled) {
35-
logger.warn("Email sending is disabled due to missing or invalid mail configuration.");
36-
} else {
37-
logger.info("Email configuration validated successfully. Email sending enabled.");
38-
}
39-
}
40-
41-
@Async
42-
public void sendWelcomeEmail(User user) {
43-
try {
44-
emailService.sendWelcomeEmail(user);
45-
logger.info("Welcome email sent successfully to: {}", user.getEmail());
46-
} catch (Exception e) {
47-
logger.warn("Failed to send welcome email to {}: {}", user.getEmail(), e.getMessage());
48-
}
49-
}
50-
51-
@Async
52-
public void sendOwnerReservedNotification(Reservation reservation) {
53-
try {
54-
emailService.sendOwnerReservedNotification(reservation);
55-
logger.info("New reservation notification email sent successfully to: {}", reservation.getAccommodation().getManagedBy().getEmail());
56-
} catch (Exception e) {
57-
logger.warn("Failed to send new reservation email to {}: {}", reservation.getAccommodation().getManagedBy().getEmail(), e.getMessage());
58-
}
59-
}
60-
61-
@Async
62-
public void sendReservationConfirmationEmail(Reservation reservation, BigDecimal discountAmount) {
63-
try {
64-
emailService.sendGuestReservationConfirmationEmail(reservation, discountAmount);
65-
logger.info("Reservation confirmation email sent successfully to: {}", reservation.getUser().getEmail());
66-
} catch (Exception e) {
67-
logger.warn("Failed to send reservation confirmation email to {}: {}", reservation.getUser().getEmail(), e.getMessage());
68-
}
69-
}
70-
71-
@Async
72-
public void sendReservationReminderEmail(Reservation reservation) {
73-
try {
74-
emailService.sendGuestReservationReminderEmail(reservation);
75-
logger.info("Reservation reminder email sent successfully to: {}", reservation.getUser().getEmail());
76-
} catch (Exception e) {
77-
logger.warn("Failed to send reservation reminder email to {}: {}", reservation.getUser().getEmail(), e.getMessage());
78-
}
79-
}
80-
81-
@Async
82-
public void sendOwnerReservationReminderEmail(Reservation reservation) {
83-
try {
84-
emailService.sendOwnerReservationReminderEmail(reservation);
85-
logger.info("Owner reservation reminder email sent successfully to: {}", reservation.getAccommodation().getManagedBy().getEmail());
86-
} catch (Exception e) {
87-
logger.warn("Failed to send owner reservation reminder email to {}: {}", reservation.getAccommodation().getManagedBy().getEmail(), e.getMessage());
88-
}
89-
}
90-
91-
@Async
92-
public void sendCancellationConfirmationEmail(Reservation reservation) {
93-
try {
94-
emailService.sendCancellationConfirmationEmail(reservation);
95-
logger.info("Cancellation confirmation email sent successfully to: {}", reservation.getUser().getEmail());
96-
} catch (Exception e) {
97-
logger.warn("Failed to send cancellation confirmation email to {}: {}", reservation.getUser().getEmail(), e.getMessage());
98-
}
99-
}
100-
101-
@Async
102-
public void sendCancellationByOwnerNotificationEmail(Reservation reservation) {
103-
try {
104-
emailService.sendCancellationByOwnerNotificationEmail(reservation);
105-
logger.info("Cancellation by owner notification email sent successfully to: {}", reservation.getUser().getEmail());
106-
} catch (Exception e) {
107-
logger.warn("Failed to send cancellation by owner notification email to {}: {}", reservation.getUser().getEmail(), e.getMessage());
108-
}
109-
}
110-
111-
@Async
112-
public void sendCancellationNotificationToOwnerEmail(Reservation reservation) {
113-
try {
114-
emailService.sendCancellationNotificationToOwnerEmail(reservation);
115-
logger.info("Cancellation notification to owner email sent successfully to: {}", reservation.getAccommodation().getManagedBy().getEmail());
116-
} catch (Exception e) {
117-
logger.warn("Failed to send cancellation notification to owner email to {}: {}", reservation.getAccommodation().getManagedBy().getEmail(), e.getMessage());
118-
}
119-
}
120-
121-
@Async
122-
public void handleNewReservationEmails(Reservation reservation, BigDecimal discountAmount) {
123-
logger.info("Processing emails for new reservation - Guest: {}, Accommodation: {}",
124-
reservation.getUser().getEmail(), reservation.getAccommodation().getName());
125-
126-
sendReservationConfirmationEmail(reservation, discountAmount);
127-
sendOwnerReservedNotification(reservation);
128-
129-
logger.info("New reservation email processing completed");
130-
}
131-
132-
@Async
133-
public void handleGuestCancellationEmails(Reservation reservation) {
134-
logger.info("Processing emails for guest cancellation - Guest: {}, Accommodation: {}",
135-
reservation.getUser().getEmail(), reservation.getAccommodation().getName());
136-
137-
sendCancellationConfirmationEmail(reservation);
138-
sendCancellationNotificationToOwnerEmail(reservation);
139-
140-
logger.info("Guest cancellation email processing completed");
141-
}
142-
143-
@Async
144-
public void handleOwnerCancellationEmails(Reservation reservation) {
145-
logger.info("Processing emails for owner cancellation - Guest: {}, Accommodation: {}",
146-
reservation.getUser().getEmail(), reservation.getAccommodation().getName());
147-
148-
sendCancellationByOwnerNotificationEmail(reservation);
149-
150-
logger.info("Owner cancellation email processing completed");
151-
}
152-
153-
@Async
154-
public void sendReservationReminders(Reservation reservation) {
155-
logger.info("Sending reservation reminders - Guest: {}, Owner: {}, Accommodation: {}",
156-
reservation.getUser().getEmail(), reservation.getAccommodation().getManagedBy().getEmail(), reservation.getAccommodation().getName());
157-
158-
sendReservationReminderEmail(reservation);
159-
sendOwnerReservationReminderEmail(reservation);
160-
161-
logger.info("Reservation reminders sent successfully");
162-
}
18+
private static final String DASHBOARD_URL = "http://localhost:8080/swagger-ui/index.html#";
19+
private static final String RESERVATIONS_URL = DASHBOARD_URL + "/api/v1/reservations";
20+
private static final String ACCOMMODATIONS_URL = DASHBOARD_URL + "/api/v1/accommodations";
16321

16422
public boolean canSendEmails(User user) {
16523
if (user == null || user.getEmail() == null || user.getEmail().trim().isEmpty()) {
@@ -192,4 +50,51 @@ public boolean canSendReservationEmails(Reservation reservation) {
19250

19351
return true;
19452
}
195-
}
53+
54+
public Context createFullContext(Reservation reservation, User user, BigDecimal discountAmount) {
55+
Context context = new Context();
56+
57+
context.setVariable("dashboardUrl", DASHBOARD_URL);
58+
59+
if (reservation != null) {
60+
Accommodation accommodation = reservation.getAccommodation();
61+
context.setVariable("reservationUrl", setReservationUrl(reservation));
62+
context.setVariable("accommodationUrl", setAccommodationUrl(accommodation));
63+
context.setVariable("accommodationName", accommodation.getName());
64+
context.setVariable("location", accommodation.getLocation());
65+
context.setVariable("checkInDate", reservation.getCheckInDate() != null ? reservation.getCheckInDate().toString() : "N/A");
66+
context.setVariable("checkOutDate", reservation.getCheckOutDate() != null ? reservation.getCheckOutDate().toString() : "N/A");
67+
context.setVariable("amount", reservation.getTotalPrice());
68+
} else {
69+
context.setVariable("accommodationName", "Default Accommodation");
70+
context.setVariable("location", "Default Location");
71+
context.setVariable("checkInDate", "N/A");
72+
context.setVariable("checkOutDate", "N/A");
73+
context.setVariable("amount", "N/A");
74+
}
75+
76+
if (user != null) {
77+
context.setVariable("userName", user.getName());
78+
context.setVariable("guestName", user.getName());
79+
} else {
80+
context.setVariable("userName", "Default User");
81+
context.setVariable("guestName", "Unknown Guest");
82+
}
83+
84+
context.setVariable("discountAmount", discountAmount);
85+
86+
return context;
87+
}
88+
89+
public String setReservationUrl(Reservation reservation) {
90+
return RESERVATIONS_URL + "/" + reservation.getId();
91+
}
92+
93+
public String setAccommodationUrl(Accommodation accommodation) {
94+
return ACCOMMODATIONS_URL + "/" + accommodation.getId();
95+
}
96+
97+
public String getDashboardUrl() {
98+
return DASHBOARD_URL;
99+
}
100+
}

0 commit comments

Comments
 (0)