Skip to content

Commit

Permalink
Merge pull request #110 from Team-Shaka/fix/109
Browse files Browse the repository at this point in the history
🐛 Fix: Notification receiver 이슈 해결
  • Loading branch information
koojun99 authored Aug 28, 2024
2 parents 4bc923f + a50ac30 commit 2b514b2
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static Member toMember(User user, MemberRequestDTO.registerMember request
.bio(request.getBio())
.profileImageUrl(request.getProfileImageURL())
.treeHouse(treeHouse)
.notificationList(new ArrayList<>())
.commentList(new ArrayList<>())
.invitationList(new ArrayList<>())
.reactionList(new ArrayList<>())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package treehouse.server.api.member.implementation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import treehouse.server.api.member.persistence.MemberRepository;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.User.User;
Expand All @@ -9,6 +10,7 @@
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.MemberException;

@Slf4j
@Adapter
@RequiredArgsConstructor
public class MemberQueryAdapter {
Expand All @@ -20,6 +22,7 @@ public Member getMember(User user){
}

public Member findByUserAndTreehouse(User user, TreeHouse treehouse) {
log.info("user: {}, treehouse: {}", user.getId(), treehouse.getId());
return memberRepository.findByUserAndTreeHouse(user, treehouse).orElseThrow(() -> new MemberException(GlobalErrorCode.MEMBER_NOT_FOUND));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NotificationMapper {

public static Notification toNotification(Member sender, Member receiver, NotificationRequestDTO.createNotification request, String reactionName) {
public static Notification toNotification(Member sender, User receiver, NotificationRequestDTO.createNotification request, String reactionName) {

return Notification.builder()
.sender(sender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class NotificationService {
private final NotificationQueryAdapter notificationQueryAdapter;

private final MemberQueryAdapter memberQueryAdapter;
private final UserQueryAdapter userQueryAdapter;

private final TreehouseQueryAdapter treehouseQueryAdapter;

Expand All @@ -45,10 +46,10 @@ public class NotificationService {
public void createNotification(User user, Long treehouseId, NotificationRequestDTO.createNotification request, String reactionName) {
TreeHouse treeHouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Member sender = memberQueryAdapter.findByUserAndTreehouse(user, treeHouse);
Member receiver = memberQueryAdapter.findById(request.getReceiverId());
User receiver = userQueryAdapter.findById(request.getReceiverId());
Notification notification = NotificationMapper.toNotification(sender, receiver, request, reactionName);
if(user.isPushAgree()) {
fcmService.sendFcmMessage(receiver.getUser(), notification.getTitle(), notification.getBody());
fcmService.sendFcmMessage(receiver, notification.getTitle(), notification.getBody());
}
notificationCommandAdapter.createNotification(notification);
}
Expand All @@ -61,11 +62,7 @@ public void createNotification(User user, Long treehouseId, NotificationRequestD
@Transactional(readOnly = true)
public NotificationResponseDTO.getNotifications getNotifications(User user){ //@AuthMember로 들어온 User 객체
List<Member> members = user.getMemberList(); // 사용자의 Member(가입된 트리하우스 프로필) 목록 조회
List<Notification> notifications = members.stream()
.map(Member::getNotificationList)
.flatMap(List::stream)
.sorted(Comparator.comparing(Notification::getCreatedAt).reversed()) // createdAt 기준으로 내림차순 정렬
.toList();
List<Notification> notifications = user.getNotificationList(); // 사용자의 알림 목록 조회

List<NotificationResponseDTO.getNotification> notificationDtos = notifications.stream()
.map(notification -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import treehouse.server.global.entity.User.UserRole;
import treehouse.server.global.entity.User.UserStatus;

import java.util.ArrayList;
import java.util.List;

@Component
Expand Down Expand Up @@ -40,6 +41,7 @@ public static User toUser(String name, String phone){
.phone(phone)
.role(UserRole.USER)
.status(UserStatus.ACTIVE)
.notificationList(new ArrayList<>())
.build();
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/treehouse/server/global/entity/User/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.hibernate.annotations.SQLDelete;
import treehouse.server.global.entity.common.BaseDateTimeEntity;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.notification.Notification;

import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -59,6 +60,9 @@ public class User extends BaseDateTimeEntity {
@OneToMany(mappedBy = "user")
private List<Member> memberList;

@OneToMany(mappedBy = "receiver")
List<Notification> notificationList;

public void addMember(Member member) {
memberList.add(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class Member extends BaseDateTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
private TreeHouse treeHouse;

@OneToMany(mappedBy = "receiver")
List<Notification> notificationList;

@OneToMany(mappedBy = "writer")
List<Comment> commentList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.*;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.entity.common.BaseDateTimeEntity;
import treehouse.server.global.entity.member.Member;

Expand Down Expand Up @@ -33,7 +34,7 @@ public class Notification extends BaseDateTimeEntity {

@JoinColumn(name = "receiverId")
@ManyToOne(fetch = FetchType.LAZY)
private Member receiver;
private User receiver;

private Long targetId;
private LocalDateTime receivedTime;
Expand Down

0 comments on commit 2b514b2

Please sign in to comment.