Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
1.6 kB
1
Indexable
Never
package com.capgemini.wsb.fitnesstracker.notification;

import com.capgemini.wsb.fitnesstracker.mail.api.EmailDto;
import com.capgemini.wsb.fitnesstracker.mail.api.EmailSender;
import com.capgemini.wsb.fitnesstracker.mail.internal.EmailService;
import com.capgemini.wsb.fitnesstracker.training.api.Training;
import com.capgemini.wsb.fitnesstracker.training.api.TrainingProvider;
import com.capgemini.wsb.fitnesstracker.user.api.User;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
@EnableScheduling
public class TrainingReportGenerator {

    private final TrainingProvider trainingProvider;
    
    private final EmailSender emailSender;


    @Scheduled(cron = "0 0 * * 1")
    public void generateTrainingReport() {
        List<Training> trainigList = trainingProvider.getAllTrainings();
        Map<User, List<Training>> userTrainings = trainigList.stream().collect(Collectors.groupingBy(Training::getUser));
        userTrainings.forEach((user, trainings) -> {
            int numberOfTrainings = trainings.size();
            String result = "Dzien dobry, " + user.getFirstName() + " " + user.getLastName() + "\n";
            result += "Oto twoje podsumowanie:\n";
            emailSender.send(new EmailDto(user.getEmail(), "Raport tygodniowy", result));
        });
    }



}
Leave a Comment