Email integration with PEC and MAILGUN services using api call.
This commit is contained in:
@@ -1,22 +1,33 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationAmendmentRequestEntity;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationEntity;
|
||||
import net.gepafin.tendermanagement.entities.HubEntity;
|
||||
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.model.request.EmailConfig;
|
||||
import net.gepafin.tendermanagement.model.response.SystemEmailTemplateResponse;
|
||||
import net.gepafin.tendermanagement.repositories.CallRepository;
|
||||
import net.gepafin.tendermanagement.repositories.HubRepository;
|
||||
import net.gepafin.tendermanagement.service.ApplicationService;
|
||||
import net.gepafin.tendermanagement.service.HubService;
|
||||
import net.gepafin.tendermanagement.service.SystemEmailTemplatesService;
|
||||
import net.gepafin.tendermanagement.service.UserService;
|
||||
import net.gepafin.tendermanagement.service.impl.EmailService;
|
||||
import net.gepafin.tendermanagement.service.impl.EmailServiceFactory;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.MailUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -24,6 +35,8 @@ import java.util.Map;
|
||||
@Component
|
||||
public class EmailNotificationDao {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(EmailNotificationDao.class);
|
||||
|
||||
@Autowired
|
||||
private MailUtil mailUtil;
|
||||
|
||||
@@ -39,126 +52,140 @@ public class EmailNotificationDao {
|
||||
@Autowired
|
||||
private HubService hubService;
|
||||
|
||||
public void sendMailToNotifyBeneficiaryRegardingNewAmendment(ApplicationAmendmentRequestEntity applicationAmendmentRequest) {
|
||||
@Autowired
|
||||
EmailServiceFactory emailServiceFactory;
|
||||
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationAmendmentRequest.getApplicationId());
|
||||
HubEntity hub = hubService.valdateHub(applicationEntity.getHubId());
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService.retrieveTemplateByTypeAndCall(
|
||||
SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.DOCUMENTATION_INTEGRATION_REQUEST, hub, null);
|
||||
@Autowired
|
||||
HubRepository hubRepository;
|
||||
|
||||
// Create the map for subject placeholders
|
||||
|
||||
private void sendEmail(ApplicationAmendmentRequestEntity amendmentRequest, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum templateType,
|
||||
Map<String, String> bodyPlaceholders, List<String> additionalRecipients) {
|
||||
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(amendmentRequest.getApplicationId());
|
||||
HubEntity hubEntity = hubService.valdateHub(applicationEntity.getHubId());
|
||||
String service = determineService(applicationEntity.getHubId());
|
||||
String legalMail = service.equals("Gepafin S.p.a.") ? "bandi.gepafin@legalmail.it" : "bandi.sviluppumbria@legalmail.it";
|
||||
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService.retrieveTemplateByTypeAndCall(templateType, hubEntity, null);
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
subjectPlaceholders.put("{{company_name}}", applicationEntity.getCompany().getCompanyName());
|
||||
|
||||
// Create the map for body placeholders
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
bodyPlaceholders.put("{{protocol_number}}", applicationEntity.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatLocalDateTime(applicationAmendmentRequest.getProtocol().getCreatedDate(), GepafinConstant.DD_MM_YYYY));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(applicationAmendmentRequest.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
bodyPlaceholders.put("{{form_dataInput}}", "");
|
||||
|
||||
// Replace placeholders in the subject and body
|
||||
bodyPlaceholders.put("{{legal_mail}}", legalMail);
|
||||
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
||||
|
||||
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
||||
mailUtil.sendByMailGun(subject, body, List.of(userEntity.getBeneficiary().getEmail()), null);
|
||||
List<String> recipientEmails = new ArrayList<>();
|
||||
if (applicationEntity.getCompany().getEmail() != null) {
|
||||
recipientEmails.add(applicationEntity.getCompany().getEmail());
|
||||
}
|
||||
if (userEntity.getBeneficiary().getEmail() != null) {
|
||||
recipientEmails.add(userEntity.getBeneficiary().getEmail());
|
||||
}
|
||||
if (additionalRecipients != null) {
|
||||
recipientEmails.addAll(additionalRecipients);
|
||||
}
|
||||
sendMail(applicationEntity.getHubId(), subject, body, recipientEmails);
|
||||
}
|
||||
|
||||
private String determineService(Long hubId) {
|
||||
|
||||
HubEntity hub = hubRepository.findById(hubId).orElseThrow(() -> new IllegalArgumentException("Invalid Hub ID: " + hubId));
|
||||
return hub.getEmailServiceType().equalsIgnoreCase("MAILGUN_SERVICE") ? "Gepafin S.p.a." : "Sviluppumbria";
|
||||
}
|
||||
|
||||
public void sendMailToNotifyBeneficiaryRegardingNewAmendment(ApplicationAmendmentRequestEntity applicationAmendmentRequest) {
|
||||
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationAmendmentRequest.getApplicationId());
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
bodyPlaceholders.put("{{protocol_number}}", applicationAmendmentRequest.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatLocalDateTime(applicationAmendmentRequest.getProtocol().getCreatedDate(), GepafinConstant.DD_MM_YYYY));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(applicationAmendmentRequest.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
String formFieldsJson = applicationAmendmentRequest.getFormFields();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
List<Map<String, Object>> formFields = objectMapper.readValue(formFieldsJson, new TypeReference<List<Map<String, Object>>>() {
|
||||
});
|
||||
//•
|
||||
StringBuilder bulletPoints = new StringBuilder();
|
||||
for (Map<String, Object> field : formFields) {
|
||||
String label = (String) field.get("label");
|
||||
boolean selected = (boolean) field.get("selected");
|
||||
if (!selected) {
|
||||
bulletPoints.append("• ").append(label).append("\n");
|
||||
}
|
||||
}
|
||||
bodyPlaceholders.put("{{form_dataInput}}", bulletPoints.toString());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to parse form fields JSON: ", e);
|
||||
}
|
||||
bodyPlaceholders.put("{{note}}", applicationAmendmentRequest.getNote());
|
||||
sendEmail(applicationAmendmentRequest, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.DOCUMENTATION_INTEGRATION_REQUEST, bodyPlaceholders, null);
|
||||
}
|
||||
|
||||
public void sendApplicationFailureNotificationEmail(ApplicationAmendmentRequestEntity amendmentRequest) {
|
||||
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(amendmentRequest.getApplicationId());
|
||||
HubEntity hub = hubService.valdateHub(applicationEntity.getHubId());
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService.retrieveTemplateByTypeAndCall(
|
||||
SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.INADMISSIBILITY_NOTIFICATION_DUE_TO_FAILURE, hub, null);
|
||||
|
||||
// Create the map for subject placeholders
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
subjectPlaceholders.put("{{company_name}}", applicationEntity.getCompany().getCompanyName());
|
||||
|
||||
// Create the map for body placeholders
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
bodyPlaceholders.put("{{date_time_emailSend}}", DateTimeUtil.formatLocalDateTime(amendmentRequest.getCreatedDate(), GepafinConstant.DD_MM_YYYY_HH_MM));
|
||||
|
||||
// Replace placeholders in the subject and body
|
||||
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
||||
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
||||
if (userEntity.getBeneficiary().getEmail() != null) {
|
||||
mailUtil.sendByMailGun(subject, body, List.of(userEntity.getBeneficiary().getEmail()), null);
|
||||
}
|
||||
sendEmail(amendmentRequest, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.INADMISSIBILITY_NOTIFICATION_DUE_TO_FAILURE, bodyPlaceholders, null);
|
||||
}
|
||||
|
||||
public void sendAdmissibilityNotificationEmailForApprovedApplication(ApplicationAmendmentRequestEntity amendmentRequest) {
|
||||
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(amendmentRequest.getApplicationId());
|
||||
HubEntity hub = hubService.valdateHub(applicationEntity.getHubId());
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService.retrieveTemplateByTypeAndCall(
|
||||
SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.ADMISSIBILITY_NOTIFICATION, hub, null);
|
||||
|
||||
// Create the map for subject placeholders
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
subjectPlaceholders.put("{{company_name}}", applicationEntity.getCompany().getCompanyName());
|
||||
|
||||
// Create the map for body placeholders
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
bodyPlaceholders.put("{{protocol_number}}", applicationEntity.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(applicationEntity.getProtocol().getCreatedDate()));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(applicationEntity.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
bodyPlaceholders.put("{{protocol_number}}", amendmentRequest.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(amendmentRequest.getProtocol().getCreatedDate()));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(amendmentRequest.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
|
||||
// Replace placeholders in the subject and body
|
||||
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
||||
|
||||
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
||||
String userEmail;
|
||||
String companyEmail;
|
||||
if (userEntity.getBeneficiary().getEmail() != null && applicationEntity.getCompany().getEmail() != null) {
|
||||
userEmail = userEntity.getBeneficiary().getEmail();
|
||||
companyEmail = applicationEntity.getCompany().getEmail();
|
||||
mailUtil.sendByMailGun(subject, body, List.of(userEmail), null);
|
||||
mailUtil.sendByMailGun(subject, body, List.of(companyEmail), null);
|
||||
}
|
||||
sendEmail(amendmentRequest, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.ADMISSIBILITY_NOTIFICATION, bodyPlaceholders, null);
|
||||
}
|
||||
|
||||
public void sendInadmissibilityEmailForRejectedApplication(ApplicationAmendmentRequestEntity amendmentRequest) {
|
||||
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(amendmentRequest.getApplicationId());
|
||||
HubEntity hub = hubService.valdateHub(applicationEntity.getHubId());
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService.retrieveTemplateByTypeAndCall(
|
||||
SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.INADMISSIBILITY_TEMPLATE, hub, null);
|
||||
|
||||
// Create the map for subject placeholders
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
subjectPlaceholders.put("{{company_name}}", applicationEntity.getCompany().getCompanyName());
|
||||
|
||||
// Create the map for body placeholders
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
bodyPlaceholders.put("{{protocol_number}}", applicationEntity.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(applicationEntity.getProtocol().getCreatedDate()));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(applicationEntity.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
bodyPlaceholders.put("{{form_text}}", "");
|
||||
bodyPlaceholders.put("{{protocol_number}}", amendmentRequest.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatCreatedDate(amendmentRequest.getProtocol().getCreatedDate()));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(amendmentRequest.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
bodyPlaceholders.put("{{form_text}}", amendmentRequest.getNote());
|
||||
|
||||
// Replace placeholders in the subject and body
|
||||
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
||||
|
||||
UserEntity userEntity = userService.validateUser(applicationEntity.getUserId());
|
||||
String userEmail;
|
||||
String companyEmail;
|
||||
if (userEntity.getBeneficiary().getEmail() != null && applicationEntity.getCompany().getEmail() != null) {
|
||||
userEmail = userEntity.getBeneficiary().getEmail();
|
||||
companyEmail = applicationEntity.getCompany().getEmail();
|
||||
mailUtil.sendByMailGun(subject, body, List.of(userEmail), null);
|
||||
mailUtil.sendByMailGun(subject, body, List.of(companyEmail), null);
|
||||
}
|
||||
sendEmail(amendmentRequest, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.INADMISSIBILITY_TEMPLATE, bodyPlaceholders, null);
|
||||
}
|
||||
|
||||
}
|
||||
public void sendMail(Long hubId, String subject, String body, List<String> recipientEmails) {
|
||||
|
||||
EmailConfig emailConfig = retrieveEmailConfig(hubId);
|
||||
EmailService emailService = emailServiceFactory.getEmailService(emailConfig.getEmailServiceType());
|
||||
emailService.sendEmail(subject, body, recipientEmails, emailConfig);
|
||||
}
|
||||
|
||||
public EmailConfig retrieveEmailConfig(Long hubId) {
|
||||
|
||||
HubEntity hubEntity = hubRepository.findById(hubId).orElseThrow(() -> new IllegalArgumentException("Invalid Hub ID: " + hubId));
|
||||
String emailServiceType = hubEntity.getEmailServiceType();
|
||||
String encryptedConfigJson = hubEntity.getEmailServiceConfig();
|
||||
String decryptedConfigJson = Utils.decryptCredential(encryptedConfigJson);
|
||||
EmailConfig emailConfig = parseEmailConfig(decryptedConfigJson);
|
||||
emailConfig.setEmailServiceType(emailServiceType);
|
||||
return emailConfig;
|
||||
}
|
||||
|
||||
private EmailConfig parseEmailConfig(String configJson) {
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
return objectMapper.readValue(configJson, EmailConfig.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalArgumentException("Failed to parse email configuration JSON", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user