Skip to content

Commit 9801bfa

Browse files
authored
Merge branch 'develop' into feat/104
2 parents 94c213e + b3c08fb commit 9801bfa

File tree

10 files changed

+32
-24
lines changed

10 files changed

+32
-24
lines changed

src/main/java/treehouse/server/api/invitation/business/InvitationService.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,20 @@ public InvitationResponseDTO.createInvitation createInvitation(User user, Invita
7777
Member sender = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);
7878
// 초대 대상이 가입된 사람인지 찾기
7979

80-
User receiverUser = null;
80+
User receiverUser = userQueryAdapter.findByPhoneNumberOptional(request.getPhoneNumber()).orElse(null);
8181

82-
try {
83-
receiverUser = userQueryAdapter.findByPhoneNumber(request.getPhoneNumber());
84-
}
85-
catch (UserException e){
86-
// 뭐 안함
87-
}
8882
// 초대장 만들어서 저장하기
8983

9084
Invitation invitation = invitationCommandAdapter.saveInvitation(InvitationMapper.toInvitation(request.getPhoneNumber(), sender, receiverUser, treehouse));
9185

9286
//알림 생성
93-
NotificationRequestDTO.createNotification notificationRequest = new NotificationRequestDTO.createNotification();
94-
notificationRequest.setReceiverId(receiverUser.getId()); // 여기서 receiver 설정 (예시)
95-
notificationRequest.setTargetId(invitation.getId());
96-
notificationRequest.setType(NotificationType.INVITATION); // 알림 타입 설정 (예시)
97-
notificationService.createNotification(user, invitation.getTreeHouse().getId(), notificationRequest, null);
98-
87+
if (receiverUser != null) {
88+
NotificationRequestDTO.createNotification notificationRequest = new NotificationRequestDTO.createNotification();
89+
notificationRequest.setReceiverId(receiverUser.getId()); // 여기서 receiver 설정 (예시)
90+
notificationRequest.setTargetId(invitation.getId());
91+
notificationRequest.setType(NotificationType.INVITATION); // 알림 타입 설정 (예시)
92+
notificationService.createNotification(user, invitation.getTreeHouse().getId(), notificationRequest, null);
93+
}
9994
// 리턴하기
10095

10196
return InvitationMapper.toCreateInvitationDTO(invitation);

