Skip to content

Commit

Permalink
Merge pull request #204 from swm-nodriversomabus/BUS-208-matching-cha…
Browse files Browse the repository at this point in the history
…troom-connect

feat(BE): 매칭 생성 시 채팅방 연결 BUS-208-matching-chatroom-connect #202
  • Loading branch information
Lemonade255 authored Nov 13, 2023
2 parents f9ab195 + be653dc commit e7566bf
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/example/api/chatroom/type/ChatRoomEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
@AllArgsConstructor
@Getter
public enum ChatRoomEnum {
Normal("일반"),
Inquery("상담");
Normal("일반", 1),
Matching("매칭", 2),
Inquiry("상담", 3);

private final String type;
}
private final Integer typeCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.api.auth.domain.SecurityUser;
import com.example.api.chatroom.application.port.in.CreateChatRoomUsecase;
import com.example.api.chatroom.domain.ChatRoom;
import com.example.api.chatroom.dto.CreateChatRoomDto;
import com.example.api.chatroom.type.ChatRoomEnum;
import com.example.api.common.exception.CustomException;
import com.example.api.common.type.ApplicationStateEnum;
import com.example.api.common.type.ErrorCodeEnum;
Expand Down Expand Up @@ -55,15 +57,30 @@ public FindMatchingDto createMatching(@Valid @RequestBody SaveMatchingDto saveMa
log.error("MatchingController::createMatching: Login is needed");
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}

// 매칭 전용 채팅방 생성
CreateChatRoomDto createChatRoomDto = CreateChatRoomDto.builder()
.masterId(securityUser.getUserId())
.chatroomName(saveMatchingDto.getTitle())
.type(ChatRoomEnum.Matching)
.isActive(true)
.build();
ChatRoom chatRoom = createChatRoomUsecase.createRoom(createChatRoomDto);
addMemberChatRoomUsecase.addMember(chatRoom.getChatroomId(), securityUser.getUserId());
saveMatchingDto.setChatRoomId(chatRoom.getChatroomId());

// 매칭 데이터 저장
FindMatchingDto findMatchingDto = saveMatchingUsecase.createMatching(securityUser.getUserId(), saveMatchingDto);

// Owner 등록
SaveMatchingApplicationDto saveMatchingApplicationDto = SaveMatchingApplicationDto.builder()
.userId(securityUser.getUserId())
.matchingId(findMatchingDto.getMatchingId())
.state(ApplicationStateEnum.Owner)
.isActive(true)
.build();
matchingApplicationUsecase.createMatchingApplicationData(securityUser.getUserId(), saveMatchingApplicationDto);

