Add new method that checks all Assigned Habits whether they are not expired.
@@ -5,6 +5,7 @@ import greencity.constant.CacheConstants;
import greencity.dto.user.UserVO;
import greencity.entity.HabitFactTranslation;
import greencity.entity.User;
+import greencity.enums.HabitAssignStatus;
import greencity.message.SendHabitNotification;
import greencity.repository.HabitAssignRepo;
import greencity.repository.HabitFactTranslationRepo;
@@ -20,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
import static greencity.enums.EmailNotification.*;
import static greencity.enums.FactOfDayStatus.*;
+import static java.time.temporal.ChronoUnit.DAYS;
/**
* Config for scheduling.
@@ -139,4 +141,19 @@ public class ScheduleConfig {
public void scheduledDeleteRatingStatisticsOlderThan() {
ratingStatisticsRepo.scheduledDeleteOlderThan();
}
+ /**
+ * Every day at 00:00 checks all Assigned Habits whether they are not expired.
+ *
+ * @author Ostap Mykhaylivskii
+ **/
+ @Transactional
+ @Scheduled(cron = "0 0q 0 * * ?", zone = "Europe/Kiev")
+ public void checkExpired() {
+ habitAssignRepo.findAll().stream().forEach(h -> {
+ if (DAYS.between(h.getCreateDate(), ZonedDateTime.now()) > h.getDuration()) {
+ h.setStatus(HabitAssignStatus.EXPIRED);
+ habitAssignRepo.save(h);
+ }
+ });
+ }
}
