Skip to content

Commit

Permalink
Merge pull request #217 from sharemindteam/feature/213-counselor-prof…
Browse files Browse the repository at this point in the history
…ile-record

feat: 상담사 프로필 수정 이력 저장
  • Loading branch information
letskuku authored Jul 15, 2024
2 parents b659561 + 0e7bf03 commit 6ec86bc
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import com.example.sharemind.counselor.domain.ConsultCost;
import com.example.sharemind.counselor.domain.ConsultTime;
import com.example.sharemind.counselor.domain.Counselor;
import com.example.sharemind.counselor.domain.ProfileRecord;
import com.example.sharemind.counselor.dto.request.CounselorGetRequest;
import com.example.sharemind.counselor.dto.request.CounselorUpdateAccountRequest;
import com.example.sharemind.counselor.dto.request.CounselorUpdateProfileRequest;
import com.example.sharemind.counselor.dto.response.*;
import com.example.sharemind.counselor.exception.CounselorErrorCode;
import com.example.sharemind.counselor.exception.CounselorException;
import com.example.sharemind.counselor.repository.CounselorRepository;
import com.example.sharemind.counselor.repository.ProfileRecordRepository;
import com.example.sharemind.customer.application.CustomerService;
import com.example.sharemind.customer.domain.Customer;
import com.example.sharemind.global.content.ConsultCategory;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class CounselorServiceImpl implements CounselorService {
private static final String SPLIT_HOURS = "~";

private final CounselorRepository counselorRepository;
private final ProfileRecordRepository profileRecordRepository;
private final CustomerService customerService;
private final WishListCounselorService wishListCounselorService;
private final RedisTemplate<String, List<Long>> redisTemplate;
Expand Down Expand Up @@ -115,7 +118,7 @@ public Boolean getRetryPermission(Long customerId) {
@Transactional
@Override
public void updateCounselorProfile(CounselorUpdateProfileRequest counselorUpdateProfileRequest,
Long customerId) {
Long customerId) {
Counselor counselor = getCounselorByCustomerId(customerId);
if ((counselor.getIsEducated() == null) || (!counselor.getIsEducated())) {
throw new CounselorException(CounselorErrorCode.COUNSELOR_NOT_EDUCATED);
Expand Down Expand Up @@ -179,6 +182,20 @@ public void updateCounselorProfile(CounselorUpdateProfileRequest counselorUpdate
consultTimes.add(ConsultTime.builder().day(day).times(times).build());
}

ProfileRecord profileRecord = ProfileRecord.builder()
.counselor(counselor)
.nickname(counselorUpdateProfileRequest.getNickname())
.consultCosts(consultCosts)
.consultTimes(consultTimes)
.consultTypes(consultTypes)
.consultCategories(consultCategories)
.consultStyle(consultStyle)
.experience(counselorUpdateProfileRequest.getExperience())
.introduction(counselorUpdateProfileRequest.getIntroduction())
.profileStatus(counselor.getProfileStatus())
.build();
profileRecordRepository.save(profileRecord);

counselor.updateProfile(counselorUpdateProfileRequest.getNickname(), consultCategories,
consultStyle, consultTypes, consultTimes, consultCosts,
counselorUpdateProfileRequest.getIntroduction(),
Expand All @@ -200,33 +217,6 @@ public List<Counselor> getEvaluationPendingConsults() {
return counselorRepository.findAllByProfileStatusIsEvaluationPendingAndIsActivatedIsTrue();
}

private List<Counselor> getCounselorByCategoryWithPagination(
CounselorGetRequest counselorGetRequest, String sortType) {
String sortColumn = getCounselorSortColumn(sortType);
Pageable pageable = PageRequest.of(counselorGetRequest.getIndex(), COUNSELOR_PAGE,
Sort.by(sortColumn).descending());
if (counselorGetRequest.getConsultCategory() == null) {
return getRealtimeCounselors(counselorGetRequest.getIndex());
}

ConsultCategory consultCategory = ConsultCategory.getConsultCategoryByName(
counselorGetRequest.getConsultCategory());
return counselorRepository.findByConsultCategoryAndLevelAndStatus(consultCategory, pageable)
.getContent();
}

private List<Counselor> getRealtimeCounselors(int index) {
int start = index * COUNSELOR_PAGE;
List<Long> counselorIds = redisTemplate.opsForValue().get(REALTIME_COUNSELOR);
if (counselorIds == null || start >= counselorIds.size()) {
return Collections.emptyList();
}

List<Long> counselorsSubList = (counselorIds.size() >= start + COUNSELOR_PAGE) ?
counselorIds.subList(start, start + COUNSELOR_PAGE) : counselorIds.subList(start, counselorIds.size());
return counselorRepository.findAllById(counselorsSubList);
}

@Override
public List<Counselor> getCounselorByWordWithPagination(
SearchWordCounselorFindRequest searchWordCounselorFindRequest,
Expand All @@ -243,8 +233,8 @@ public List<Counselor> getCounselorByWordWithPagination(

@Override
public List<CounselorGetListResponse> getCounselorsByCategoryAndCustomer(Long customerId,
String sortType,
CounselorGetRequest counselorGetRequest) {
String sortType,
CounselorGetRequest counselorGetRequest) {
List<Counselor> counselors = getCounselorByCategoryWithPagination(counselorGetRequest,
sortType);
List<Long> counselorIds = redisTemplate.opsForValue().get(REALTIME_COUNSELOR);
Expand All @@ -266,7 +256,7 @@ public List<CounselorGetListResponse> getCounselorsByCategoryAndCustomer(Long cu

@Override
public List<CounselorGetListResponse> getAllCounselorsByCategory(String sortType,
CounselorGetRequest counselorGetRequest) {
CounselorGetRequest counselorGetRequest) {
List<Counselor> counselors = getCounselorByCategoryWithPagination(counselorGetRequest,
sortType);
List<Long> counselorIds = redisTemplate.opsForValue().get(REALTIME_COUNSELOR);
Expand All @@ -283,7 +273,7 @@ public List<CounselorGetListResponse> getAllCounselorsByCategory(String sortType

@Override
public CounselorGetMinderProfileResponse getCounselorMinderProfileByCustomer(Long counselorId,
Long customerId) {
Long customerId) {
Customer customer = customerService.getCustomerByCustomerId(customerId);
Counselor counselor = getCounselorByCounselorId(counselorId);

Expand All @@ -301,7 +291,7 @@ public CounselorGetMinderProfileResponse getAllCounselorMinderProfile(Long couns
@Transactional
@Override
public void updateAccount(CounselorUpdateAccountRequest counselorUpdateAccountRequest,
Long customerId) {
Long customerId) {
Counselor counselor = getCounselorByCustomerId(customerId);
Bank.existsByDisplayName(counselorUpdateAccountRequest.getBank());
counselor.updateAccountInfo(counselorUpdateAccountRequest.getAccount(),
Expand All @@ -316,12 +306,6 @@ public CounselorGetAccountResponse getAccount(Long customerId) {
return CounselorGetAccountResponse.of(counselor);
}

private String getCounselorSortColumn(String sortType) {
CounselorListSortType counselorListSortType = CounselorListSortType.getSortTypeByName(
sortType);
return counselorListSortType.getSortColumn();
}

@Override
public CounselorGetInfoResponse getCounselorMyInfo(Long customerId) {
Customer customer = customerService.getCustomerByCustomerId(customerId);
Expand All @@ -335,7 +319,7 @@ public CounselorGetInfoResponse getCounselorMyInfo(Long customerId) {

@Override
public CounselorGetForConsultResponse getCounselorForConsultCreation(Long counselorId,
String type) {
String type) {
Counselor counselor = getCounselorByCounselorId(counselorId);
ConsultType consultType = ConsultType.getConsultTypeByName(type);
if (!counselor.getConsultTypes().contains(consultType)) {
Expand All @@ -345,12 +329,6 @@ public CounselorGetForConsultResponse getCounselorForConsultCreation(Long counse
return CounselorGetForConsultResponse.of(counselor, consultType);
}

private void checkDuplicateNickname(String nickname, Long counselorId) {
if (counselorRepository.existsByNicknameAndCounselorIdNot(nickname, counselorId)) {
throw new CounselorException(CounselorErrorCode.DUPLICATE_NICKNAME);
}
}

@Override
public CounselorGetBannerResponse getCounselorChatBanner(Chat chat) {
Counselor counselor = getCounselorByCounselorId(
Expand Down Expand Up @@ -391,7 +369,8 @@ public void updateRealtimeCounselors() {
int currentHour = LocalTime.now().getHour();

List<Counselor> realtimeCounselors = counselors.stream()
.filter(counselor -> isAvailableAtRealTime(counselor.getConsultTimes(), currentDay, currentHour))
.filter(counselor -> isAvailableAtRealTime(counselor.getConsultTimes(), currentDay,
currentHour))
.sorted((c1, c2) -> Long.compare(c2.getTotalConsult(), c1.getTotalConsult()))
.toList();

Expand All @@ -402,6 +381,46 @@ public void updateRealtimeCounselors() {
redisTemplate.opsForValue().set(REALTIME_COUNSELOR, counselorIds);
}

private List<Counselor> getCounselorByCategoryWithPagination(
CounselorGetRequest counselorGetRequest, String sortType) {
String sortColumn = getCounselorSortColumn(sortType);
Pageable pageable = PageRequest.of(counselorGetRequest.getIndex(), COUNSELOR_PAGE,
Sort.by(sortColumn).descending());
if (counselorGetRequest.getConsultCategory() == null) {
return getRealtimeCounselors(counselorGetRequest.getIndex());
}

ConsultCategory consultCategory = ConsultCategory.getConsultCategoryByName(
counselorGetRequest.getConsultCategory());
return counselorRepository.findByConsultCategoryAndLevelAndStatus(consultCategory, pageable)
.getContent();
}

private String getCounselorSortColumn(String sortType) {
CounselorListSortType counselorListSortType = CounselorListSortType.getSortTypeByName(
sortType);
return counselorListSortType.getSortColumn();
}

private List<Counselor> getRealtimeCounselors(int index) {
int start = index * COUNSELOR_PAGE;
List<Long> counselorIds = redisTemplate.opsForValue().get(REALTIME_COUNSELOR);
if (counselorIds == null || start >= counselorIds.size()) {
return Collections.emptyList();
}

List<Long> counselorsSubList = (counselorIds.size() >= start + COUNSELOR_PAGE) ?
counselorIds.subList(start, start + COUNSELOR_PAGE)
: counselorIds.subList(start, counselorIds.size());
return counselorRepository.findAllById(counselorsSubList);
}

private void checkDuplicateNickname(String nickname, Long counselorId) {
if (counselorRepository.existsByNicknameAndCounselorIdNot(nickname, counselorId)) {
throw new CounselorException(CounselorErrorCode.DUPLICATE_NICKNAME);
}
}

private boolean isAvailableAtRealTime(Set<ConsultTime> consultTimes, String day, int hour) {
for (ConsultTime consultTime : consultTimes) {
if (Objects.equals(consultTime.getDay().toString(), day)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ public class Counselor extends BaseEntity {
private ProfileStatus profileStatus;

@ElementCollection(targetClass = ConsultCost.class, fetch = FetchType.LAZY)
@JoinTable(name = "costs", joinColumns = @JoinColumn(name = "counselor_id"))
@JoinTable(name = "consult_costs", joinColumns = @JoinColumn(name = "counselor_id"))
@Column(name = "consult_costs")
private Set<ConsultCost> consultCosts;

@ElementCollection(targetClass = ConsultTime.class, fetch = FetchType.LAZY)
@JoinTable(name = "times", joinColumns = @JoinColumn(name = "counselor_id"))
@JoinTable(name = "consult_times", joinColumns = @JoinColumn(name = "counselor_id"))
@Column(name = "consult_times")
private Set<ConsultTime> consultTimes;

@ElementCollection(targetClass = ConsultType.class, fetch = FetchType.LAZY)
@JoinTable(name = "types", joinColumns = @JoinColumn(name = "counselor_id"))
@JoinTable(name = "consult_types", joinColumns = @JoinColumn(name = "counselor_id"))
@Enumerated(EnumType.STRING)
@Column(name = "consult_types")
private Set<ConsultType> consultTypes;

@ElementCollection(targetClass = ConsultCategory.class, fetch = FetchType.LAZY)
@JoinTable(name = "categories", joinColumns = @JoinColumn(name = "counselor_id"))
@JoinTable(name = "consult_categories", joinColumns = @JoinColumn(name = "counselor_id"))
@Enumerated(EnumType.STRING)
@Column(name = "consult_categories")
private Set<ConsultCategory> consultCategories;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.example.sharemind.counselor.domain;

import com.example.sharemind.counselor.content.ConsultStyle;
import com.example.sharemind.counselor.content.ProfileStatus;
import com.example.sharemind.global.content.ConsultCategory;
import com.example.sharemind.global.content.ConsultType;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToOne;
import java.util.Set;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class ProfileRecord {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "profile_record_id")
private Long profileRecordId;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "counselor_id")
private Counselor counselor;

@Column(nullable = false)
private String nickname;

@ElementCollection(targetClass = ConsultCost.class, fetch = FetchType.LAZY)
@JoinTable(name = "consult_costs_record", joinColumns = @JoinColumn(name = "profile_record_id"))
@Column(name = "consult_costs")
private Set<ConsultCost> consultCosts;

@ElementCollection(targetClass = ConsultTime.class, fetch = FetchType.LAZY)
@JoinTable(name = "consult_times_record", joinColumns = @JoinColumn(name = "profile_record_id"))
@Column(name = "consult_times")
private Set<ConsultTime> consultTimes;

@ElementCollection(targetClass = ConsultType.class, fetch = FetchType.LAZY)
@JoinTable(name = "consult_types_record", joinColumns = @JoinColumn(name = "profile_record_id"))
@Enumerated(EnumType.STRING)
@Column(name = "consult_types")
private Set<ConsultType> consultTypes;

@ElementCollection(targetClass = ConsultCategory.class, fetch = FetchType.LAZY)
@JoinTable(name = "consult_categories_record", joinColumns = @JoinColumn(name = "profile_record_id"))
@Enumerated(EnumType.STRING)
@Column(name = "consult_categories")
private Set<ConsultCategory> consultCategories;

@Column(name = "consult_style")
@Enumerated(EnumType.STRING)
private ConsultStyle consultStyle;

@Column(columnDefinition = "TEXT")
private String experience;

@Column(columnDefinition = "TEXT")
private String introduction;

@Column(name = "profile_status", nullable = false)
@Enumerated(EnumType.STRING)
private ProfileStatus profileStatus;

@Builder
public ProfileRecord(Counselor counselor, String nickname, Set<ConsultCost> consultCosts,
Set<ConsultTime> consultTimes, Set<ConsultType> consultTypes,
Set<ConsultCategory> consultCategories, ConsultStyle consultStyle, String experience,
String introduction, ProfileStatus profileStatus) {
this.counselor = counselor;
this.nickname = nickname;
this.consultCosts = consultCosts;
this.consultTimes = consultTimes;
this.consultTypes = consultTypes;
this.consultCategories = consultCategories;
this.consultStyle = consultStyle;
this.experience = experience;
this.introduction = introduction;
this.profileStatus = profileStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.sharemind.counselor.repository;

import com.example.sharemind.counselor.domain.ProfileRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProfileRecordRepository extends JpaRepository<ProfileRecord, Long> {

}

0 comments on commit 6ec86bc

Please sign in to comment.