Skip to content
Snippets Groups Projects

feat: [DLCM-2812] send email to request creator when request is processed

Merged Alicia.DeDiosFuente requested to merge adf-2812-send-email-request-creator- into master
All threads resolved!
Files
16
@@ -51,6 +51,7 @@ import ch.unige.solidify.exception.SolidifyUnmodifiableException;
import ch.unige.solidify.service.ResourceService;
import ch.unige.solidify.util.StringTool;
import ch.dlcm.DLCMConstants;
import ch.dlcm.controller.AdminController;
import ch.dlcm.message.EmailMessage;
import ch.dlcm.model.StatusHistory;
@@ -283,19 +284,8 @@ public class NotificationService extends ResourceService<Notification> {
@Override
public Notification save(Notification item) {
// Send an email for notification of type access dataset request
if (item.getNotificationType().equals(NotificationType.ACCESS_DATASET_REQUEST)) {
EmailMessage emailMessage;
Map<String, Object> parameters = new HashMap<>();
parameters.put(item.getObjectId(), null);
if (Objects.requireNonNull(item.getNotificationStatus()) == NotificationStatus.APPROVED) {
emailMessage = new EmailMessage(item.getEmitter().getEmail(), EmailMessage.EmailTemplate.REQUEST_TO_ACCESS_DATASET_ACCEPTED, parameters);
SolidifyEventPublisher.getPublisher().publishEvent(Objects.requireNonNull(emailMessage));
} else if (item.getNotificationStatus() == NotificationStatus.REFUSED) {
emailMessage = new EmailMessage(item.getEmitter().getEmail(), EmailMessage.EmailTemplate.REQUEST_TO_ACCESS_DATASET_REFUSED, parameters);
SolidifyEventPublisher.getPublisher().publishEvent(Objects.requireNonNull(emailMessage));
}
}
// Email emitter for certain types of notification
this.sendEmailToEmitter(item);
// Status is non-applicable if notification category is info
Optional<NotificationType> notificationType = this.notificationTypeRepository.findById(item.getNotificationType().getResId());
@@ -307,6 +297,58 @@ public class NotificationService extends ResourceService<Notification> {
return super.save(item);
}
private void sendEmailToEmitter(Notification notification) {
if ((notification.getNotificationType().equals(NotificationType.ACCESS_DATASET_REQUEST)
|| notification.getNotificationType().equals(NotificationType.JOIN_ORGUNIT_REQUEST)
|| notification.getNotificationType().equals(NotificationType.CREATE_ORGUNIT_REQUEST)
|| notification.getNotificationType().equals(NotificationType.APPROVE_DISPOSAL_REQUEST)
|| notification.getNotificationType().equals(NotificationType.APPROVE_DISPOSAL_BY_ORGUNIT_REQUEST))
&& (notification.getNotificationStatus().equals(NotificationStatus.APPROVED)
|| notification.getNotificationStatus().equals(NotificationStatus.REFUSED))) {
EmailMessage.EmailTemplate emailTemplate = this.getEmailTemplate(notification.getNotificationType(), notification.getNotificationStatus());
if (emailTemplate != null) {
Map<String, Object> parameters = new HashMap<>();
if (notification.getNotificationType().equals(NotificationType.JOIN_ORGUNIT_REQUEST)) {
parameters.put("objectId", notification.getNotifiedOrgUnit().getResId());
} else {
parameters.put("objectId", notification.getObjectId());
}
if (notification.getNotificationStatus().equals(NotificationStatus.REFUSED)) {
parameters.put(DLCMConstants.REASON, notification.getResponseMessage());
}
EmailMessage emailMessage = new EmailMessage(notification.getEmitter().getEmail(), emailTemplate, parameters);
SolidifyEventPublisher.getPublisher().publishEvent(Objects.requireNonNull(emailMessage));
}
}
}
private EmailMessage.EmailTemplate getEmailTemplate(NotificationType notificationType, NotificationStatus notificationStatus) {
Map<String, Map<NotificationStatus, EmailMessage.EmailTemplate>> templateMap = Map.of(
NotificationType.ACCESS_DATASET_REQUEST.getResId(), Map.of(
NotificationStatus.APPROVED, EmailMessage.EmailTemplate.REQUEST_TO_ACCESS_DATASET_ACCEPTED,
NotificationStatus.REFUSED, EmailMessage.EmailTemplate.REQUEST_TO_ACCESS_DATASET_REFUSED
),
NotificationType.JOIN_ORGUNIT_REQUEST.getResId(), Map.of(
NotificationStatus.APPROVED, EmailMessage.EmailTemplate.REQUEST_TO_JOIN_ORG_UNIT_ACCEPTED,
NotificationStatus.REFUSED, EmailMessage.EmailTemplate.REQUEST_TO_JOIN_ORG_UNIT_REFUSED
),
NotificationType.CREATE_ORGUNIT_REQUEST.getResId(), Map.of(
NotificationStatus.APPROVED, EmailMessage.EmailTemplate.REQUEST_TO_CREATE_ORG_UNIT_ACCEPTED,
NotificationStatus.REFUSED, EmailMessage.EmailTemplate.REQUEST_TO_CREATE_ORG_UNIT_REFUSED
),
NotificationType.APPROVE_DISPOSAL_REQUEST.getResId(), Map.of(
NotificationStatus.APPROVED, EmailMessage.EmailTemplate.REQUEST_TO_DISPOSE_ARCHIVE_ACCEPTED
),
NotificationType.APPROVE_DISPOSAL_BY_ORGUNIT_REQUEST.getResId(), Map.of(
NotificationStatus.APPROVED, EmailMessage.EmailTemplate.REQUEST_TO_DISPOSE_ARCHIVE_ACCEPTED
)
);
return Optional.ofNullable(templateMap.get(notificationType.getResId()))
.map(statusMap -> statusMap.get(notificationStatus))
.orElse(null);
}
/*
* Verify that each contributorsId correspond to an authenticated user and the type of notification is the type of
* MY_INDIRECT_DEPOSIT_COMPLETED_INFO
@@ -340,6 +382,9 @@ public class NotificationService extends ResourceService<Notification> {
NotificationStatus.REFUSED.equals(newStatus)) {
throw new SolidifyUnmodifiableException("Notification of category INFO cannot be refused");
}
if (notification.getNotificationStatus().equals(NotificationStatus.APPROVED) || notification.getNotificationStatus().equals(NotificationStatus.REFUSED)) {
throw new SolidifyUnmodifiableException("Notification already approved or refused, cannot be changed");
}
notification.setNotificationStatus(newStatus);
notification.setResponseMessage(responseMessage);
this.save(notification);
Loading