src/main/java/treehouse/server/api/member/business/MemberMapper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public static Member toMember(User user, MemberRequestDTO.registerMember request
2525
.bio(request.getBio())
2626
.profileImageUrl(request.getProfileImageURL())
2727
.treeHouse(treeHouse)
28-
.notificationList(new ArrayList<>())
2928
.commentList(new ArrayList<>())
3029
.invitationList(new ArrayList<>())
3130
.reactionList(new ArrayList<>())

src/main/java/treehouse/server/api/member/implementation/MemberQueryAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package treehouse.server.api.member.implementation;
22

33
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
45
import treehouse.server.api.member.persistence.MemberRepository;
56
import treehouse.server.global.annotations.Adapter;
67
import treehouse.server.global.entity.User.User;
@@ -9,6 +10,7 @@
910
import treehouse.server.global.exception.GlobalErrorCode;
1011
import treehouse.server.global.exception.ThrowClass.MemberException;
1112

13+
@Slf4j
1214
@Adapter
1315
@RequiredArgsConstructor
1416
public class MemberQueryAdapter {
@@ -20,6 +22,7 @@ public Member getMember(User user){
2022
}
2123

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

src/main/java/treehouse/server/api/notification/business/NotificationMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1717
public class NotificationMapper {
1818

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

2121
return Notification.builder()
2222
.sender(sender)

src/main/java/treehouse/server/api/notification/business/NotificationService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class NotificationService {
3030
private final NotificationQueryAdapter notificationQueryAdapter;
3131

3232
private final MemberQueryAdapter memberQueryAdapter;
33+
private final UserQueryAdapter userQueryAdapter;
3334

3435
private final TreehouseQueryAdapter treehouseQueryAdapter;
3536

@@ -45,11 +46,12 @@ public class NotificationService {
4546
public void createNotification(User user, Long treehouseId, NotificationRequestDTO.createNotification request, String reactionName) {
4647
TreeHouse treeHouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
4748
Member sender = memberQueryAdapter.findByUserAndTreehouse(user, treeHouse);
48-
Member receiver = memberQueryAdapter.findById(request.getReceiverId());
49+
User receiver = userQueryAdapter.findByIdOptional(request.getReceiverId()).orElse(null);
4950
Notification notification = NotificationMapper.toNotification(sender, receiver, request, reactionName);
5051

5152
if(receiver.getUser().isPushAgree()) {
5253
fcmService.sendFcmMessage(receiver.getUser(), notification.getTitle(), notification.getBody());
54+
5355
}
5456
notificationCommandAdapter.createNotification(notification);
5557
}
@@ -62,11 +64,7 @@ public void createNotification(User user, Long treehouseId, NotificationRequestD
6264
@Transactional(readOnly = true)
6365
public NotificationResponseDTO.getNotifications getNotifications(User user){ //@AuthMember로 들어온 User 객체
6466
List<Member> members = user.getMemberList(); // 사용자의 Member(가입된 트리하우스 프로필) 목록 조회
65-
List<Notification> notifications = members.stream()
66-
.map(Member::getNotificationList)
67-
.flatMap(List::stream)
68-
.sorted(Comparator.comparing(Notification::getCreatedAt).reversed()) // createdAt 기준으로 내림차순 정렬
69-
.toList();
67+
List<Notification> notifications = user.getNotificationList(); // 사용자의 알림 목록 조회
7068

7169
List<NotificationResponseDTO.getNotification> notificationDtos = notifications.stream()
7270
.map(notification -> {

src/main/java/treehouse/server/api/user/business/UserMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import treehouse.server.global.entity.User.UserRole;
1010
import treehouse.server.global.entity.User.UserStatus;
1111

12+
import java.util.ArrayList;
1213
import java.util.List;
1314

1415
@Component
@@ -40,6 +41,7 @@ public static User toUser(String name, String phone){
4041
.phone(phone)
4142
.role(UserRole.USER)
4243
.status(UserStatus.ACTIVE)
44+
.notificationList(new ArrayList<>())
4345
.build();
4446
}
4547

src/main/java/treehouse/server/api/user/implement/UserQueryAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ public User findByPhoneNumber(String phone){
3030
return userRepository.findByPhone(phone).orElseThrow(()->new UserException(GlobalErrorCode.USER_NOT_FOUND));
3131
}
3232

33+
public Optional<User> findByPhoneNumberOptional(String phone){
34+
return userRepository.findByPhone(phone);
35+
}
36+
3337
public User findById(Long id){
3438
return userRepository.findById(id).orElseThrow(()->new UserException(GlobalErrorCode.USER_NOT_FOUND));
3539
}
3640

41+
public Optional<User> findByIdOptional(Long id){
42+
return userRepository.findById(id);
43+
}
44+
3745
public Optional<User> optionalUserFindById(Long id){
3846
return userRepository.findById(id);
3947
}

src/main/java/treehouse/server/global/entity/User/User.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.hibernate.annotations.SQLDelete;
1111
import treehouse.server.global.entity.common.BaseDateTimeEntity;
1212
import treehouse.server.global.entity.member.Member;
13+
import treehouse.server.global.entity.notification.Notification;
1314

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

63+
@OneToMany(mappedBy = "receiver")
64+
List<Notification> notificationList;
65+
6266
public void addMember(Member member) {
6367
memberList.add(member);
6468
}

src/main/java/treehouse/server/global/entity/member/Member.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public class Member extends BaseDateTimeEntity {
4040
@ManyToOne(fetch = FetchType.LAZY)
4141
private TreeHouse treeHouse;
4242

43-
@OneToMany(mappedBy = "receiver")
44-
List<Notification> notificationList;
4543

4644
@OneToMany(mappedBy = "writer")
4745
List<Comment> commentList;

src/main/java/treehouse/server/global/entity/notification/Notification.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import jakarta.persistence.*;
44
import lombok.*;
5+
import treehouse.server.global.entity.User.User;
56
import treehouse.server.global.entity.common.BaseDateTimeEntity;
67
import treehouse.server.global.entity.member.Member;
78

@@ -33,7 +34,7 @@ public class Notification extends BaseDateTimeEntity {
3334

3435
@JoinColumn(name = "receiverId")
3536
@ManyToOne(fetch = FetchType.LAZY)
36-
private Member receiver;
37+
private User receiver;
3738

3839
private Long targetId;
3940
private LocalDateTime receivedTime;

0 commit comments

Comments
 (0)