Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug response 500 when user update profile #397

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions service/src/main/java/greencity/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<UserNotificationPreference> newPreferences = new HashSet<>();
if (Objects.nonNull(userProfileDtoRequest.getEmailPreferences())) {
newPreferences = userProfileDtoRequest.getEmailPreferences().stream()
Set<UserNotificationPreference> newPreferences = userProfileDtoRequest.getEmailPreferences().stream()
.map(type -> {
UserNotificationPreference preference = new UserNotificationPreference();
preference.setUser(user);
preference.setEmailPreference(type);
return preference;
}).collect(Collectors.toSet());
Set<UserNotificationPreference> currentPreferences = user.getNotificationPreferences();
currentPreferences.clear();
currentPreferences.addAll(newPreferences);
}
user.setNotificationPreferences(newPreferences);
userRepo.save(user);
return UpdateConstants.getResultByLanguageCode(user.getLanguage().getCode());
}
Expand Down
13 changes: 5 additions & 8 deletions service/src/test/java/greencity/service/UserServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Loading