Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#175 fix member delete error #178

Merged
merged 9 commits into from
Nov 29, 2023
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 @@ -76,4 +76,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("λͺ¨λ“  λ°©μ—μ„œ λ‚˜κ°€μ•Ό νšŒμ› νƒˆν‡΄κ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€."),
PARTICIPANT_DEPORT_ERROR("λ°©μž₯은 μžμ‹ μ„ μΆ”λ°©ν•  수 μ—†μŠ΅λ‹ˆλ‹€."),

LOGIN_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 @@ -52,7 +52,7 @@ class ReportServiceTest {
@Test
void no_report_subject_fail(@WithMember AuthMember authMember) {
// given
ReportRequest reportRequest = new ReportRequest(5L, null, null, "st");
ReportRequest reportRequest = new ReportRequest(null, null, null, "st");

// When + Then
assertThatThrownBy(() -> reportService.report(authMember, reportRequest))
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