Done ticket GEPAFINBE-89
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package net.gepafin.tendermanagement.entities;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Table(name = "expiration_config")
|
||||
@Data
|
||||
public class ExpirationConfigEntity extends BaseEntity {
|
||||
|
||||
@Column(name="INTERVAL_DAYS")
|
||||
private Long intervalDays;
|
||||
|
||||
@Column(name="TYPE")
|
||||
private String type;
|
||||
|
||||
@Column(name="IS_DELETED")
|
||||
private Boolean isDeleted;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package net.gepafin.tendermanagement.enums;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum ExpirationTypeEnum {
|
||||
|
||||
AMENDMENT("AMENDMENT"),
|
||||
EVALUATION("EVALUATION");
|
||||
|
||||
private String value;
|
||||
|
||||
ExpirationTypeEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,9 @@ public enum NotificationTypeEnum {
|
||||
AMENDMENT_CLOSED("AMENDMENT_CLOSED"),
|
||||
NDG_GENERATION("NDG_GENERATION"),
|
||||
EVALUATION_CREATION("EVALUATION_CREATION"),
|
||||
EVALUATION_EXPIRED("EVALUATION_EXPIRED");
|
||||
EVALUATION_EXPIRED("EVALUATION_EXPIRED"),
|
||||
AMENDMENT_EXPIRATION_REMINDER("AMENDMENT_EXPIRATION_REMINDER"),
|
||||
EVALUATION_EXPIRATION_REMINDER("EVALUATION_EXPIRATION_REMINDER");
|
||||
|
||||
private final String value;
|
||||
|
||||
|
||||
@@ -74,4 +74,10 @@ public interface ApplicationAmendmentRequestRepository extends JpaRepository<App
|
||||
" AND activeAmendment.isDeleted = false) ")
|
||||
Set<ApplicationEvaluationEntity> findEvaluationsWithoutActiveAmendmentsByIds(@Param("applicationEvaluationIds") Set<Long> applicationEvaluationIds);
|
||||
|
||||
@Query("SELECT a FROM ApplicationAmendmentRequestEntity a " +
|
||||
"WHERE a.isDeleted = false " +
|
||||
"AND a.status NOT IN ('CLOSE', 'EXPIRED') " +
|
||||
"AND a.endDate BETWEEN :startTime AND :endTime")
|
||||
List<ApplicationAmendmentRequestEntity> findExpiringBetween(LocalDateTime startTime, LocalDateTime endTime);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.gepafin.tendermanagement.repositories;
|
||||
|
||||
import net.gepafin.tendermanagement.entities.ApplicationEntity;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationAmendmentRequestEntity;
|
||||
import net.gepafin.tendermanagement.entities.ApplicationEvaluationEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -31,4 +32,10 @@ public interface ApplicationEvaluationRepository extends JpaRepository<Applicati
|
||||
|
||||
@Query("SELECT a FROM ApplicationEvaluationEntity a WHERE a.isDeleted = false AND a.endDate < :currentDate")
|
||||
List<ApplicationEvaluationEntity> findAllByIsDeletedFalseAndEndDateBefore(@Param("currentDate") LocalDateTime currentDate);
|
||||
|
||||
@Query("SELECT a FROM ApplicationEvaluationEntity a " +
|
||||
"WHERE a.isDeleted = false " +
|
||||
"AND a.status NOT IN ('CLOSE', 'EXPIRED') " +
|
||||
"AND a.endDate BETWEEN :startTime AND :endTime")
|
||||
List<ApplicationEvaluationEntity> findExpiringBetween(LocalDateTime startTime, LocalDateTime endTime);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package net.gepafin.tendermanagement.repositories;
|
||||
|
||||
import net.gepafin.tendermanagement.entities.ExpirationConfigEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ExpirationConfigRepository extends JpaRepository<ExpirationConfigEntity, Long> {
|
||||
List<ExpirationConfigEntity> findByTypeAndIsDeletedFalse(String type);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package net.gepafin.tendermanagement.scheduler;
|
||||
|
||||
import net.gepafin.tendermanagement.dao.ApplicationAmendmentRequestDao;
|
||||
import net.gepafin.tendermanagement.dao.ApplicationEvaluationDao;
|
||||
import net.gepafin.tendermanagement.dao.NotificationDao;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.enums.ExpirationTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.NotificationTypeEnum;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationAmendmentRequestRepository;
|
||||
import net.gepafin.tendermanagement.repositories.ApplicationEvaluationRepository;
|
||||
import net.gepafin.tendermanagement.repositories.ExpirationConfigRepository;
|
||||
import net.gepafin.tendermanagement.service.ApplicationService;
|
||||
import net.gepafin.tendermanagement.service.CompanyService;
|
||||
import net.gepafin.tendermanagement.service.UserService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class ExpirationScheduler {
|
||||
|
||||
@Autowired
|
||||
private ApplicationAmendmentRequestRepository applicationAmendmentRequestRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
||||
|
||||
@Autowired
|
||||
private ExpirationConfigRepository expirationNotificationConfigRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEvaluationDao applicationEvaluationDao;
|
||||
|
||||
@Autowired
|
||||
private ApplicationAmendmentRequestDao applicationAmendmentRequestDao;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationService applicationService;
|
||||
|
||||
@Autowired
|
||||
private NotificationDao notificationDao;
|
||||
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ExpirationScheduler.class);
|
||||
|
||||
@Scheduled(cron = "0 0 3 * * ?")
|
||||
public void processExpiration(){
|
||||
log.info("Starting the Expiration scheduler...");
|
||||
try {
|
||||
Utils.setHttpServletRequestForScheduler();
|
||||
|
||||
log.info("Starting processing expiration notifications for Amendment");
|
||||
processExpiration(ExpirationTypeEnum.AMENDMENT);
|
||||
|
||||
log.info("Starting processing expiration notifications for Evaluation");
|
||||
processExpiration(ExpirationTypeEnum.EVALUATION);
|
||||
|
||||
log.info("Expiration scheduler completed successfully.");
|
||||
|
||||
}
|
||||
catch (Exception e){
|
||||
log.error("An error occurred during the Notification Expiration Scheduler: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void processExpiration(ExpirationTypeEnum type) {
|
||||
List<ExpirationConfigEntity> configEntities = expirationNotificationConfigRepository.findByTypeAndIsDeletedFalse(type.getValue());
|
||||
|
||||
for (ExpirationConfigEntity config : configEntities) {
|
||||
Long daysBefore = config.getIntervalDays();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime startDate = now.plusDays(daysBefore).withHour(0).withMinute(0).withSecond(0).withNano(0);
|
||||
LocalDateTime endDate = startDate.plusDays(1).minusNanos(1).withNano(0);
|
||||
|
||||
if (ExpirationTypeEnum.AMENDMENT.equals(type)) {
|
||||
processAmendmentExpiration(startDate, endDate, daysBefore);
|
||||
} else if (ExpirationTypeEnum.EVALUATION.equals(type)) {
|
||||
processEvaluationExpiration(startDate, endDate, daysBefore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processAmendmentExpiration(LocalDateTime startDate, LocalDateTime endDate, Long daysBefore) {
|
||||
List<ApplicationAmendmentRequestEntity> amendmentEntities = applicationAmendmentRequestRepository.findExpiringBetween(startDate, endDate);
|
||||
|
||||
for (ApplicationAmendmentRequestEntity entity : amendmentEntities) {
|
||||
ApplicationEntity application = entity.getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication();
|
||||
Map<String, String> placeHolders = replacePlaceholders(application,daysBefore);
|
||||
notificationDao.sendNotificationToInstructor(placeHolders,entity.getApplicationEvaluationEntity(), NotificationTypeEnum.AMENDMENT_EXPIRATION_REMINDER);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> replacePlaceholders (ApplicationEntity application, Long daysBefore){
|
||||
Long companyId = application.getCompanyId();
|
||||
CompanyEntity company = companyService.validateCompany(companyId);
|
||||
Map<String, String> placeHolders = new HashMap<>();
|
||||
placeHolders.put("{{call_name}}",application.getCall().getName());
|
||||
placeHolders.put("{{company_name}}", company.getCompanyName());
|
||||
placeHolders.put("{{days_before}}", daysBefore.toString());
|
||||
return placeHolders;
|
||||
}
|
||||
|
||||
private void processEvaluationExpiration(LocalDateTime startDate, LocalDateTime endDate, Long daysBefore) {
|
||||
List<ApplicationEvaluationEntity> evaluationEntities = applicationEvaluationRepository.findExpiringBetween(startDate, endDate);
|
||||
|
||||
for (ApplicationEvaluationEntity entity : evaluationEntities) {
|
||||
ApplicationEntity application = entity.getAssignedApplicationsEntity().getApplication();
|
||||
Map<String, String> placeHolders = replacePlaceholders(application,daysBefore);
|
||||
notificationDao.sendNotificationToInstructor(placeHolders,entity,NotificationTypeEnum.EVALUATION_EXPIRATION_REMINDER);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user