Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.2 kB
3
Indexable
Never
    public MonthlyUserReportDto getMonthlyReportByUser(Long employeeId, String year, String month) {
        User user = findUserById(employeeId);
        Employee employee = employeeRepository.findByEmail(user.getEmail());
        List<Project> projects = projectRepository.findByMembers_Email(user.getEmail());
        List<ProjectInfo> projectInfoList = generateProjectInfoList(year, month, user.getEmail(), projects);
        return buildMonthlyReportDto(user, employee, projectInfoList, year, month);
    }

    private User findUserById(Long id) {
        return userRepository.findById(id)
                .orElseThrow(() -> new NoSuchElementException("Kullanıcı bulunamadı."));
    }

    private List<ProjectInfo> generateProjectInfoList(String year, String month, String userEmail, List<Project> projects) {
        return projects.stream().map(project -> {
            List<TimeTrack> timeTrackList = timeTrackRepository.findByPeriodYearAndPeriodMonthAndUser_EmailAndProject_Name(year, month, userEmail, project.getName());
            List<TimeTrackInfo> timeTrackInfoList = timeTrackList.stream().map(this::mapTimeTrackToInfo).collect(Collectors.toList());
            return ProjectInfo.builder().projectName(project.getName()).timeTrackList(timeTrackInfoList).build();
        }).collect(Collectors.toList());
    }

    private TimeTrackInfo mapTimeTrackToInfo(TimeTrack timeTrack) {
        return TimeTrackInfo.builder()
                .date(timeTrack.getDate())
                .description(timeTrack.getDescription())
                .workHours(timeTrack.getWorkHours())
                .overTime(timeTrack.getOverTime())
                .build();
    }

    private MonthlyUserReportDto buildMonthlyReportDto(User user, Employee employee, List<ProjectInfo> projectInfoList, String year, String month) {
        return MonthlyUserReportDto.builder()
                .userId(user.getId())
                .userName(employee.getFirstName())
                .userSurname(employee.getLastName())
                .projectInfoList(projectInfoList)
                .periodMonth(month)
                .periodYear(year)
                .build();
    }
}