Skip to content

Commit

Permalink
Merge branch 'develop' into feat/104
Browse files Browse the repository at this point in the history
  • Loading branch information
HyoBN authored Aug 30, 2024
2 parents 94c213e + b3c08fb commit 9801bfa
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,20 @@ public InvitationResponseDTO.createInvitation createInvitation(User user, Invita
Member sender = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);
// 초대 대상이 가입된 사람인지 찾기

User receiverUser = null;
User receiverUser = userQueryAdapter.findByPhoneNumberOptional(request.getPhoneNumber()).orElse(null);

try {
receiverUser = userQueryAdapter.findByPhoneNumber(request.getPhoneNumber());
}
catch (UserException e){
// 뭐 안함
}
// 초대장 만들어서 저장하기

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

//알림 생성
NotificationRequestDTO.createNotification notificationRequest = new NotificationRequestDTO.createNotification();
notificationRequest.setReceiverId(receiverUser.getId()); // 여기서 receiver 설정 (예시)
notificationRequest.setTargetId(invitation.getId());
notificationRequest.setType(NotificationType.INVITATION); // 알림 타입 설정 (예시)
notificationService.createNotification(user, invitation.getTreeHouse().getId(), notificationRequest, null);

if (receiverUser != null) {
NotificationRequestDTO.createNotification notificationRequest = new NotificationRequestDTO.createNotification();
notificationRequest.setReceiverId(receiverUser.getId()); // 여기서 receiver 설정 (예시)
notificationRequest.setTargetId(invitation.getId());
notificationRequest.setType(NotificationType.INVITATION); // 알림 타입 설정 (예시)
notificationService.createNotification(user, invitation.getTreeHouse().getId(), notificationRequest, null);
}
// 리턴하기

return InvitationMapper.toCreateInvitationDTO(invitation);
Expand Down
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,11 +46,12 @@ 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.findByIdOptional(request.getReceiverId()).orElse(null);
Notification notification = NotificationMapper.toNotification(sender, receiver, request, reactionName);

if(receiver.getUser().isPushAgree()) {
fcmService.sendFcmMessage(receiver.getUser(), notification.getTitle(), notification.getBody());

}
notificationCommandAdapter.createNotification(notification);
}
Expand All @@ -62,11 +64,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
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ public User findByPhoneNumber(String phone){
return userRepository.findByPhone(phone).orElseThrow(()->new UserException(GlobalErrorCode.USER_NOT_FOUND));
}

public Optional<User> findByPhoneNumberOptional(String phone){
return userRepository.findByPhone(phone);
}

public User findById(Long id){
return userRepository.findById(id).orElseThrow(()->new UserException(GlobalErrorCode.USER_NOT_FOUND));
}

public Optional<User> findByIdOptional(Long id){
return userRepository.findById(id);
}

public Optional<User> optionalUserFindById(Long id){
return userRepository.findById(id);
}
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 9801bfa

Please sign in to comment.