Skip to content

Commit

Permalink
[MERGE/#144] 간헐적 트랜잭션 오류 해결을 위한 객체 생성 로직 변경
Browse files Browse the repository at this point in the history
[FIX] #144 - 간헐적 트랜잭션 오류 해결을 위한 객체 생성 로직 변경
  • Loading branch information
ckkim817 authored Jul 26, 2024
2 parents 700ccaf + 9894a68 commit d7e2129
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Appointment extends BaseTimeEntity {
@Column(name = "reject_detail")
private String rejectDetail;

@Builder(access = AccessLevel.PRIVATE)
@Builder
private Appointment(
Member member,
Senior senior,
Expand All @@ -83,24 +83,6 @@ private Appointment(
this.personalTopic = personalTopic;
}

public static Appointment create(
Member member,
Senior senior,
AppointmentStatus appointmentStatus,
List<DateTimeRange> timeList,
List<String> topic,
String personalTopic
) {
return Appointment.builder()
.member(member)
.senior(senior)
.appointmentStatus(appointmentStatus)
.timeList(timeList)
.topic(topic)
.personalTopic(personalTopic)
.build();
}

public void acceptAppointment(
List<DateTimeRange> timeList,
String googleMeetLink,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AppointmentCard implements Comparable<AppointmentCard> {
@JsonIgnore
private LocalDateTime updatedAt;

@Builder(access = AccessLevel.PRIVATE)
@Builder
private AppointmentCard(
Long appointmentId,
AppointmentStatus appointmentStatus,
Expand Down Expand Up @@ -98,46 +98,4 @@ public int compareTo(AppointmentCard other) {
// 날짜가 다르면 날짜를 기준으로 비교 결과 반환
return dateComparison;
}

public static AppointmentCard create(
Long appointmentId,
AppointmentStatus appointmentStatus,
Long seniorId,
String nickname,
String image,
String field,
String department,
List<String> topic,
String personalTopic,
String company,
String position,
String detailPosition,
String level,
String date,
String startTime,
String endTime,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
return AppointmentCard.builder()
.appointmentId(appointmentId)
.appointmentStatus(appointmentStatus)
.seniorId(seniorId)
.nickname(nickname)
.image(image)
.field(field)
.department(department)
.topic(topic)
.personalTopic(personalTopic)
.company(company)
.position(position)
.detailPosition(detailPosition)
.level(level)
.date(date)
.startTime(startTime)
.endTime(endTime)
.createdAt(createdAt)
.updatedAt(updatedAt)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JuniorInfo {
private String field;
private String department;

@Builder(access = AccessLevel.PRIVATE)
@Builder
private JuniorInfo(
String nickname,
String univName,
Expand All @@ -26,18 +26,4 @@ private JuniorInfo(
this.field = field;
this.department = department;
}

public static JuniorInfo create(
String nickname,
String univName,
String field,
String department
) {
return JuniorInfo.builder()
.nickname(nickname)
.univName(univName)
.field(field)
.department(department)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SeniorInfo {
private String detailPosition;
private String level;

@Builder(access = AccessLevel.PRIVATE)
@Builder
private SeniorInfo(
String nickname,
String image,
Expand All @@ -35,24 +35,4 @@ private SeniorInfo(
this.detailPosition = detailPosition;
this.level = level;
}

public static SeniorInfo create(
String nickname,
String image,
String company,
String field,
String position,
String detailPosition,
String level
) {
return SeniorInfo.builder()
.nickname(nickname)
.image(image)
.company(company)
.field(field)
.position(position)
.detailPosition(detailPosition)
.level(level)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ public void postAppointment(AppointmentRequest appointmentRequest) {
throw new CustomException(ErrorType.INVALID_SAME_SENIOR);
}

Appointment appointment = Appointment.create(
member,
senior,
AppointmentStatus.PENDING,
appointmentRequest.timeList(),
appointmentRequest.topic(),
appointmentRequest.personalTopic()
);
Appointment appointment = Appointment.builder()
.member(member)
.senior(senior)
.appointmentStatus(AppointmentStatus.PENDING)
.timeList(appointmentRequest.timeList())
.topic(appointmentRequest.topic())
.personalTopic(appointmentRequest.personalTopic())
.build();

appointmentRepository.save(appointment);

sendNoticeMessage(
Expand Down Expand Up @@ -253,26 +254,26 @@ private AppointmentCard createAppointmentCard(Member user, Appointment appointme
}

// Appointment에서 필요한 필드들을 매핑
return AppointmentCard.create(
appointment.getId(),
appointment.getAppointmentStatus(),
seniorId,
nickname,
image,
field,
department,
topic,
personalTopic,
company,
position,
detailPosition,
level,
date,
startTime,
endTime,
appointment.getCreatedAt(),
appointment.getUpdatedAt()
);
return AppointmentCard.builder()
.appointmentId(appointment.getId())
.appointmentStatus(appointment.getAppointmentStatus())
.seniorId(seniorId)
.nickname(nickname)
.image(image)
.field(field)
.department(department)
.topic(topic)
.personalTopic(personalTopic)
.company(company)
.position(position)
.detailPosition(detailPosition)
.level(level)
.date(date)
.startTime(startTime)
.endTime(endTime)
.createdAt(appointment.getCreatedAt())
.updatedAt(appointment.getUpdatedAt())
.build();
}

@Transactional(readOnly = true)
Expand All @@ -285,27 +286,28 @@ public AppointmentDetailResponse getAppointmentDetail(

Member member = appointment.getMember();
Senior senior = appointment.getSenior();
Member seniorMember = senior.getMember();

if (!userId.equals(member.getId()) && !userId.equals(senior.getMember().getId())) {
throw new CustomException(ErrorType.NOT_MEMBERS_APPOINTMENT_ERROR);
}

JuniorInfo juniorInfo = JuniorInfo.create(
member.getNickname(),
member.getUnivName(),
member.getField(),
member.getDepartmentList().get(0)
);

SeniorInfo seniorInfo = SeniorInfo.create(
senior.getMember().getNickname(),
senior.getMember().getImage(),
senior.getCompany(),
senior.getMember().getField(),
senior.getPosition(),
senior.getDetailPosition(),
senior.getLevel()
);
JuniorInfo juniorInfo = JuniorInfo.builder()
.nickname(member.getNickname())
.univName(member.getUnivName())
.field(member.getField())
.department(member.getDepartmentList().get(0))
.build();

SeniorInfo seniorInfo = SeniorInfo.builder()
.nickname(seniorMember.getNickname())
.image(seniorMember.getImage())
.company(senior.getCompany())
.field(seniorMember.getField())
.position(senior.getPosition())
.detailPosition(senior.getDetailPosition())
.level(senior.getLevel())
.build();

return new AppointmentDetailResponse(
appointment.getAppointmentStatus(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.sopt.seonyakServer.domain.member.dto;

import jakarta.validation.constraints.NotBlank;
import java.util.List;

public record MemberJoinRequest(
int userType,
@NotBlank(message = "role 공백일 수 없습니다.")
String role,
Boolean isSubscribed,
String nickname,
String image,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class Member extends BaseTimeEntity {
@OneToOne(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Senior senior;

@Builder(access = AccessLevel.PRIVATE)
@Builder
private Member(
final SocialType socialType,
final String socialId,
Expand All @@ -78,18 +78,6 @@ private Member(
this.email = email;
}

public static Member create(
final SocialType socialType,
final String socialId,
final String email
) {
return Member.builder()
.socialType(socialType)
.socialId(socialId)
.email(email)
.build();
}

public void updateMember(
Boolean isSubscribed,
String nickname,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.seonyakServer.domain.member.service;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import java.time.LocalDateTime;
import java.util.Random;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class MemberService {
private DefaultMessageService defaultMessageService;
private final CodeService codeService;

private final EntityManager entityManager;

@Value("${coolsms.api.key}")
private String apiKey;

Expand Down Expand Up @@ -110,11 +113,11 @@ private LoginSuccessResponse getTokenDto(final MemberInfoResponse memberInfoResp
member.getId()
);
} else {
Member member = Member.create(
memberInfoResponse.socialType(),
memberInfoResponse.socialId(),
memberInfoResponse.email()
);
Member member = Member.builder()
.socialType(memberInfoResponse.socialType())
.socialId(memberInfoResponse.socialId())
.email(memberInfoResponse.email())
.build();

return getTokenByMemberId(null, memberRepository.save(member).getId());
}
Expand Down Expand Up @@ -172,8 +175,9 @@ public void validNickname(final NicknameRequest nicknameRequest) {
@Transactional
public MemberJoinResponse patchMemberJoin(MemberJoinRequest memberJoinRequest) {
Member member = memberRepository.findMemberByIdOrThrow(principalHandler.getUserIdFromPrincipal());
log.info("handler id: " + principalHandler.getUserIdFromPrincipal());
log.info("member id: " + member.getId());

log.info("영속성 컨텍스트에 포함되어 있나요(업데이트 이전)? " + entityManager.contains(member));

member.updateMember(
memberJoinRequest.isSubscribed(),
memberJoinRequest.nickname(),
Expand All @@ -183,25 +187,26 @@ public MemberJoinResponse patchMemberJoin(MemberJoinRequest memberJoinRequest) {
memberJoinRequest.field(),
memberJoinRequest.departmentList()
);
memberRepository.save(member);

Long seniorId = null;
String role;
log.info("userType: " + memberJoinRequest.userType());
log.info("영속성 컨텍스트에 포함되어 있나요(업데이트 이후)? " + entityManager.contains(member));
log.info("role: " + memberJoinRequest.role());

if (memberJoinRequest.userType() == 1) {
role = "SENIOR";
} else {
role = "JUNIOR";
}
Long seniorId = null;

if ("SENIOR".equals(role)) {
if ("SENIOR".equals(memberJoinRequest.role())) {
member.addSenior(seniorService.createSenior(memberJoinRequest, member));
seniorId = member.getSenior().getId();
} else if (!"JUNIOR".equals(memberJoinRequest.role())) {
throw new CustomException(ErrorType.INVALID_USER_TYPE_ERROR);
}

log.info("제대로 저장됐나요? 닉네임 필드를 통해 확인: "
+ memberRepository.findMemberByIdOrThrow(principalHandler.getUserIdFromPrincipal()).getNickname());

return MemberJoinResponse.of(
seniorId,
role
memberJoinRequest.role()
);
}

Expand Down
Loading

0 comments on commit d7e2129

Please sign in to comment.