diff --git a/src/main/java/com/yello/server/domain/admin/controller/AdminController.java b/src/main/java/com/yello/server/domain/admin/controller/AdminController.java index 54f81d07..e6ba2ccb 100644 --- a/src/main/java/com/yello/server/domain/admin/controller/AdminController.java +++ b/src/main/java/com/yello/server/domain/admin/controller/AdminController.java @@ -28,6 +28,7 @@ import com.yello.server.global.common.annotation.AccessTokenUser; import com.yello.server.global.common.dto.BaseResponse; import com.yello.server.global.common.dto.EmptyObject; +import com.yello.server.infrastructure.firebase.dto.request.NotificationCustomMessage; import com.yello.server.infrastructure.firebase.service.NotificationService; import javax.annotation.Nullable; import lombok.RequiredArgsConstructor; @@ -56,7 +57,8 @@ public BaseResponse postAdminLogin(@RequestBody AdminLoginRe } @GetMapping("/user") - public BaseResponse getUserAdmin(@AccessTokenUser User user, @RequestParam Integer page, + public BaseResponse getUserAdmin(@AccessTokenUser User user, + @RequestParam Integer page, @Nullable @RequestParam String field, @Nullable @RequestParam String value) { val data = (field == null && value == null) @@ -67,7 +69,8 @@ public BaseResponse getUserAdmin(@AccessTokenUser User user, } @GetMapping("/user/{id}") - public BaseResponse getUserDetailAdmin(@AccessTokenUser User user, @PathVariable Long id) { + public BaseResponse getUserDetailAdmin(@AccessTokenUser User user, + @PathVariable Long id) { val data = adminService.findUserDetail(user.getId(), id); return BaseResponse.success(READ_USER_DETAIL_ADMIN_SUCCESS, data); } @@ -86,7 +89,8 @@ public BaseResponse deleteUser(@AccessTokenUser User user, @RequestParam Long us } @GetMapping("/cooldown") - public BaseResponse getCooldownAdmin(@AccessTokenUser User user, @RequestParam Integer page, + public BaseResponse getCooldownAdmin(@AccessTokenUser User user, + @RequestParam Integer page, @Nullable @RequestParam String yelloId) { val data = yelloId == null ? adminService.findCooldown(user.getId(), createPageableLimitTen(page)) @@ -109,7 +113,8 @@ public BaseResponse getQuestionAdmin(@AccessTokenUser Use } @GetMapping("/question/{id}") - public BaseResponse getQuestionDetailAdmin(@AccessTokenUser User user, + public BaseResponse getQuestionDetailAdmin( + @AccessTokenUser User user, @PathVariable Long id) { val data = adminService.findQuestionDetail(user.getId(), id); return BaseResponse.success(READ_QUESTION_DETAIL_ADMIN_SUCCESS, data); @@ -129,4 +134,12 @@ public BaseResponse deleteQuestion(@AccessTokenUser User user, @RequestParam Lon adminService.deleteQuestion(user.getId(), questionId); return BaseResponse.success(DELETE_QUESTION_ADMIN_SUCCESS); } + + @PostMapping("/notification") + public BaseResponse postCustomNotificationSendAdmin(@AccessTokenUser User user, + @RequestBody NotificationCustomMessage request) { + val data = notificationService.adminSendCustomNotification(user.getId(), request); + + return BaseResponse.success(CREATE_VOTE_SUCCESS, data); + } } diff --git a/src/main/java/com/yello/server/domain/admin/service/AdminService.java b/src/main/java/com/yello/server/domain/admin/service/AdminService.java index 3e6e8db3..5f07c431 100644 --- a/src/main/java/com/yello/server/domain/admin/service/AdminService.java +++ b/src/main/java/com/yello/server/domain/admin/service/AdminService.java @@ -88,7 +88,8 @@ public AdminUserResponse findUser(Long adminId, Pageable page) { return AdminUserResponse.of(totalCount, list); } - public AdminUserResponse findUserContaining(Long adminId, Pageable page, String field, String value) { + public AdminUserResponse findUserContaining(Long adminId, Pageable page, String field, + String value) { // exception final User admin = userRepository.getById(adminId); userAdminRepository.getByUser(admin); @@ -185,16 +186,18 @@ public AdminCooldownResponse findCooldown(Long adminId, Pageable page) { return AdminCooldownResponse.of(totalCount, list); } - public AdminCooldownResponse findCooldownContaining(Long adminId, Pageable page, String yelloId) { + public AdminCooldownResponse findCooldownContaining(Long adminId, Pageable page, + String yelloId) { // exception final User admin = userRepository.getById(adminId); userAdminRepository.getByUser(admin); // logic final Long totalCount = cooldownRepository.countAllByYelloIdContaining(yelloId); - final List list = cooldownRepository.findAllContaining(page, yelloId).stream() - .map(AdminCooldownContentVO::of) - .toList(); + final List list = + cooldownRepository.findAllContaining(page, yelloId).stream() + .map(AdminCooldownContentVO::of) + .toList(); return AdminCooldownResponse.of(totalCount, list); } @@ -245,8 +248,10 @@ public List createVote(Long adminId, Long questionId, AdminQuestionVoteReq // logic final List result = new ArrayList<>(); request.voteContentList().forEach((voteContent -> { - final Optional sender = userRepository.findByIdNotFiltered(voteContent.senderId()); - final Optional receiver = userRepository.findByIdNotFiltered(voteContent.receiverId()); + final Optional sender = + userRepository.findByIdNotFiltered(voteContent.senderId()); + final Optional receiver = + userRepository.findByIdNotFiltered(voteContent.receiverId()); if (sender.isPresent() && receiver.isPresent()) { final Vote vote = Vote.createVote( diff --git a/src/main/java/com/yello/server/domain/vote/service/VoteService.java b/src/main/java/com/yello/server/domain/vote/service/VoteService.java index 8ba3c12c..9e7b591e 100644 --- a/src/main/java/com/yello/server/domain/vote/service/VoteService.java +++ b/src/main/java/com/yello/server/domain/vote/service/VoteService.java @@ -136,6 +136,7 @@ public List findVoteQuestionList(Long userId) { } return true; }) + .filter(question -> !question.getId().equals(102L)) .toList(); return voteManager.generateVoteQuestion(user, questions); } diff --git a/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationCustomMessage.java b/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationCustomMessage.java new file mode 100644 index 00000000..125f21e5 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationCustomMessage.java @@ -0,0 +1,12 @@ +package com.yello.server.infrastructure.firebase.dto.request; + +import java.util.List; + +public record NotificationCustomMessage( + List userIdList, + String title, + String message + +) { + +} diff --git a/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java b/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java index 1bdcdd73..07fb6d61 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/dto/request/NotificationMessage.java @@ -18,7 +18,8 @@ public record NotificationMessage( public static NotificationMessage toRecommendNotificationContent(User user) { return NotificationMessage.builder() .title("40분 대기 초기화 + 100포인트 적립") - .message(MessageFormat.format("{0}님이 나를 추천인으로 가입해 옐로하기 대기 초기화 + 100포인트가 적립되었어요!", user.getName())) + .message(MessageFormat.format("{0}님이 나를 추천인으로 가입해 옐로하기 대기 초기화 + 100포인트가 적립되었어요!", + user.getName())) .type(NotificationType.RECOMMEND) .build(); } @@ -42,7 +43,8 @@ public static NotificationMessage toFriendNotificationContent(User user) { public static NotificationMessage toYelloNotificationContent(Vote vote) { final User sender = vote.getSender(); - final String target = Gender.MALE.getIntial().equals(sender.getGender().getIntial()) ? "남학생" : "여학생"; + final String target = + Gender.MALE.getIntial().equals(sender.getGender().getIntial()) ? "남학생" : "여학생"; return NotificationMessage.builder() .title(MessageFormat.format("{0}이 쪽지를 보냈어요!", target)) .message(vote.getQuestion().toNotificationSentence()) @@ -50,6 +52,16 @@ public static NotificationMessage toYelloNotificationContent(Vote vote) { .build(); } + public static NotificationMessage toYelloNotificationCustomContent( + NotificationCustomMessage message) { + + return NotificationMessage.builder() + .title(message.title()) + .message(message.message()) + .type(NotificationType.NEW_VOTE) + .build(); + } + public Notification toNotification() { return Notification.builder() .setTitle(title) diff --git a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java index af282107..c92a36f6 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java @@ -1,10 +1,13 @@ package com.yello.server.infrastructure.firebase.service; import com.google.firebase.messaging.Message; +import com.yello.server.domain.admin.repository.UserAdminRepository; import com.yello.server.domain.friend.entity.Friend; import com.yello.server.domain.user.entity.User; import com.yello.server.domain.user.repository.UserRepository; import com.yello.server.domain.vote.entity.Vote; +import com.yello.server.global.common.dto.EmptyObject; +import com.yello.server.infrastructure.firebase.dto.request.NotificationCustomMessage; import com.yello.server.infrastructure.firebase.dto.request.NotificationMessage; import com.yello.server.infrastructure.firebase.manager.FCMManager; import com.yello.server.infrastructure.redis.repository.TokenRepository; @@ -23,13 +26,16 @@ public class NotificationFcmService implements NotificationService { private final UserRepository userRepository; private final TokenRepository tokenRepository; private final FCMManager fcmManager; + private final UserAdminRepository userAdminRepository; @Override public void sendRecommendNotification(User user, User target) { - NotificationMessage notificationMessage = NotificationMessage.toRecommendNotificationContent(user); + NotificationMessage notificationMessage = + NotificationMessage.toRecommendNotificationContent(user); if (target.getDeviceToken() != null && !Objects.equals(target.getDeviceToken(), "")) { - final Message message = fcmManager.createMessage(target.getDeviceToken(), notificationMessage); + final Message message = + fcmManager.createMessage(target.getDeviceToken(), notificationMessage); fcmManager.send(message); } } @@ -38,12 +44,14 @@ public void sendRecommendNotification(User user, User target) { public void sendYelloNotification(Vote vote) { final User receiver = vote.getReceiver(); - NotificationMessage notificationMessage = NotificationMessage.toYelloNotificationContent(vote); + NotificationMessage notificationMessage = + NotificationMessage.toYelloNotificationContent(vote); final String path = "/api/v1/vote/" + vote.getId().toString(); if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), "")) { - final Message message = fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage, path); + final Message message = + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage, path); fcmManager.send(message); } } @@ -53,10 +61,12 @@ public void sendFriendNotification(Friend friend) { final User receiver = friend.getTarget(); final User sender = friend.getUser(); - NotificationMessage notificationMessage = NotificationMessage.toFriendNotificationContent(sender); + NotificationMessage notificationMessage = + NotificationMessage.toFriendNotificationContent(sender); if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), "")) { - final Message message = fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); + final Message message = + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); fcmManager.send(message); } } @@ -65,12 +75,47 @@ public void sendFriendNotification(Friend friend) { public void sendVoteAvailableNotification(Long receiverId) { final User receiveUser = userRepository.getById(receiverId); - NotificationMessage notificationMessage = NotificationMessage.toVoteAvailableNotificationContent(); + NotificationMessage notificationMessage = + NotificationMessage.toVoteAvailableNotificationContent(); - if (receiveUser.getDeviceToken() != null && !Objects.equals(receiveUser.getDeviceToken(), "")) { - final Message message = fcmManager.createMessage(receiveUser.getDeviceToken(), notificationMessage); + if (receiveUser.getDeviceToken() != null && !Objects.equals(receiveUser.getDeviceToken(), + "")) { + final Message message = + fcmManager.createMessage(receiveUser.getDeviceToken(), notificationMessage); fcmManager.send(message); log.info("[rabbitmq] successfully send notification!"); } } + + @Override + public void sendCustomNotification(NotificationCustomMessage request) { + + request.userIdList().stream() + .forEach(userId -> { + final User receiver = userRepository.getById(userId); + + NotificationMessage notificationMessage = + NotificationMessage.toYelloNotificationCustomContent(request); + + if (receiver.getDeviceToken() != null && !Objects.equals(receiver.getDeviceToken(), + "")) { + final Message message = + fcmManager.createMessage(receiver.getDeviceToken(), notificationMessage); + fcmManager.send(message); + } + }); + + } + + @Override + public EmptyObject adminSendCustomNotification(Long adminId, NotificationCustomMessage request) { + // exception + final User admin = userRepository.getById(adminId); + userAdminRepository.getByUser(admin); + + // logic + this.sendCustomNotification(request); + + return EmptyObject.builder().build(); + } } diff --git a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java index d2e6fbf0..b0d8f687 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationService.java @@ -3,6 +3,8 @@ import com.yello.server.domain.friend.entity.Friend; import com.yello.server.domain.user.entity.User; import com.yello.server.domain.vote.entity.Vote; +import com.yello.server.global.common.dto.EmptyObject; +import com.yello.server.infrastructure.firebase.dto.request.NotificationCustomMessage; public interface NotificationService { @@ -13,4 +15,8 @@ public interface NotificationService { void sendFriendNotification(Friend friend); void sendVoteAvailableNotification(Long receiverId); + + void sendCustomNotification(NotificationCustomMessage request); + + EmptyObject adminSendCustomNotification(Long adminId, NotificationCustomMessage request); }