diff --git a/service/src/main/java/greencity/service/UserServiceImpl.java b/service/src/main/java/greencity/service/UserServiceImpl.java index ec526c49c..7da07c27d 100644 --- a/service/src/main/java/greencity/service/UserServiceImpl.java +++ b/service/src/main/java/greencity/service/UserServiceImpl.java @@ -83,7 +83,6 @@ import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -601,18 +600,18 @@ public String saveUserProfile(UserProfileDtoRequest userProfileDtoRequest, Strin user.setShowLocation(userProfileDtoRequest.getShowLocation()); user.setShowEcoPlace(userProfileDtoRequest.getShowEcoPlace()); user.setShowShoppingList(userProfileDtoRequest.getShowShoppingList()); - userNotificationPreferenceRepository.deleteAllByUserId(user.getId()); - Set newPreferences = new HashSet<>(); if (Objects.nonNull(userProfileDtoRequest.getEmailPreferences())) { - newPreferences = userProfileDtoRequest.getEmailPreferences().stream() + Set newPreferences = userProfileDtoRequest.getEmailPreferences().stream() .map(type -> { UserNotificationPreference preference = new UserNotificationPreference(); preference.setUser(user); preference.setEmailPreference(type); return preference; }).collect(Collectors.toSet()); + Set currentPreferences = user.getNotificationPreferences(); + currentPreferences.clear(); + currentPreferences.addAll(newPreferences); } - user.setNotificationPreferences(newPreferences); userRepo.save(user); return UpdateConstants.getResultByLanguageCode(user.getLanguage().getCode()); } diff --git a/service/src/test/java/greencity/service/UserServiceImplTest.java b/service/src/test/java/greencity/service/UserServiceImplTest.java index 3f597ba33..4bfca21e2 100644 --- a/service/src/test/java/greencity/service/UserServiceImplTest.java +++ b/service/src/test/java/greencity/service/UserServiceImplTest.java @@ -84,12 +84,13 @@ import org.springframework.test.util.ReflectionTestUtils; import java.sql.Timestamp; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.Stream; import static greencity.ModelUtils.CREATE_USER_ALL_FRIENDS_DTO; @@ -99,11 +100,6 @@ import static greencity.ModelUtils.getLanguage; import static greencity.ModelUtils.getUser; import static greencity.ModelUtils.getUserLocation; -import static greencity.enums.Role.ROLE_ADMIN; -import static greencity.enums.Role.ROLE_MODERATOR; -import static greencity.enums.Role.ROLE_USER; -import static greencity.enums.UserStatus.ACTIVATED; -import static greencity.enums.UserStatus.DEACTIVATED; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -616,6 +612,7 @@ void findUserByName() { void saveUserProfileTest() { var request = ModelUtils.getUserProfileDtoRequest(); var user = ModelUtils.getUserWithSocialNetworks(); + user.setNotificationPreferences(new HashSet<>()); when(userRepo.findByEmail("test@gmail.com")).thenReturn(Optional.of(user)); when(userRepo.save(user)).thenReturn(user); when(googleApiService.getLocationByCoordinates(