Template issue.
This commit is contained in:
@@ -9,9 +9,13 @@ import net.gepafin.tendermanagement.enums.EmailEntityTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.RecipientTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.StatusTypeEnum;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.model.request.ApplicationAmendmentRequest;
|
||||
import net.gepafin.tendermanagement.model.request.EmailConfig;
|
||||
import net.gepafin.tendermanagement.model.request.EmailLogRequest;
|
||||
import net.gepafin.tendermanagement.model.response.AmendmentFormFieldResponse;
|
||||
import net.gepafin.tendermanagement.model.response.SystemEmailTemplateResponse;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationFormFieldRepository;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationFormRepository;
|
||||
import net.gepafin.tendermanagement.repositories.EmailLogRepository;
|
||||
import net.gepafin.tendermanagement.repositories.HubRepository;
|
||||
import net.gepafin.tendermanagement.service.*;
|
||||
@@ -27,6 +31,8 @@ import org.springframework.stereotype.Component;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
import static net.gepafin.tendermanagement.dao.ApplicationAmendmentRequestDao.filterByName;
|
||||
|
||||
@Component
|
||||
public class EmailNotificationDao {
|
||||
|
||||
@@ -40,7 +46,7 @@ public class EmailNotificationDao {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private HubService hubService;
|
||||
|
||||
@@ -55,91 +61,133 @@ public class EmailNotificationDao {
|
||||
|
||||
@Autowired
|
||||
private EmailLogRepository emailLogRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationFormRepository applicationFormRepository;
|
||||
|
||||
private void sendEmail(ApplicationEntity applicationEntity, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum templateType,
|
||||
Map<String, String> bodyPlaceholders, List<String> additionalRecipients,Long amendmentId) {
|
||||
@Autowired
|
||||
private ApplicationFormFieldRepository applicationFormFieldRepository;
|
||||
|
||||
private void sendEmail(ApplicationEntity applicationEntity, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum templateType, Map<String, String> bodyPlaceholders,
|
||||
List<String> additionalRecipients, Long amendmentId) {
|
||||
|
||||
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";
|
||||
// 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<>();
|
||||
CompanyEntity company=companyService.validateCompany(applicationEntity.getCompanyId());
|
||||
CompanyEntity company = companyService.validateCompany(applicationEntity.getCompanyId());
|
||||
subjectPlaceholders.put("{{call_name}}", applicationEntity.getCall().getName());
|
||||
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
|
||||
bodyPlaceholders.put("{{legal_mail}}", legalMail);
|
||||
// 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());
|
||||
List<String> recipientEmails = getRecipientEmails(applicationEntity, userEntity, additionalRecipients);
|
||||
EmailLogRequest emailLogRequest=emailLogDao.createEmailLogRequest(systemEmailTemplateResponse.getEmailScenario(), RecipientTypeEnum.BENEFICIARY,userEntity.getBeneficiary().getId(),Utils.listToCommaSeparatedString(recipientEmails),userEntity.getId(),applicationEntity.getId(),amendmentId ,applicationEntity.getCall().getId());
|
||||
sendMail(applicationEntity.getHubId(), subject, body, recipientEmails,emailLogRequest);
|
||||
EmailLogRequest emailLogRequest = emailLogDao.createEmailLogRequest(systemEmailTemplateResponse.getEmailScenario(), RecipientTypeEnum.BENEFICIARY, 1L,
|
||||
Utils.listToCommaSeparatedString(recipientEmails), userEntity.getId(), applicationEntity.getId(), amendmentId, applicationEntity.getCall().getId());
|
||||
sendMail(applicationEntity.getHubId(), subject, body, recipientEmails, emailLogRequest);
|
||||
}
|
||||
private List<String> getRecipientEmails(ApplicationEntity applicationEntity, UserEntity userEntity, List<String> additionalRecipients) {
|
||||
|
||||
List<String> recipientEmails = new ArrayList<>();
|
||||
CompanyEntity company=companyService.validateCompany(applicationEntity.getCompanyId());
|
||||
String companyEmail = company.getEmail();
|
||||
String contactEmail = company.getContactEmail();
|
||||
|
||||
if (companyEmail != null && !companyEmail.isEmpty()) {
|
||||
recipientEmails.add(companyEmail);
|
||||
}
|
||||
if (contactEmail != null && !contactEmail.isEmpty() && !contactEmail.equals(companyEmail)) {
|
||||
recipientEmails.add(contactEmail);
|
||||
}
|
||||
|
||||
if (userEntity.getBeneficiary().getEmail() != null) {
|
||||
recipientEmails.add(userEntity.getBeneficiary().getEmail());
|
||||
}
|
||||
if (additionalRecipients != null) {
|
||||
recipientEmails.addAll(additionalRecipients);
|
||||
}
|
||||
CompanyEntity company = companyService.validateCompany(applicationEntity.getCompanyId());
|
||||
// String companyEmail = company.getEmail();
|
||||
// String contactEmail = company.getContactEmail();
|
||||
//
|
||||
// if (companyEmail != null && !companyEmail.isEmpty()) {
|
||||
// recipientEmails.add(companyEmail);
|
||||
// }
|
||||
// if (contactEmail != null && !contactEmail.isEmpty() && !contactEmail.equals(companyEmail)) {
|
||||
// recipientEmails.add(contactEmail);
|
||||
// }
|
||||
//
|
||||
// if (userEntity.getBeneficiary().getEmail() != null) {
|
||||
// recipientEmails.add(userEntity.getBeneficiary().getEmail());
|
||||
// }
|
||||
// if (additionalRecipients != null) {
|
||||
// recipientEmails.addAll(additionalRecipients);
|
||||
// }
|
||||
|
||||
recipientEmails.add("piyush1.kag1@gmail.com");
|
||||
return recipientEmails;
|
||||
}
|
||||
//
|
||||
// private String determineService(Long hubId) {
|
||||
//
|
||||
// HubEntity hub = hubRepository.findById(hubId).orElseThrow(() -> new IllegalArgumentException("Invalid Hub ID: " + hubId));
|
||||
// return hub.getEmailServiceType().equalsIgnoreCase("MAILGUN_SERVICE") ? "Sviluppumbria" : "Gepafin S.p.a.";
|
||||
// }
|
||||
|
||||
public void sendMailToNotifyBeneficiaryRegardingNewAmendment(ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity) {
|
||||
|
||||
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());
|
||||
ApplicationEntity applicationEntity = applicationService.validateApplication(applicationAmendmentRequestEntity.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();
|
||||
bodyPlaceholders.put("{{protocol_number}}", applicationAmendmentRequestEntity.getProtocol().getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{protocol_date}}", DateTimeUtil.formatLocalDateTime(applicationAmendmentRequestEntity.getProtocol().getCreatedDate(), GepafinConstant.DD_MM_YYYY));
|
||||
bodyPlaceholders.put("{{protocol_time}}", DateTimeUtil.parseLocalTimeToString(applicationAmendmentRequestEntity.getProtocol().getTime(), GepafinConstant.HH_MM_SS));
|
||||
|
||||
try {
|
||||
List<Map<String, Object>> formFields = objectMapper.readValue(formFieldsJson, new TypeReference<List<Map<String, Object>>>() {
|
||||
});
|
||||
//•
|
||||
// Retrieve forms and initialize required collections
|
||||
List<ApplicationFormEntity> forms = applicationFormRepository.findByApplicationId(applicationEntity.getId());
|
||||
List<AmendmentFormFieldResponse> allFormFields = new ArrayList<>();
|
||||
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");
|
||||
}
|
||||
|
||||
// Extract data from forms
|
||||
for (ApplicationFormEntity form : forms) {
|
||||
String content = form.getForm().getContent();
|
||||
List<Map<String, Object>> result = filterByName(content, "fileupload");
|
||||
allFormFields.addAll(getIdAndLabelFromResult(result));
|
||||
}
|
||||
|
||||
// Process allFormFields and generate bullet points
|
||||
for (AmendmentFormFieldResponse field : allFormFields) {
|
||||
// Build bullet points
|
||||
bulletPoints.append(field.getLabel());
|
||||
bulletPoints.append("\n");
|
||||
}
|
||||
// Add the generated bullet points to placeholders
|
||||
bodyPlaceholders.put("{{form_dataInput}}", bulletPoints.toString());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to parse form fields JSON: ", e);
|
||||
log.error("Failed to process form fields: ", e);
|
||||
}
|
||||
bodyPlaceholders.put("{{note}}", applicationAmendmentRequest.getNote());
|
||||
sendEmail(applicationEntity, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.DOCUMENTATION_INTEGRATION_REQUEST, bodyPlaceholders, null,applicationAmendmentRequest.getId());
|
||||
|
||||
|
||||
bodyPlaceholders.put("{{note}}", applicationAmendmentRequestEntity.getNote());
|
||||
sendEmail(applicationEntity, SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum.DOCUMENTATION_INTEGRATION_REQUEST, bodyPlaceholders, null,
|
||||
applicationAmendmentRequestEntity.getId());
|
||||
}
|
||||
|
||||
public List<AmendmentFormFieldResponse> getIdAndLabelFromResult(List<Map<String, Object>> result) {
|
||||
List<AmendmentFormFieldResponse> formFieldResponses = new ArrayList<>();
|
||||
|
||||
for (Map<String, Object> item : result) {
|
||||
AmendmentFormFieldResponse formFieldResponse = new AmendmentFormFieldResponse();
|
||||
formFieldResponse.setFieldId((String) item.get("id"));
|
||||
|
||||
// Extract "label" value from the "settings" array
|
||||
List<Map<String, Object>> settings = (List<Map<String, Object>>) item.get("settings");
|
||||
String label = settings.stream()
|
||||
.filter(setting -> "label".equals(setting.get("name")))
|
||||
.map(setting -> (String) setting.get("value"))
|
||||
.findFirst()
|
||||
.orElse(""); // Default to empty string if not found
|
||||
|
||||
if (label == null || label.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
formFieldResponse.setLabel(label); // Set the label as fieldValue
|
||||
formFieldResponses.add(formFieldResponse);
|
||||
}
|
||||
|
||||
return formFieldResponses;
|
||||
}
|
||||
|
||||
public void sendApplicationFailureNotificationEmail(ApplicationAmendmentRequestEntity amendmentRequest) {
|
||||
|
||||
Reference in New Issue
Block a user