Skip to content

Commit

Permalink
fix: 회원 탈퇴시 방 참여에 대한 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
parksey committed Nov 29, 2023
1 parent 109a842 commit 68da68f
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.repository.MemberRepository;
import com.moabam.api.domain.member.repository.MemberSearchRepository;
import com.moabam.api.domain.room.Participant;
import com.moabam.api.domain.room.repository.ParticipantRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.dto.auth.AuthorizationTokenInfoResponse;
import com.moabam.api.dto.auth.LoginResponse;
import com.moabam.api.dto.member.MemberInfo;
Expand All @@ -42,6 +45,8 @@ public class MemberService {
private final InventoryRepository inventoryRepository;
private final ItemRepository itemRepository;
private final MemberSearchRepository memberSearchRepository;
private final ParticipantSearchRepository participantSearchRepository;
private final ParticipantRepository participantRepository;
private final ClockHolder clockHolder;

public Member findMember(Long memberId) {
Expand Down Expand Up @@ -69,6 +74,12 @@ public Member findMemberToDelete(Long memberId) {

@Transactional
public void delete(Member member) {
List<Participant> participants = participantRepository.findAllByMemberId(member.getId());

if (!participants.isEmpty()) {
throw new BadRequestException(NEED_TO_EXIT_ALL_ROOMS);
}

member.delete(clockHolder.times());
memberRepository.flush();
memberRepository.delete(member);
Expand All @@ -92,14 +103,29 @@ public void modifyInfo(AuthMember authMember, ModifyMemberRequest modifyMemberRe
Member member = memberSearchRepository.findMember(authMember.id())
.orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND));

member.changeNickName(modifyMemberRequest.nickname());
boolean nickNameChanged = member.changeNickName(modifyMemberRequest.nickname());
member.changeIntro(modifyMemberRequest.intro());
member.changeProfileUri(newProfileUri);

memberRepository.save(member);

if (nickNameChanged) {
changeNickname(authMember.id(), modifyMemberRequest.nickname());
}
}

private void changeNickname(Long memberId, String changedName) {
List<Participant> participants = participantSearchRepository.findAllRoomMangerByMemberId(memberId);

for (Participant participant : participants) {
participant.getRoom().changeManagerNickname(changedName);
}
}

private void validateNickname(String nickname) {
if (Objects.isNull(nickname)) {
return;
}
if (StringUtils.isEmpty(nickname) && memberRepository.existsByNickname(nickname)) {
throw new ConflictException(NICKNAME_CONFLICT);
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/moabam/api/application/report/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,30 @@ public class ReportService {

@Transactional
public void report(AuthMember authMember, ReportRequest reportRequest) {
validateNoReportSubject(reportRequest.roomId(), reportRequest.certificationId());
validateNoReportSubject(reportRequest.reportedId());
Report report = createReport(authMember.id(), reportRequest);
reportRepository.save(report);
}

private Report createReport(Long reporterId, ReportRequest reportRequest) {
Member reportedMember = memberService.findMember(reportRequest.reportedId());

Certification certification = null;
if (nonNull(reportRequest.certificationId())) {
Certification certification = certificationService.findCertification(reportRequest.certificationId());

return ReportMapper.toReport(reporterId, reportedMember.getId(),
null, certification, reportRequest.description());
certification = certificationService.findCertification(reportRequest.certificationId());
}

Room room = roomService.findRoom(reportRequest.roomId());
Room room = null;
if (nonNull(reportRequest.roomId())) {
room = roomService.findRoom(reportRequest.roomId());
}

return ReportMapper.toReport(reporterId, reportedMember.getId(),
room, null, reportRequest.description());
room, certification, reportRequest.description());
}

private void validateNoReportSubject(Long roomId, Long certificationId) {
if (isNull(roomId) && isNull(certificationId)) {
private void validateNoReportSubject(Long reportedId) {
if (isNull(reportedId)) {
throw new BadRequestException(ErrorMessage.REPORT_REQUEST_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RoomService {
private final MemberService memberService;

@Transactional
public Long createRoom(Long memberId, String nickname, CreateRoomRequest createRoomRequest) {
public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
Room room = RoomMapper.toRoomEntity(createRoomRequest);
List<Routine> routines = RoutineMapper.toRoutineEntities(room, createRoomRequest.routines());
Participant participant = ParticipantMapper.toParticipant(room, memberId);
Expand All @@ -55,7 +55,7 @@ public Long createRoom(Long memberId, String nickname, CreateRoomRequest createR
Member member = memberService.findMember(memberId);
member.enterRoom(room.getRoomType());
participant.enableManager();
room.changeManagerNickname(nickname);
room.changeManagerNickname(member.getNickname());

Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/domain/member/BadgeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum BadgeType {
public static List<BadgeResponse> memberBadgeMap(Set<BadgeType> badgeTypes) {
return Arrays.stream(BadgeType.values())
.map(badgeType -> BadgeResponse.builder()
.badge(badgeType)
.badge(badgeType.korean)
.unlock(badgeTypes.contains(badgeType))
.build())
.toList();
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/moabam/api/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static java.util.Objects.*;

import java.time.LocalDateTime;
import java.util.Objects;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class Member extends BaseTimeEntity {
@Column(name = "social_id", nullable = false, unique = true)
private String socialId;

@Column(name = "nickname", nullable = false, unique = true)
@Column(name = "nickname", unique = true)
private String nickname;

@Column(name = "intro", length = 30)
Expand Down Expand Up @@ -134,10 +135,15 @@ public void increaseTotalCertifyCount() {

public void delete(LocalDateTime now) {
socialId = deleteSocialId(now);
nickname = null;
}

public void changeNickName(String nickname) {
this.nickname = requireNonNullElse(nickname, this.nickname);
public boolean changeNickName(String nickname) {
if (Objects.isNull(nickname)) {
return false;
}
this.nickname = nickname;
return true;
}

public void changeIntro(String intro) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.moabam.api.domain.room.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.moabam.api.domain.room.Participant;

public interface ParticipantRepository extends JpaRepository<Participant, Long> {

List<Participant> findAllByMemberId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ public List<Participant> findAllByRoomCertifyTime(int certifyTime) {
)
.fetch();
}

public List<Participant> findAllRoomMangerByMemberId(Long memberId) {
return jpaQueryFactory
.selectFrom(participant)
.join(participant.room, room).fetchJoin()
.where(
participant.memberId.eq(memberId),
participant.isManager.isTrue()
)
.fetch();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/dto/member/BadgeResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Builder
public record BadgeResponse(
BadgeType badge,
String badge,
boolean unlock
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class RoomController {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Long createRoom(@Auth AuthMember authMember, @Valid @RequestBody CreateRoomRequest createRoomRequest) {
return roomService.createRoom(authMember.id(), authMember.nickname(), createRoomRequest);
return roomService.createRoom(authMember.id(), createRoomRequest);
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ErrorMessage {
INVALID_REQUEST_URL("잘못된 URL 요청입니다."),
INVALID_CERTIFY_TIME("현재 인증 시간이 아닙니다."),
CERTIFICATION_NOT_FOUND("인증 정보가 없습니다."),
NEED_TO_EXIT_ALL_ROOMS("모든 방에서 나가야 회원 탈퇴가 가능합니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILED("네트워크 접근 실패입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.repository.MemberRepository;
import com.moabam.api.domain.member.repository.MemberSearchRepository;
import com.moabam.api.domain.room.repository.ParticipantRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.dto.auth.AuthorizationTokenInfoResponse;
import com.moabam.api.dto.auth.LoginResponse;
import com.moabam.api.dto.member.MemberInfo;
Expand Down Expand Up @@ -54,6 +56,12 @@ class MemberServiceTest {
@Mock
MemberSearchRepository memberSearchRepository;

@Mock
ParticipantRepository participantRepository;

@Mock
ParticipantSearchRepository participantSearchRepository;

@Mock
InventorySearchRepository inventorySearchRepository;

Expand Down Expand Up @@ -204,6 +212,8 @@ void modify_success_test(@WithMember AuthMember authMember) {
Member member = MemberFixture.member();
ModifyMemberRequest modifyMemberRequest = ModifyImageFixture.modifyMemberRequest();
given(memberSearchRepository.findMember(authMember.id())).willReturn(Optional.ofNullable(member));
given(participantSearchRepository.findAllRoomMangerByMemberId(any()))
.willReturn(List.of());

// when
memberService.modifyInfo(authMember, modifyMemberRequest, "/main");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void create_room_no_password_success() {
given(memberService.findMember(1L)).willReturn(member);

// when
Long result = roomService.createRoom(1L, "닉네임", createRoomRequest);
Long result = roomService.createRoom(1L, createRoomRequest);

// then
verify(roomRepository).save(any(Room.class));
Expand Down Expand Up @@ -98,7 +98,7 @@ void create_room_with_password_success() {
given(memberService.findMember(1L)).willReturn(member);

// when
Long result = roomService.createRoom(1L, "닉네임", createRoomRequest);
Long result = roomService.createRoom(1L, createRoomRequest);

// then
verify(roomRepository).save(any(Room.class));
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/com/moabam/api/presentation/MemberControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void delete_member_success() throws Exception {

Member deletedMEmber = deletedMemberOptional.get();
assertThat(deletedMEmber.getDeletedAt()).isNotNull();
assertThat(deletedMEmber.getNickname()).isEqualTo(nickname);
assertThat(deletedMEmber.getNickname()).isNull();
}

@DisplayName("회원이 없어서 회원 삭제 실패")
Expand Down Expand Up @@ -291,13 +291,13 @@ void search_my_info_success() throws Exception {
// MockMvcResultMatchers.jsonPath("$.birds.MORNING").value(morningInven.getItem().getImage()),
// MockMvcResultMatchers.jsonPath("$.birds.NIGHT").value(nightInven.getItem().getImage()),

MockMvcResultMatchers.jsonPath("$.badges[0].badge").value("MORNING_BIRTH"),
MockMvcResultMatchers.jsonPath("$.badges[0].badge").value("오목눈이 탄생"),
MockMvcResultMatchers.jsonPath("$.badges[0].unlock").value(true),
MockMvcResultMatchers.jsonPath("$.badges[1].badge").value("MORNING_ADULT"),
MockMvcResultMatchers.jsonPath("$.badges[1].badge").value("어른 오목눈이"),
MockMvcResultMatchers.jsonPath("$.badges[1].unlock").value(true),
MockMvcResultMatchers.jsonPath("$.badges[2].badge").value("NIGHT_BIRTH"),
MockMvcResultMatchers.jsonPath("$.badges[2].badge").value("부엉이 탄생"),
MockMvcResultMatchers.jsonPath("$.badges[2].unlock").value(true),
MockMvcResultMatchers.jsonPath("$.badges[3].badge").value("NIGHT_ADULT"),
MockMvcResultMatchers.jsonPath("$.badges[3].badge").value("어른 부엉이"),
MockMvcResultMatchers.jsonPath("$.badges[3].unlock").value(false),
MockMvcResultMatchers.jsonPath("$.goldenBug").value(member.getBug().getGoldenBug()),
MockMvcResultMatchers.jsonPath("$.morningBug").value(member.getBug().getMorningBug()),
Expand Down Expand Up @@ -342,13 +342,13 @@ void search_my_info_with_no_badge_success() throws Exception {
// MockMvcResultMatchers.jsonPath("$.birds.MORNING").value(morningInven.getItem().getImage()),
// MockMvcResultMatchers.jsonPath("$.birds.NIGHT").value(nightInven.getItem().getImage()),

MockMvcResultMatchers.jsonPath("$.badges[0].badge").value("MORNING_BIRTH"),
MockMvcResultMatchers.jsonPath("$.badges[0].badge").value("오목눈이 탄생"),
MockMvcResultMatchers.jsonPath("$.badges[0].unlock").value(false),
MockMvcResultMatchers.jsonPath("$.badges[1].badge").value("MORNING_ADULT"),
MockMvcResultMatchers.jsonPath("$.badges[1].badge").value("어른 오목눈이"),
MockMvcResultMatchers.jsonPath("$.badges[1].unlock").value(false),
MockMvcResultMatchers.jsonPath("$.badges[2].badge").value("NIGHT_BIRTH"),
MockMvcResultMatchers.jsonPath("$.badges[2].badge").value("부엉이 탄생"),
MockMvcResultMatchers.jsonPath("$.badges[2].unlock").value(false),
MockMvcResultMatchers.jsonPath("$.badges[3].badge").value("NIGHT_ADULT"),
MockMvcResultMatchers.jsonPath("$.badges[3].badge").value("어른 부엉이"),
MockMvcResultMatchers.jsonPath("$.badges[3].unlock").value(false),
MockMvcResultMatchers.jsonPath("$.goldenBug").value(member.getBug().getGoldenBug()),
MockMvcResultMatchers.jsonPath("$.morningBug").value(member.getBug().getMorningBug()),
Expand Down Expand Up @@ -405,13 +405,13 @@ void search_friend_info_success() throws Exception {
MockMvcResultMatchers.jsonPath("$.birds.MORNING").value(morningInven.getItem().getAwakeImage()),
MockMvcResultMatchers.jsonPath("$.birds.NIGHT").value(nightInven.getItem().getAwakeImage()),

MockMvcResultMatchers.jsonPath("$.badges[0].badge").value("MORNING_BIRTH"),
MockMvcResultMatchers.jsonPath("$.badges[0].badge").value("오목눈이 탄생"),
MockMvcResultMatchers.jsonPath("$.badges[0].unlock").value(true),
MockMvcResultMatchers.jsonPath("$.badges[1].badge").value("MORNING_ADULT"),
MockMvcResultMatchers.jsonPath("$.badges[1].badge").value("어른 오목눈이"),
MockMvcResultMatchers.jsonPath("$.badges[1].unlock").value(true),
MockMvcResultMatchers.jsonPath("$.badges[2].badge").value("NIGHT_BIRTH"),
MockMvcResultMatchers.jsonPath("$.badges[2].badge").value("부엉이 탄생"),
MockMvcResultMatchers.jsonPath("$.badges[2].unlock").value(true),
MockMvcResultMatchers.jsonPath("$.badges[3].badge").value("NIGHT_ADULT"),
MockMvcResultMatchers.jsonPath("$.badges[3].badge").value("어른 부엉이"),
MockMvcResultMatchers.jsonPath("$.badges[3].unlock").value(true)
).andDo(print());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,22 @@ void reports_success(boolean roomFilter, boolean certificationFilter) throws Exc
.andExpect(status().is2xxSuccessful());
}

@DisplayName("방과 인증 값 둘 다 들어오지 않는다면 테스트 실패")
@DisplayName("사용자 신고 성공")
@WithMember
@Test
void reports_failBy_subject_null() throws Exception {
// given
ReportRequest reportRequest = ReportFixture.reportRequest(123L, null, null);
Member member = MemberFixture.member("2", "ji");
memberRepository.save(member);

ReportRequest reportRequest = ReportFixture.reportRequest(member.getId(), null, null);
String request = objectMapper.writeValueAsString(reportRequest);

// expected
mockMvc.perform(post("/reports")
.contentType(MediaType.APPLICATION_JSON)
.content(request))
.andExpect(status().isBadRequest());
.andExpect(status().is2xxSuccessful());
}

@DisplayName("회원 조회 실패로 신고 실패")
Expand Down

0 comments on commit 68da68f

Please sign in to comment.