Skip to content

Commit

Permalink
refactor: 알림 API 모두 Notification Category 추가 #207
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbonai06 committed Sep 8, 2022
1 parent 1e9ed15 commit dbbda9d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ceos.bankids.config.CommonResponse;
import com.ceos.bankids.constant.ChallengeStatus;
import com.ceos.bankids.constant.ErrorCode;
import com.ceos.bankids.constant.NotificationCategory;
import com.ceos.bankids.domain.Challenge;
import com.ceos.bankids.domain.ChallengeUser;
import com.ceos.bankids.domain.FamilyUser;
Expand Down Expand Up @@ -56,16 +57,17 @@ public CommonResponse<String> allSendNotification(

String title = allSendNotificationRequest.getTitle();
String message = allSendNotificationRequest.getMessage();
NotificationCategory notificationCategory = NotificationCategory.NOTICE;
userRepository.findAll().stream()
.filter(user -> user.getExpoToken() != null && !Objects.equals(user.getExpoToken(),
"web"))
.forEach(user -> {
if (user.getNoticeOptIn()) {
expoNotificationService.sendMessage(user, title, message,
allSendNotificationRequest.getNewMap());
allSendNotificationRequest.getNewMap(), notificationCategory);
} else {
Notification notification = Notification.builder().user(user).title(title)
.message(message).build();
.message(message).notificationCategory(notificationCategory).build();
notificationRepository.save(notification);
}
});
Expand Down Expand Up @@ -109,9 +111,12 @@ public void notification(Challenge challenge, User authUser) {
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("challengeId", challenge.getId());
newMap.put("userId", authUser.getId());
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.CHALLENGE;
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("유저 {}의 돈길 {}의 {} 상태변경 알림", authUser.getId(), challenge.getId(),
challenge.getChallengeStatus());
Expand All @@ -125,9 +130,12 @@ public void userLevelUpMinusOne(User authUser) {
String notificationBody = "레벨업하기까지 \uD83D\uDD381 개\uD83D\uDD38의 돈길만 완주하면 돼요";
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("userId", authUser.getId());
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.LEVEL;
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("유저 id = {}의 레벨업 직전 알림", authUser.getId());
}
Expand All @@ -141,9 +149,12 @@ public void userLevelUpHalf(User authUser) {

HashMap<String, Object> newMap = new HashMap<>();
newMap.put("userId", authUser.getId());
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.LEVEL;
Boolean checkServiceOptIn = checkServiceOptIn(authUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(authUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("유저 id = {}의 레벨업 절반 달성 알림", authUser.getId());
}
Expand All @@ -158,9 +169,12 @@ public void createPendingChallengeNotification(User contractUser, ChallengeUser
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.CHALLENGE;
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("부모 유저 id = {}에게 유저 id = {} 돈길 id = {} 의 돈길 제안", contractUser.getId(),
challengeUser.getUser().getId(), challengeUser.getChallenge().getId());
Expand All @@ -176,9 +190,12 @@ public void runProgressNotification(User contractUser, ChallengeUser challengeUs
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.CHALLENGE;
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 돈길 id = {} 돈길 걷기 알림 전송", contractUser.getId(),
challengeUser.getUser().getId(), challengeUser.getChallenge().getId());
Expand All @@ -195,9 +212,12 @@ public void achieveChallengeNotification(User contractUser, ChallengeUser challe
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.CHALLENGE;
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 돈길 id = {} 돈길 완주 알림 전송", contractUser.getId(),
challengeUser.getUser().getId(), challengeUser.getChallenge().getId());
Expand All @@ -212,9 +232,12 @@ public void kidLevelUpNotification(User contractUser, User user, Long level, Lon
user.getUsername() + "님이 레벨" + level + "에서 레벨" + afterLevel + "로 올랐어요! 확인해볼까요?";
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", user.getId());
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.LEVEL;
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 레벨업 알림 전송", contractUser.getId(), user.getId());
}
Expand All @@ -228,9 +251,12 @@ public void challengeFailedNotification(User contractUser, ChallengeUser challen
HashMap<String, Object> newMap = new HashMap<>();
newMap.put("user", challengeUser.getUser().getId());
newMap.put("challenge", challengeUser.getChallenge().getId());
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody);
NotificationCategory notificationCategory = NotificationCategory.CHALLENGE;
Boolean checkServiceOptIn = checkServiceOptIn(contractUser, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap);
expoNotificationService.sendMessage(contractUser, title, notificationBody, newMap,
notificationCategory);
}
log.info("부모 유저 id = {}에게 유저 id = {}의 돈길 id = {} 돈길 실패 알림 전송", contractUser.getId(),
challengeUser.getChallenge().getId(), challengeUser.getChallenge().getId());
Expand All @@ -242,21 +268,26 @@ public void newFamilyUserNotification(User newFamilyUser, List<FamilyUser> famil
String title = "가족그룹\uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66에 새로 참여했어요";
String notificationBody = "누가 가족그룹에 참여했는지 확인해요!\uD83D\uDCAB";
HashMap<String, Object> newMap = new HashMap<>();
NotificationCategory notificationCategory = NotificationCategory.FAMILY;
// newMap.put("user", newFamilyUser.getId());
familyUserList.forEach(familyUser -> {
User user = familyUser.getUser();
Boolean checkServiceOptIn = checkServiceOptIn(user, title, notificationBody);
Boolean checkServiceOptIn = checkServiceOptIn(user, title, notificationBody,
notificationCategory);
if (checkServiceOptIn) {
expoNotificationService.sendMessage(user, title, notificationBody, newMap);
expoNotificationService.sendMessage(user, title, notificationBody, newMap,
notificationCategory);
}
log.info("기존 가족 구성원 id = {}에게 유저 id = {}의 가족 참여 알림 전송", familyUser.getUser().getId(),
newFamilyUser.getId());
});
}

private Boolean checkServiceOptIn(User user, String title, String body) {
private Boolean checkServiceOptIn(User user, String title, String body,
NotificationCategory notificationCategory) {
if (!user.getServiceOptIn()) {
Notification notification = Notification.builder().user(user).title(title).message(body)
.notificationCategory(notificationCategory)
.build();
notificationRepository.save(notification);
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ceos.bankids.service;

import com.ceos.bankids.constant.ErrorCode;
import com.ceos.bankids.constant.NotificationCategory;
import com.ceos.bankids.domain.Notification;
import com.ceos.bankids.domain.User;
import com.ceos.bankids.dto.NotificationDTO;
Expand Down Expand Up @@ -91,7 +92,8 @@ public void deleteAllNotification(User user) {
notificationRepository.deleteAllByUserId(user.getId());
}

public void sendMessage(User user, String title, String body, Map<String, Object> data) {
public void sendMessage(User user, String title, String body, Map<String, Object> data,
NotificationCategory notificationCategory) {

String token = user.getExpoToken();
if (token == null) {
Expand Down Expand Up @@ -123,7 +125,7 @@ public void sendMessage(User user, String title, String body, Map<String, Object
}
//Todo 메서드 인자가 user로 바뀌면 데이터 베이스에 꽂기
Notification notification = Notification.builder().title(title).message(body).user(user)
.build();
.notificationCategory(notificationCategory).build();
notificationRepository.save(notification);
List<ExpoPushTicket> allTickets = new ArrayList<>();
for (CompletableFuture<List<ExpoPushTicket>> messageReplyFuture : messageRepliesFutures) {
Expand Down

0 comments on commit dbbda9d

Please sign in to comment.