Skip to content

Commit

Permalink
feat: 알림 동의 내역 업데이트 로직 수정(#66)
Browse files Browse the repository at this point in the history
- 요청 DTO를 통해 알림 타입별 동의 여부를 Map 형태로 치환
- 이 Map을 기반으로 조회된 알림 내역들, 타입별로 동의 여부 업데이트
  • Loading branch information
Minjae-An committed May 10, 2024
1 parent c18ca5f commit ed86b24
Showing 1 changed file with 11 additions and 44 deletions.
55 changes: 11 additions & 44 deletions be/src/main/java/yeonba/be/mypage/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

import java.time.LocalDate;
import java.time.Period;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -34,7 +31,6 @@
import yeonba.be.notification.dto.response.NotificationPermissionsResponse;
import yeonba.be.notification.entity.NotificationPermission;
import yeonba.be.notification.enums.NotificationType;
import yeonba.be.notification.repository.NotificationPermissionCommand;
import yeonba.be.notification.repository.NotificationPermissionQuery;
import yeonba.be.user.entity.Animal;
import yeonba.be.user.entity.Area;
Expand Down Expand Up @@ -63,7 +59,6 @@ public class MyPageService {
private final VocalRangeQuery vocalRangeQuery;
private final NotificationPermissionQuery notificationPermissionQuery;

private final NotificationPermissionCommand notificationPermissionCommand;
private final BlockCommand blockCommand;

private final S3Client s3Client;
Expand Down Expand Up @@ -315,44 +310,16 @@ public void updateNotificationPermissions(long userId, UserAllowNotificationsReq
List<NotificationPermission> notificationPermissions =
notificationPermissionQuery.findAllByUser(user);

// 알림 타입, 알림 동의 내역 Map 구성
Map<NotificationType, NotificationPermission> typePermissionMap =
notificationPermissions.stream()
.collect(Collectors.toMap(NotificationPermission::getType, Function.identity()));

// 알림 타입, 알림 동의 상태 Map 구성
Map<NotificationType, Boolean> typePermissonStatusMap = new HashMap<>();
typePermissonStatusMap.put(ARROW_RECEIVED, request.isAllowArrowReceivedNotification());
typePermissonStatusMap.put(CHATTING_REQUESTED,
request.isAllowChattingRequestNotification());
typePermissonStatusMap.put(
CHATTING_REQUEST_ACCEPTED, request.isAllowChattingRequestAcceptedNotification());

// 알림 타입별 내역 수정 or 내역 생성 작업 수행
typePermissonStatusMap.forEach((type, permissionStatus) ->
updateOrCreateNotificationPermissionBy(typePermissionMap, user, type, permissionStatus)
);
}

private void updateOrCreateNotificationPermissionBy(
Map<NotificationType, NotificationPermission> typePermissonMap,
User user,
NotificationType type,
boolean permissionStatus) {

Optional<NotificationPermission> foundNotificationPermission =
Optional.ofNullable(typePermissonMap.get(type));

// 동의 내역이 존재할 경우 동의 상태 업데이트
if (foundNotificationPermission.isPresent()) {
foundNotificationPermission.get().updatePermissionStatus(permissionStatus);

return;
}

// 동의 내역이 존재하지 않을 시, 생성 후 저장
NotificationPermission notificationPermission =
new NotificationPermission(permissionStatus, type, user);
notificationPermissionCommand.save(notificationPermission);
// 알림 타입, 동의 여부 맵 구성
Map<NotificationType, Boolean> typePermissionStatusMap =
request.toNotificationTypePermissionStatusMap();

// 알림 타입별 동의 내역, 동의 여부 업데이트
notificationPermissions.forEach(notificationPermission -> {
NotificationType type = notificationPermission.getType();
boolean permissionStatus = typePermissionStatusMap
.getOrDefault(type, notificationPermission.getPermissionStatus());
notificationPermission.updatePermissionStatus(permissionStatus);
});
}
}

0 comments on commit ed86b24

Please sign in to comment.