Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 50 notification manager #62

Merged
merged 4 commits into from
Nov 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public AlertDTO createAlert(AlertAddDTO alertAddDTO) {
Alert alert = alertRepository.save(
mapper.mapAlertAddDtoToAlert(alertAddDTO));

//TODO: powiadomienia są wysyłane przed zakończeniem tranzakcji, to umożliwia wysłanie powiadomień
// nawet jeżeli alert nie zostanie zapisany w bazie
sendNotifications(alertAddDTO);

return mapper.mapAlertToAlertDto(alert);
Expand All @@ -35,11 +37,17 @@ public AlertDTO createAlert(AlertAddDTO alertAddDTO) {
void sendNotifications(AlertAddDTO alertAddDTO) {
Set<UserDTO> interestedUsers = userFacade.getInterestedUsers(alertAddDTO.location());

notificationManager.addEmailService();
notificationManager.addSMSService();
notificationManager.sendNotifications(alertAddDTO, interestedUsers);

notificationManager.removeEmailService();
notificationManager.removeSMSService();
interestedUsers.stream()
.forEach(user -> {
if (user.phoneNumber() != null && !user.phoneNumber().isEmpty()) {
notificationManager.addSMSService();
}
if (user.email() != null && !user.email().isEmpty()) {
notificationManager.addEmailService();
}

notificationManager.sendNotifications(alertAddDTO, user);
notificationManager.clearNotificationServices();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,20 @@
import pl.ateam.disasteralerts.disasteralert.dto.AlertAddDTO;
import pl.ateam.disasteralerts.user.dto.UserDTO;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
@RequiredArgsConstructor
class EmailService implements AlertListener {
class EmailService implements NotificationListener {

private static final Logger logger = LoggerFactory.getLogger(EmailService.class);

private final JavaMailSender mailSender;

@Override
public void addedAlert(AlertAddDTO alertAddDTO, Set<UserDTO> interestedUsers) {
ExecutorService executor = Executors.newFixedThreadPool(3);

interestedUsers.forEach(interestedUser -> {
executor.submit(() -> {
try {
sendEmail(interestedUser.email(), "Alert for " + alertAddDTO.location(), alertAddDTO.description());
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
});

executor.shutdown();
public void addedAlert(AlertAddDTO alertAddDTO, UserDTO interestedUser) {
sendEmail(interestedUser.email(), "Alert for " + alertAddDTO.location(), alertAddDTO.description());
}

@Recover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import pl.ateam.disasteralerts.disasteralert.dto.AlertAddDTO;
import pl.ateam.disasteralerts.user.dto.UserDTO;

import java.util.Set;
interface NotificationListener {

interface AlertListener {

void addedAlert(AlertAddDTO alertAddDTO, Set<UserDTO> interestedUsers);
void addedAlert(AlertAddDTO alertAddDTO, UserDTO interestedUser);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
@RequiredArgsConstructor
Expand All @@ -16,32 +15,28 @@ class NotificationManager {
private final EmailService emailService;
private final SMSService smsService;

private final List<AlertListener> alertListeners = new ArrayList<>();
void sendNotifications(AlertAddDTO alertAddDTO, Set<UserDTO> interestedUsers) {
alertListeners.forEach(alertListener -> alertListener.addedAlert(alertAddDTO, interestedUsers));
}

void addAlertListener(AlertListener alertListener) {
alertListeners.add(alertListener);
}

void removeAlertListener(AlertListener alertListener) {
alertListeners.remove(alertListener);
private final List<NotificationListener> notificationListeners = new ArrayList<>();
void sendNotifications(AlertAddDTO alertAddDTO, UserDTO interestedUser) {
notificationListeners.forEach(notificationListener -> notificationListener.addedAlert(alertAddDTO, interestedUser));
}

void addEmailService() {
alertListeners.add(emailService);
notificationListeners.add(emailService);
}

void addSMSService() {
alertListeners.add(smsService);
notificationListeners.add(smsService);
}

void removeEmailService() {
alertListeners.remove(emailService);
notificationListeners.remove(emailService);
}

void removeSMSService() {
alertListeners.remove(smsService);
notificationListeners.remove(smsService);
}

void clearNotificationServices() {
notificationListeners.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package pl.ateam.disasteralerts.disasteralert;

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
Expand All @@ -13,20 +16,16 @@
import org.springframework.transaction.annotation.Transactional;
import pl.ateam.disasteralerts.disasteralert.dto.AlertAddDTO;
import pl.ateam.disasteralerts.user.dto.UserDTO;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import pl.ateam.disasteralerts.util.EntityAudit;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

@Service
@RequiredArgsConstructor
@Slf4j
class SMSService implements AlertListener {
class SMSService implements NotificationListener {

private final SMSLimitService smsLimitService;

Expand All @@ -35,10 +34,8 @@ class SMSService implements AlertListener {
public static final String TWILIO_PHONE_NUMBER = System.getenv("TWILIO_PHONE_NUMBER");

@Override
public void addedAlert(AlertAddDTO alertAddDTO, Set<UserDTO> interestedUsers) {
interestedUsers.forEach(userDTO -> {
sendSMS(alertAddDTO.description(), userDTO.phoneNumber());
});
public void addedAlert(AlertAddDTO alertAddDTO, UserDTO interestedUser) {
sendSMS(alertAddDTO.description(), interestedUser.phoneNumber());
}

public void sendSMS(String alertDescription, String phoneNumber) {
Expand All @@ -55,7 +52,7 @@ public void sendSMS(String alertDescription, String phoneNumber) {
)
.create();

System.out.println(message.getSid());
log.info(message.getSid());

smsLimitService.increaseLimit(today);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pl.ateam.disasteralerts.disasteralert.dto.AlertAddDTO;
import pl.ateam.disasteralerts.disasteralert.dto.AlertDTO;
import pl.ateam.disasteralerts.user.UserFacade;
import pl.ateam.disasteralerts.user.dto.UserDTO;

import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -57,9 +58,7 @@ void shouldReturnAlertDto_whenAlertAddDtoIsValid() {
when(alertMapper.mapAlertAddDtoToAlert(alertAddDto)).thenReturn(alert);
when(alertMapper.mapAlertToAlertDto(alert)).thenReturn(alertDto);

when(userFacade.getInterestedUsers(any(String.class))).thenReturn(null);
doNothing().when(notificationManager).addSMSService();
doNothing().when(notificationManager).sendNotifications(any(AlertAddDTO.class), any(Set.class));
doNothing().when(notificationManager).sendNotifications(any(AlertAddDTO.class), any(UserDTO.class));

//then
AlertDTO result = alertService.createAlert(alertAddDto);
Expand Down