<commit_before>package info.fingo.urlopia.api.v2.preferences;

import info.fingo.urlopia.api.v2.preferences.working.hours.*;
import info.fingo.urlopia.api.v2.presence.PresenceConfirmation;
import info.fingo.urlopia.api.v2.presence.PresenceConfirmationRepository;
import info.fingo.urlopia.config.persistance.filter.Filter;
import info.fingo.urlopia.user.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserPreferencesService {
    private final UserWorkingHoursPreferenceRepository userWorkingHoursPreferenceRepository;
    private final PresenceConfirmationRepository presenceConfirmationRepository;
    private final UserService userService;
    private final ModelMapper modelMapper = new ModelMapper();

    public Map<Long, UserWorkingHoursPreference> getWorkingHoursPreference(Long authenticatedUserId) {
        var user = userService.get(authenticatedUserId);
        var filter = Filter.empty();

        if (!user.isAdmin()) {
            return Map.of(authenticatedUserId, getWorkingHoursPreferenceOf(authenticatedUserId));
        }

        return userWorkingHoursPreferenceRepository.findAll(filter).stream()
                .collect(Collectors.toMap(UserWorkingHoursPreference::getUserId, pref -> pref));
    }

    // if unspecified absence endpoint still be slow this is probably the place to change
    public UserWorkingHoursPreference getWorkingHoursPreferenceOf(Long userId) {
        var userPreferences = userWorkingHoursPreferenceRepository.findById(userId);
        return userPreferences.orElse(initUserPreferences(userId));
    }


    private UserWorkingHoursPreference initUserPreferences(Long userId){
        return userWorkingHoursPreferenceRepository.save(UserWorkingHoursPreference.getDefault(userId));
    }

    public UserWorkingHoursPreferenceDTO changeWorkingHoursPreference(Long authenticatedUserId,
                                                                      UserWorkingHoursPreferenceDTO dto) {
        var newPreference = userWorkingHoursPreferenceRepository.findById(authenticatedUserId)
                .map(preference -> {
                    fixPresenceConfirmations(preference);
                    return updateExistingPreference(preference, dto);
                })
                .orElseGet(() -> createNewPreferenceUsing(authenticatedUserId, dto));
        return UserWorkingHoursPreferenceDTO.from(userWorkingHoursPreferenceRepository.save(newPreference));<commit_msg>https://jira.fingo.info/browse/IJ-249 Work preferences stop saving (#140)<commit_after>package info.fingo.urlopia.api.v2.preferences;

import info.fingo.urlopia.api.v2.preferences.working.hours.*;
import info.fingo.urlopia.api.v2.presence.PresenceConfirmation;
import info.fingo.urlopia.api.v2.presence.PresenceConfirmationRepository;
import info.fingo.urlopia.config.persistance.filter.Filter;
import info.fingo.urlopia.user.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserPreferencesService {
    private final UserWorkingHoursPreferenceRepository userWorkingHoursPreferenceRepository;
    private final PresenceConfirmationRepository presenceConfirmationRepository;
    private final UserService userService;
    private final ModelMapper modelMapper = new ModelMapper();

    public Map<Long, UserWorkingHoursPreference> getWorkingHoursPreference(Long authenticatedUserId) {
        var user = userService.get(authenticatedUserId);
        var filter = Filter.empty();

        if (!user.isAdmin()) {
            return Map.of(authenticatedUserId, getWorkingHoursPreferenceOf(authenticatedUserId));
        }

        return userWorkingHoursPreferenceRepository.findAll(filter).stream()
                .collect(Collectors.toMap(UserWorkingHoursPreference::getUserId, pref -> pref));
    }

    // if unspecified absence endpoint still be slow this is probably the place to change
    public UserWorkingHoursPreference getWorkingHoursPreferenceOf(Long userId) {
        var userPreferences = userWorkingHoursPreferenceRepository.findById(userId);
        return userPreferences.orElseGet(() -> initUserPreferences(userId));
    }


    private UserWorkingHoursPreference initUserPreferences(Long userId){
        return userWorkingHoursPreferenceRepository.save(UserWorkingHoursPreference.getDefault(userId));
    }

    public UserWorkingHoursPreferenceDTO changeWorkingHoursPreference(Long authenticatedUserId,
                                                                      UserWorkingHoursPreferenceDTO dto) {
        var newPreference = userWorkingHoursPreferenceRepository.findById(authenticatedUserId)
                .map(preference -> {
                    fixPresenceConfirmations(preference);
                    return updateExistingPreference(preference, dto);
                })
                .orElseGet(() -> createNewPreferenceUsing(authenticatedUserId, dto));
        return UserWorkingHoursPreferenceDTO.from(userWorkingHoursPreferenceRepository.save(newPreference));
{'fraction_non_alphanumeric': 0.07632645866384628, 'fraction_numerical': 0.0035254715318173806, 'mean_word_length': 4.075134168157424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5147461', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1489, 'n_words': 259}