Files
bflows-bandi-be/src/main/java/net/gepafin/tendermanagement/dao/EmailDao.java
2025-11-11 15:23:40 +05:30

214 lines
8.7 KiB
Java

package net.gepafin.tendermanagement.dao;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.*;
import net.gepafin.tendermanagement.enums.EmailScenarioTypeEnum;
import net.gepafin.tendermanagement.enums.EmailServiceTypeEnum;
import net.gepafin.tendermanagement.enums.RecipientTypeEnum;
import net.gepafin.tendermanagement.enums.StatusTypeEnum;
import net.gepafin.tendermanagement.model.request.EmailLogRequest;
import net.gepafin.tendermanagement.model.response.EmailResendResponseBean;
import net.gepafin.tendermanagement.model.response.EmailSendResponse;
import net.gepafin.tendermanagement.repositories.*;
import net.gepafin.tendermanagement.service.CallService;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
@Log4j2
public class EmailDao {
@Autowired
EmailLogRepository emailLogRepository;
@Autowired
EmailNotificationDao emailNotificationDao;
@Autowired
private CallService callService;
@Autowired
private EmailLogDao emailLogDao;
@Autowired
private UserActionsRepository userActionsRepository;
@Autowired
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private ApplicationEvaluationRepository applicationEvaluationRepository;
public EmailResendResponseBean resendEmail(HttpServletRequest request , Long userActionId){
UserActionEntity userActionEntity = userActionsRepository.findUserActionByIdAndIsDeletedFalse(userActionId);
if(userActionEntity == null){
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_ACTION_ID_NOT_FOUND));
}
List<EmailLogEntity> emailLogs = emailLogRepository.findByUserActionIdAndEmailServiceTypeAndSendStatus(userActionId,EmailServiceTypeEnum.PEC_SERVICE.getValue(),StatusTypeEnum.FAILED.getValue());
if (emailLogs.isEmpty()) {
log.info("No emails found for given userActionId: {}",userActionId);
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.NO_EMAIL_LOG_FOUND));
}
EmailResendResponseBean emailResendResponseBean = new EmailResendResponseBean();
for (EmailLogEntity log : emailLogs){
EmailLogRequest emailLogRequest = emailLogDao.createEmailLogRequest(EmailScenarioTypeEnum.valueOf(log.getEmailType()),
RecipientTypeEnum.valueOf(log.getRecipientType()),
log.getRecipientId(),
log.getRecipientEmails(),
log.getUserId(),
log.getApplicationId(),
log.getAmendmentId(),
log.getCallId(),log.getEmailSubject(),log.getEmailBody()
);
EmailLogEntity emailLogEntity=emailLogDao.createEmailLog(emailLogRequest,Utils.convertJsonStringToList(log.getAttachments(),String.class));
List<String> recipients = Utils.commaSeparatedStringToList(log.getRecipientEmails());
CallEntity call = callService.validateCall(log.getCallId());
emailNotificationDao.sendMail(
call.getHub().getId(),
log.getEmailSubject(),
log.getEmailBody(),
recipients,
emailLogEntity
);
}
EmailSendResponse emailSendResponse = buildEmailSendResponseFromRequest(request);
emailResendResponseBean.setEmailSendResponse(emailSendResponse);
if (Boolean.TRUE.equals(emailSendResponse.getIsEmailSend())){
updateEmailSendStatusIfSuccessful(emailSendResponse);
}
return emailResendResponseBean;
}
private void updateEmailSendStatusIfSuccessful(EmailSendResponse emailSendResponse){
Long actionId = emailSendResponse.getUserActionId();
EmailLogEntity emailLog = emailLogRepository.findTopByUserActionIdAndEmailServiceTypeAndSendStatusOrderByIdDesc(
actionId,
EmailServiceTypeEnum.PEC_SERVICE.getValue(),
StatusTypeEnum.SUCCESS.getValue()
);
if (emailLog != null) {
switch (emailSendResponse.getEmailScenario()) {
case APPLICATION_AMENDMENT_REQUESTED:
case APPLICATION_AMENDMENT_REMINDER:
updateApplicationAmendmentStatus(emailLog, emailSendResponse.getEmailScenario().getValue());
break;
case USER_CREATION:
case PASSWORD_RESET_REQUEST:
updateUserEmailStatus(emailLog, emailSendResponse.getEmailScenario().getValue());
break;
case APPLICATION_ADMISSIBLE:
case APPLICATION_REJECTED:
updateApplicationEvaluationStatus(emailLog, emailSendResponse.getEmailScenario().getValue());
break;
default:
log.warn("Unhandled email scenario: {}", emailSendResponse.getEmailScenario());
}
}
}
private void updateApplicationAmendmentStatus(EmailLogEntity log, String scenario) {
if (log.getAmendmentId() != null) {
applicationAmendmentRequestRepository.findById(log.getAmendmentId()).ifPresent(amendment -> {
if (updateEmailSendResponse(amendment.getEmailSendResponse(), scenario)) {
applicationAmendmentRequestRepository.save(amendment);
}
});
}
}
private void updateApplicationEvaluationStatus(EmailLogEntity log, String scenario) {
if (log.getApplicationId() != null) {
ApplicationEvaluationEntity evaluation = applicationEvaluationRepository.findByApplicationId(log.getApplicationId());
if (evaluation != null && updateEmailSendResponse(evaluation.getEmailSendResponse(), scenario)) {
applicationEvaluationRepository.save(evaluation);
}
}
}
private void updateUserEmailStatus(EmailLogEntity log, String scenario) {
if (log.getUserId() != null) {
userRepository.findById(log.getUserId()).ifPresent(user -> {
if (updateEmailSendResponse(user.getEmailSendResponse(), scenario)) {
userRepository.save(user);
}
});
}
}
private boolean updateEmailSendResponse(List<EmailSendResponse> responses, String scenario) {
if (responses == null || responses.isEmpty()) return false;
for (Iterator<EmailSendResponse> iterator = responses.iterator(); iterator.hasNext(); ) {
EmailSendResponse response = iterator.next();
if (scenario.equals(response.getEmailScenario().getValue())) {
iterator.remove(); // remove only the first match
return true;
}
}
return false;
}
public EmailSendResponse buildEmailSendResponseFromRequest(HttpServletRequest request) {
Long userActionId = (Long) request.getAttribute(GepafinConstant.USER_ACTION_ID);
List<EmailLogEntity> emailLogs = emailLogRepository.findByUserActionIdAndEmailServiceType(userActionId,EmailServiceTypeEnum.PEC_SERVICE.getValue());
boolean allSuccess = true;
String emailScenario = null;
for (EmailLogEntity log : emailLogs) {
if (emailScenario == null) {
emailScenario = log.getEmailType();
}
boolean isSuccess = StatusTypeEnum.SUCCESS.getValue().equals(log.getSendStatus());
if (Boolean.FALSE.equals(isSuccess)) {
allSuccess = false;
break;
}
}
return buildResponse(userActionId, allSuccess, emailScenario);
}
private EmailSendResponse buildResponse(Long userActionId, boolean allSuccess, String emailScenario) {
EmailSendResponse response = new EmailSendResponse();
response.setUserActionId(userActionId);
response.setIsEmailSend(allSuccess);
response.setEmailScenario(emailScenario != null ? EmailScenarioTypeEnum.valueOf(emailScenario) : null);
return response;
}
}