1 | package it.finki.charitable.util;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.AppUser;
|
---|
4 | import it.finki.charitable.entities.DonationPost;
|
---|
5 | import it.finki.charitable.entities.EmailMessage;
|
---|
6 | import it.finki.charitable.security.ConfirmationToken;
|
---|
7 | import it.finki.charitable.services.*;
|
---|
8 | import org.springframework.mail.SimpleMailMessage;
|
---|
9 | import org.springframework.scheduling.annotation.Scheduled;
|
---|
10 | import org.springframework.stereotype.Component;
|
---|
11 |
|
---|
12 | import java.time.Duration;
|
---|
13 | import java.time.LocalDate;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.stream.Collectors;
|
---|
16 |
|
---|
17 | @Component
|
---|
18 | public class AutomaticEvents {
|
---|
19 |
|
---|
20 | private final UserService userService;
|
---|
21 | private final ConfirmationTokenService confirmationTokenService;
|
---|
22 | private final DonationPostService donationPostService;
|
---|
23 | private final EmailMessageService emailMessageService;
|
---|
24 | private final EmailService emailService;
|
---|
25 |
|
---|
26 | public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService, DonationPostService donationPostService, EmailMessageService emailMessageService, EmailService emailService) {
|
---|
27 | this.userService = userService;
|
---|
28 | this.confirmationTokenService = confirmationTokenService;
|
---|
29 | this.donationPostService = donationPostService;
|
---|
30 | this.emailMessageService = emailMessageService;
|
---|
31 | this.emailService = emailService;
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Scheduled(cron = "0 0 0 * * *")
|
---|
35 | public void deleteNonEnabledUsers() {
|
---|
36 | List<ConfirmationToken> toDelete = confirmationTokenService.getAll();
|
---|
37 | for(ConfirmationToken token : toDelete) {
|
---|
38 | AppUser user = token.getUser();
|
---|
39 | confirmationTokenService.deleteConfirmationToken(token);
|
---|
40 | if(!user.getEnabled()) {
|
---|
41 | userService.deleteUser(user);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Scheduled(cron = "0 0 0 * * *")
|
---|
47 | public void setRisk() {
|
---|
48 | System.out.println("cron");
|
---|
49 | List<DonationPost> donationPosts = donationPostService.findAll();
|
---|
50 | donationPosts = donationPosts.stream().filter(post -> {
|
---|
51 | long daysToEnd = Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
|
---|
52 | long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
|
---|
53 | System.out.println(daysToEnd + " " + totalDays);
|
---|
54 |
|
---|
55 | if(totalDays < 10)
|
---|
56 | return true;
|
---|
57 |
|
---|
58 | return (daysToEnd * 1f/totalDays) * 100 < 75;
|
---|
59 | }).collect(Collectors.toList());
|
---|
60 |
|
---|
61 | donationPosts.forEach(post -> {
|
---|
62 | float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays() + 1);
|
---|
63 | float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1);
|
---|
64 |
|
---|
65 | System.out.println(dailyAverage + " " + neededAverage);
|
---|
66 | int risk = (int) (dailyAverage / neededAverage * 100);
|
---|
67 |
|
---|
68 | if(risk > 100) {
|
---|
69 | risk = 100;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) {
|
---|
73 | risk = 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if(post.getFundsNeeded() <= post.getTotalFundsCollected()) {
|
---|
77 | risk = 101;
|
---|
78 | }
|
---|
79 |
|
---|
80 | post.setRiskFactor(risk);
|
---|
81 | donationPostService.save(post);
|
---|
82 | });
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Scheduled(cron = "0 0 * * * *")
|
---|
86 | public void sendMessages() {
|
---|
87 | List<EmailMessage> messages = emailMessageService.findAll();
|
---|
88 | for(EmailMessage message: messages) {
|
---|
89 | SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
|
---|
90 | simpleMailMessage.setTo(message.getSendTo());
|
---|
91 | simpleMailMessage.setSubject(message.getSubject());
|
---|
92 | simpleMailMessage.setText(message.getText());
|
---|
93 | emailService.sendMail(simpleMailMessage);
|
---|
94 | emailMessageService.delete(message);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|