147 lines
7.2 KiB
Java
147 lines
7.2 KiB
Java
package net.gepafin.tendermanagement.dao;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import liquibase.util.Validate;
|
|
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.EmailServiceTypeEnum;
|
|
import net.gepafin.tendermanagement.enums.StatusTypeEnum;
|
|
import net.gepafin.tendermanagement.model.response.EmailLogResponse;
|
|
import net.gepafin.tendermanagement.model.response.PecEmailLogResponse;
|
|
import net.gepafin.tendermanagement.model.response.PecMailResponse;
|
|
import net.gepafin.tendermanagement.repositories.EmailLogRepository;
|
|
import net.gepafin.tendermanagement.repositories.UserActionsRepository;
|
|
import net.gepafin.tendermanagement.service.ApplicationService;
|
|
import net.gepafin.tendermanagement.service.CallService;
|
|
import net.gepafin.tendermanagement.util.Utils;
|
|
import net.gepafin.tendermanagement.util.Validator;
|
|
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.checkerframework.checker.units.qual.A;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Component
|
|
@Log4j2
|
|
public class PecMailDao {
|
|
|
|
@Autowired
|
|
private Validator validator;
|
|
|
|
@Autowired
|
|
private UserActionsRepository userActionsRepository;
|
|
|
|
@Autowired
|
|
private EmailLogRepository emailLogRepository;
|
|
|
|
@Autowired
|
|
private CallService callService;
|
|
|
|
@Autowired
|
|
private EmailNotificationDao emailNotificationDao;
|
|
|
|
@Autowired
|
|
private ApplicationService applicationService;
|
|
|
|
|
|
public List<PecMailResponse> sendPecMail(HttpServletRequest request, List<Long> userActionIds) {
|
|
|
|
List<PecMailResponse> pecMailResponses=new ArrayList<>();
|
|
for (Long userActionId: userActionIds) {
|
|
List<EmailLogEntity> emailLogs = getEmailLogEntities(request, userActionId);
|
|
for (EmailLogEntity log : emailLogs) {
|
|
List<String> recipients = Utils.commaSeparatedStringToList(log.getRecipientEmails());
|
|
CallEntity call = callService.validateCall(log.getCallId());
|
|
emailNotificationDao.sendPendingMail(
|
|
call.getHub().getId(),
|
|
log.getEmailSubject(),
|
|
log.getEmailBody(),
|
|
recipients,
|
|
log
|
|
);
|
|
ApplicationEntity applicationEntity=applicationService.validateApplication(log.getApplicationId());
|
|
String callName=applicationEntity.getCall().getName();
|
|
PecMailResponse pecMailResponse=createPecMailResponse(log.getUserAction().getId(),log,callName);
|
|
pecMailResponses.add(pecMailResponse);
|
|
}
|
|
}
|
|
return pecMailResponses;
|
|
}
|
|
|
|
private List<EmailLogEntity> getEmailLogEntities(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));
|
|
}
|
|
validator.validateHubId(request, userActionEntity.getHubId());
|
|
|
|
List<EmailLogEntity> emailLogs = emailLogRepository.findByUserActionIdAndSendStatusAndIsDeletedFalse(userActionId,StatusTypeEnum.PENDING.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));
|
|
}
|
|
return emailLogs;
|
|
}
|
|
|
|
public List<PecEmailLogResponse> getEmailLogByUserActionId(HttpServletRequest request,Long userActionId){
|
|
List<EmailLogEntity> emailLogs = getEmailLogEntities(request, userActionId);
|
|
ApplicationEntity applicationEntity=applicationService.validateApplication(emailLogs.get(0).getApplicationId());
|
|
String callName=applicationEntity.getCall().getName();
|
|
List<PecEmailLogResponse> pecEmailLogResponses=new ArrayList<>();
|
|
for(EmailLogEntity emailLogEntity:emailLogs) {
|
|
PecEmailLogResponse pecEmailLogResponse = createPecEmailLogResponse(userActionId, emailLogEntity, callName);
|
|
pecEmailLogResponses.add(pecEmailLogResponse);
|
|
}
|
|
return pecEmailLogResponses;
|
|
}
|
|
|
|
private PecEmailLogResponse createPecEmailLogResponse(Long userActionId, EmailLogEntity emailLogEntity, String callName) {
|
|
PecEmailLogResponse pecEmailLogResponse = new PecEmailLogResponse();
|
|
pecEmailLogResponse.setId(emailLogEntity.getId());
|
|
pecEmailLogResponse.setUserActionId(userActionId);
|
|
pecEmailLogResponse.setUserId(emailLogEntity.getUserId());
|
|
pecEmailLogResponse.setApplicationId(emailLogEntity.getApplicationId());
|
|
pecEmailLogResponse.setCallName(callName);
|
|
EmailLogResponse emailLogResponse=new EmailLogResponse();
|
|
emailLogResponse.setRecipientId(emailLogEntity.getRecipientId());
|
|
emailLogResponse.setRecipientType(emailLogEntity.getRecipientType());
|
|
pecEmailLogResponse.setEmailLogs(emailLogResponse);
|
|
pecEmailLogResponse.setType(emailLogEntity.getEmailType());
|
|
pecEmailLogResponse.setSubject(emailLogEntity.getEmailSubject());
|
|
pecEmailLogResponse.setHtmlContent(emailLogEntity.getEmailBody());
|
|
pecEmailLogResponse.setCallId(emailLogEntity.getCallId());
|
|
return pecEmailLogResponse;
|
|
}
|
|
private PecMailResponse createPecMailResponse(Long userActionId, EmailLogEntity emailLogEntity, String callName) {
|
|
PecMailResponse pecMailResponse = new PecMailResponse();
|
|
pecMailResponse.setId(emailLogEntity.getId());
|
|
pecMailResponse.setUserActionId(userActionId);
|
|
pecMailResponse.setUserId(emailLogEntity.getUserId());
|
|
pecMailResponse.setApplicationId(emailLogEntity.getApplicationId());
|
|
pecMailResponse.setCallName(callName);
|
|
pecMailResponse.setType(emailLogEntity.getEmailType());
|
|
pecMailResponse.setSubject(emailLogEntity.getEmailSubject());
|
|
pecMailResponse.setHtmlContent(emailLogEntity.getEmailBody());
|
|
pecMailResponse.setCallId(emailLogEntity.getCallId());
|
|
return pecMailResponse;
|
|
}
|
|
public List<PecMailResponse> getAllEmailLogs(HttpServletRequest request) {
|
|
List<EmailLogEntity> emailLogs=emailLogRepository.findPendingPECEmailLogs();
|
|
List<PecMailResponse> pecMailResponses=new ArrayList<>();
|
|
for (EmailLogEntity emailLogEntity: emailLogs){
|
|
ApplicationEntity applicationEntity=applicationService.validateApplication(emailLogEntity.getApplicationId());
|
|
String callName=applicationEntity.getCall().getName();
|
|
PecMailResponse pecMailResponse=createPecMailResponse(emailLogEntity.getUserAction().getId(),emailLogEntity,callName);
|
|
pecMailResponses.add(pecMailResponse);
|
|
}
|
|
return pecMailResponses;
|
|
}
|
|
}
|