Skip to content

Commit

Permalink
Merge pull request #147 from IT-Cotato/develop
Browse files Browse the repository at this point in the history
Fix: 동의한 정책이 보이지 않는 에러 해결 (#145)
  • Loading branch information
Youthhing authored Sep 6, 2024
2 parents 21cbbc0 + b2bd023 commit 45a7625
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ public class MemberPolicy extends BaseTimeEntity {
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "policy_id")
private Policy policy;
@Column(name = "policy_id", nullable = false)
private Long policyId;

private MemberPolicy(Boolean isChecked, Member member, Policy policy) {
private MemberPolicy(Boolean isChecked, Member member, Long policyId) {
this.isChecked = isChecked;
this.member = member;
this.policy = policy;
this.policyId = policyId;
this.checkTime = LocalDateTime.now();
}

public static MemberPolicy of(Boolean isChecked, Member member, Policy policy){
return new MemberPolicy(isChecked, member, policy);
public static MemberPolicy of(Boolean isChecked, Member member, Long policyId) {
return new MemberPolicy(isChecked, member, policyId);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.cotato.csquiz.domain.auth.service;

import jakarta.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.cotato.csquiz.api.policy.dto.CheckMemberPoliciesRequest;
import org.cotato.csquiz.api.policy.dto.CheckPolicyRequest;
import org.cotato.csquiz.api.policy.dto.FindMemberPolicyResponse;
import org.cotato.csquiz.api.policy.dto.PoliciesResponse;
Expand Down Expand Up @@ -34,7 +33,7 @@ public FindMemberPolicyResponse findUnCheckedPolicies(final Long memberId) {
// 회원이 체크한 정책
List<Long> checkedPolicies = memberPolicyRepository.findAllByMemberId(memberId).stream()
.filter(MemberPolicy::getIsChecked)
.map(MemberPolicy::getId)
.map(MemberPolicy::getPolicyId)
.toList();

List<PolicyInfoResponse> uncheckedEssentialPolicies = policyRepository.findAllByPolicyType(PolicyType.ESSENTIAL)
Expand All @@ -43,7 +42,8 @@ public FindMemberPolicyResponse findUnCheckedPolicies(final Long memberId) {
.map(PolicyInfoResponse::from)
.toList();

List<PolicyInfoResponse> uncheckedOptionalPolicies = policyRepository.findAllByPolicyType(PolicyType.OPTIONAL).stream()
List<PolicyInfoResponse> uncheckedOptionalPolicies = policyRepository.findAllByPolicyType(PolicyType.OPTIONAL)
.stream()
.filter(policy -> !checkedPolicies.contains(policy.getId()))
.map(PolicyInfoResponse::from)
.toList();
Expand All @@ -52,47 +52,54 @@ public FindMemberPolicyResponse findUnCheckedPolicies(final Long memberId) {
}

@Transactional
public void checkPolicies(Long memberId, List<CheckPolicyRequest> policies) {
public void checkPolicies(Long memberId, List<CheckPolicyRequest> checkedPolicies) {
Member findMember = memberService.findById(memberId);

List<Long> policyIds = policies.stream()
List<Long> policyIds = checkedPolicies.stream()
.map(CheckPolicyRequest::policyId)
.toList();

List<Policy> policies = policyRepository.findAllByIdIn(policyIds);

if (policies.size() != policyIds.size()) {
throw new EntityNotFoundException("해당 정책을 찾을 수 없습니다.");
}
// 해당 정책에 이미 체크했는지 확인
if (isAlreadyChecked(findMember, policyIds)) {
throw new AppException(ErrorCode.ALREADY_POLICY_CHECK);
}
// 필수 정책에 체크하지 않았는지 확인
validateCheckEssentialPolicies(getEssentialPolicies(policies), policyIds);

Map<Long, Policy> policyMap = policyRepository.findAllByIdIn(policyIds).stream()
.collect(Collectors.toMap(Policy::getId, Function.identity()));

List<MemberPolicy> memberPolicies = policies.stream()
.map(policyRequest -> MemberPolicy.of(policyRequest.isChecked(), findMember,
policyMap.get(policyRequest.policyId())))
List<MemberPolicy> memberPolicies = checkedPolicies.stream()
.map(policyRequest -> MemberPolicy.of(policyRequest.isChecked(), findMember, policyRequest.policyId()))
.toList();

if (hasDisagreementInEssential(memberPolicies)) {
memberPolicyRepository.saveAll(memberPolicies);
}

private void validateCheckEssentialPolicies(List<Policy> essentialPolicies, List<Long> checkedPolicyIds) {
Set<Long> essentialIds = essentialPolicies.stream()
.map(Policy::getId)
.collect(Collectors.toUnmodifiableSet());

if (checkedPolicyIds.stream().anyMatch(id -> !essentialIds.contains(id))) {
throw new AppException(ErrorCode.SHOULD_AGREE_POLICY);
}
}

memberPolicyRepository.saveAll(memberPolicies);
private List<Policy> getEssentialPolicies(List<Policy> policies) {
return policies.stream()
.filter(policy -> policy.getPolicyType() == PolicyType.ESSENTIAL)
.toList();
}

private boolean isAlreadyChecked(Member findMember, List<Long> policyIds) {
return memberPolicyRepository.findAllByMemberId(findMember.getId()).stream()
.map(MemberPolicy::getPolicy)
.map(Policy::getId)
.map(MemberPolicy::getPolicyId)
.anyMatch(policyIds::contains);
}

private boolean hasDisagreementInEssential(List<MemberPolicy> checkedPolicies) {
return checkedPolicies.stream()
.filter(checkedPolicy -> checkedPolicy.getIsChecked().equals(false))
.map(MemberPolicy::getPolicy)
.map(Policy::getPolicyType)
.anyMatch(PolicyType.ESSENTIAL::equals);
}

public PoliciesResponse findPolicies() {
List<PolicyInfoResponse> policies = policyRepository.findAll().stream()
.map(PolicyInfoResponse::from)
Expand Down

0 comments on commit 45a7625

Please sign in to comment.