return findMatchingDto;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class MatchingEntity extends BaseEntity {
@Column(nullable = false)
private UUID writerId;

@Column(nullable = false)
private UUID chatRoomId;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private MatchingTypeEnum type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class Matching {
private Long matchingId;
private UUID writerId;
private UUID chatRoomId;
private MatchingTypeEnum type;
private String title;
private String place;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class FindMatchingDto {
@NotNull
private UUID writerId;

@NotNull
private UUID chatRoomId;

@NotNull
private MatchingTypeEnum type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.*;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Setter
Expand All @@ -15,6 +16,8 @@
@NoArgsConstructor
@AllArgsConstructor
public class SaveMatchingDto {
private UUID chatRoomId;

@NotNull
private MatchingTypeEnum type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.matching.dto.FindMatchingDto;
import com.example.api.matching.dto.SaveMatchingApplicationDto;
import com.example.api.member.service.MemberService;
import com.example.api.user.adapter.out.persistence.UserEntity;
import com.example.api.user.adapter.out.persistence.UserMapperInterface;
import com.example.api.user.application.port.out.FindUserPort;
Expand All @@ -37,6 +38,7 @@ public class MatchingApplicationService implements MatchingApplicationUsecase {
private final FindUserPort findUserPort;
private final FindMatchingPort findMatchingPort;
private final MatchingApplicationPort matchingApplicationPort;
private final MemberService memberService;
private final FcmService fcmService;

/**
Expand Down Expand Up @@ -126,9 +128,18 @@ public void processMatchingApplication(SaveMatchingApplicationDto matchingApplic
log.error("MatchingApplicationService::processMatchingApplication: Data not found");
throw new CustomException(ErrorCodeEnum.APPLICATION_NOT_FOUND);
}

MatchingApplication matchingApplication = matchingMapper.toDomain(matchingApplicationEntity.get());
matchingApplication.setState(state);
matchingApplicationPort.saveMatchingApplication(matchingApplication);

// 신청 수락 시 매칭 채팅방에 멤버 초대
if (state.equals(ApplicationStateEnum.Approved)) {
MatchingEntity matchingEntity = findMatchingPort.getByMatchingId(matchingApplication.getMatchingId()).get();
memberService.addMember(matchingEntity.getChatRoomId(), matchingApplication.getUserId());
}

// 푸시 알림 전송
FcmDto fcmDto = FcmDto.builder()
.userId(matchingApplication.getUserId())
.title(state.equals(ApplicationStateEnum.Approved) ? "신청 수락" : "신청 거절")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class MemberController {
* 방 생성 후, 유저들을 초대해 들어오는 경우
* @param addMemberDto (데이터)
*/
@Operation(summary = "Add member", description = "채팅방에 사용자를 초대한다.")
@Operation(summary = "Add members", description = "채팅방에 사용자를 초대한다.")
@PostMapping("/members")
public void addMembers(@Valid @RequestBody AddMemberDto addMemberDto) {
SecurityUser securityUser = AuthenticationUtils.getCurrentUserAuthentication();
if (securityUser == null) {
log.error("MemberController::addMembers: Login is needed");
throw new CustomException(ErrorCodeEnum.LOGIN_IS_NOT_DONE);
}
addMemberChatRoomUsecase.addMember(addMemberDto);
addMemberChatRoomUsecase.addMembers(addMemberDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MemberPersistentAdapter implements AddMemberChatRoomPort {
private final MemberRepository memberRepository;

@Override
public void addMember(List<Member> members, ChatRoom chatRoom) {
public void addMembers(List<Member> members, ChatRoom chatRoom) {
memberRepository.saveAll(memberMapper.fromListDomainToEntity(members, chatRoomMapper.toEntity(chatRoom)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.example.api.matching.domain.MatchingApplication;
import com.example.api.member.dto.AddMemberDto;

import java.util.UUID;

public interface AddMemberChatRoomUsecase {
void addMember(AddMemberDto addMemberDto);
void addMember(UUID chatRoomId, UUID userId);
void addMembers(AddMemberDto addMemberDto);
ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, ChatRoom chatRoom);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import java.util.List;

public interface AddMemberChatRoomPort {
void addMember(List<Member> members, ChatRoom chatRoom);
void addMembers(List<Member> members, ChatRoom chatRoom);
}
28 changes: 20 additions & 8 deletions src/main/java/com/example/api/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;
import java.util.*;

@Service
@Slf4j
Expand All @@ -26,14 +23,29 @@ public class MemberService implements AddMemberChatRoomUsecase {
private final FindMatchingPort findMatchingPort;
private final AddMemberChatRoomPort addMemberChatRoomPort;
private final RetrieveChatRoomPort retrieveChatRoomPort;

/**
* 채팅방의 한 명의 멤버 추가
* @param chatRoomId (채팅방 ID)
* @param userId (사용자 ID)
*/
@Override
@Transactional
public void addMember(UUID chatRoomId, UUID userId) {
AddMemberDto addMemberDto = AddMemberDto.builder()
.chatroomId(chatRoomId)
.memberIds(new ArrayList<>(Collections.singletonList(userId)))
.build();
this.addMembers(addMemberDto);
}

/**
* 채팅방에 멤버 추가
* 채팅방에 여러 명의 멤버 추가
* @param addMemberDto (데이터)
*/
@Override
@Transactional
public void addMember(AddMemberDto addMemberDto) {
public void addMembers(AddMemberDto addMemberDto) {
List<Member> members = new ArrayList<>();
ChatRoom chatRoom = retrieveChatRoomPort.retrieveChatRoom(addMemberDto.getChatroomId());
for (UUID userId: addMemberDto.getMemberIds()) {
Expand All @@ -44,7 +56,7 @@ public void addMember(AddMemberDto addMemberDto) {
.build();
members.add(member);
}
addMemberChatRoomPort.addMember(members, chatRoom);
addMemberChatRoomPort.addMembers(members, chatRoom);
}

/**
Expand All @@ -66,7 +78,7 @@ public ChatRoom setupMatchingChatRoom(MatchingApplication matchingApplication, C
.chatroomId(chatRoom.getChatroomId())
.memberIds(memberIds)
.build();
this.addMember(addMemberDto);
this.addMembers(addMemberDto);

chatRoom.setMembers(memberIds);
return chatRoom;
Expand Down

0 comments on commit e7566bf

Please sign in to comment.