Skip to content

Commit

Permalink
feat: 맴버 액션 삭제시 지출 디테일 초기화 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee committed Aug 16, 2024
1 parent e032893 commit 4f9e44f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package server.haengdong.application;

import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import server.haengdong.application.request.MemberActionsSaveAppRequest;
import server.haengdong.application.response.CurrentMemberAppResponse;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.ActionRepository;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.BillActionDetail;
import server.haengdong.domain.action.BillActionDetailRepository;
import server.haengdong.domain.action.BillActionRepository;
import server.haengdong.domain.action.CurrentMembers;
import server.haengdong.domain.action.MemberAction;
import server.haengdong.domain.action.MemberActionRepository;
Expand All @@ -25,6 +30,8 @@ public class MemberActionService {
private final MemberActionRepository memberActionRepository;
private final EventRepository eventRepository;
private final ActionRepository actionRepository;
private final BillActionDetailRepository billActionDetailRepository;
private final BillActionRepository billActionRepository;

@Transactional
public void saveMemberAction(String token, MemberActionsSaveAppRequest request) {
Expand Down Expand Up @@ -76,7 +83,26 @@ public void deleteMemberAction(String token, Long actionId) {
MemberAction memberAction = memberActionRepository.findByAction(action)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.MEMBER_ACTION_NOT_FOUND));

memberActionRepository.deleteAllByMemberNameAndMinSequence(memberAction.getMemberName(),
memberAction.getSequence());
memberActionRepository.deleteAllByMemberNameAndMinSequence(memberAction.getMemberName(), memberAction.getSequence());

List<BillAction> billActions = billActionRepository.findByGreaterThanSequence(action.getSequence());
billActions.forEach(billAction -> resetBillAction(event, billAction));
}

private void resetBillAction(Event event, BillAction billAction) {
List<MemberAction> memberActions = memberActionRepository.findByEventAndSequence(event, billAction.getSequence());
CurrentMembers currentMembers = CurrentMembers.of(memberActions);
Set<String> members = currentMembers.getMembers();

billActionDetailRepository.deleteAllByBillAction(billAction);

int memberCount = members.size();
if (memberCount != 0) {
Long eachPrice = billAction.getPrice() / memberCount;
for (String member : members) {
BillActionDetail billActionDetail = new BillActionDetail(billAction, member, eachPrice);
billActionDetailRepository.save(billActionDetail);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface BillActionDetailRepository extends JpaRepository<BillActionDetail, Long> {

List<BillActionDetail> findByBillAction(BillAction billAction);

void deleteAllByBillAction(BillAction billAction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import server.haengdong.domain.event.Event;

Expand All @@ -16,4 +17,11 @@ public interface BillActionRepository extends JpaRepository<BillAction, Long> {
void deleteByAction_EventAndActionId(Event event, Long actionId);

Optional<BillAction> findByAction_Id(Long actionId);

@Query("""
select ba
from BillAction ba
where ba.action.sequence > :sequence
""")
List<BillAction> findByGreaterThanSequence(Long sequence);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public interface MemberActionRepository extends JpaRepository<MemberAction, Long
List<MemberAction> findAllByAction_EventAndMemberName(Event event, String memberName);

boolean existsByAction_EventAndMemberName(Event event, String updatedMemberName);

@Query("""
select ma
from MemberAction ma
where ma.action.event = :event and ma.action.sequence < :sequence
""")
List<MemberAction> findByEventAndSequence(Event event, Long sequence);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import server.haengdong.application.request.MemberActionSaveAppRequest;
import server.haengdong.application.request.MemberActionsSaveAppRequest;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.BillActionDetail;
import server.haengdong.domain.action.BillActionDetailRepository;
import server.haengdong.domain.action.BillActionRepository;
import server.haengdong.domain.action.MemberAction;
import server.haengdong.domain.action.MemberActionRepository;
import server.haengdong.domain.action.MemberActionStatus;
Expand All @@ -33,6 +37,12 @@ class MemberActionServiceTest extends ServiceTestSupport {
@Autowired
private EventRepository eventRepository;

@Autowired
private BillActionRepository billActionRepository;

@Autowired
private BillActionDetailRepository billActionDetailRepository;

@DisplayName("현재 행사에 참여하고 있는 경우에 나갈 수 있다.")
@Test
void saveMemberActionTest() {
Expand Down Expand Up @@ -151,6 +161,45 @@ void deleteMemberAction() {
);
}

@DisplayName("이벤트에 속한 멤버 액션을 삭제하면 이후 지출 내역 디테일이 초기화된다.")
@Test
void deleteMemberAction1() {
Event event = Fixture.EVENT1;
eventRepository.save(event);
MemberAction memberAction1 = createMemberAction(new Action(event, 1L), "토다리", IN, 1L);
Action targetAction = new Action(event, 2L);
MemberAction memberAction2 = createMemberAction(targetAction, "토다리", OUT, 2L);
MemberAction memberAction3 = createMemberAction(new Action(event, 3L), "쿠키", IN, 3L);
MemberAction memberAction4 = createMemberAction(new Action(event, 4L), "웨디", IN, 4L);
MemberAction memberAction5 = createMemberAction(new Action(event, 5L), "감자", IN, 5L);
memberActionRepository.saveAll(
List.of(memberAction1,
memberAction2,
memberAction3,
memberAction4,
memberAction5
)
);
BillAction billAction = new BillAction(new Action(event, 6L), "뽕족", 100_000L);
billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "쿠키", 40_000L);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "웨디", 30_000L);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "감자", 30_000L);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2, billActionDetail3));

memberActionService.deleteMemberAction(event.getToken(), targetAction.getId());
List<BillActionDetail> billActionDetails = billActionDetailRepository.findByBillAction(billAction);

assertThat(billActionDetails).hasSize(4)
.extracting("memberName", "price")
.containsExactlyInAnyOrder(
tuple("토다리", 25_000L),
tuple("쿠키", 25_000L),
tuple("웨디", 25_000L),
tuple("감자", 25_000L)
);
}

private MemberAction createMemberAction(
Action action,
String memberName,
Expand Down

0 comments on commit 4f9e44f

Please sign in to comment.