From 5de69151a1df627213418ce1536cf6c6210cbabb Mon Sep 17 00:00:00 2001 From: SangWoon123 Date: Sun, 1 Sep 2024 21:03:12 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20planner=20=EC=97=85=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/planner/entity/Planner.java | 20 +++++++- .../planner/service/GroupPlannerService.java | 46 +++++++++++++++---- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/tukorea/planding/domain/planner/entity/Planner.java b/src/main/java/com/tukorea/planding/domain/planner/entity/Planner.java index f895725..d8422ce 100644 --- a/src/main/java/com/tukorea/planding/domain/planner/entity/Planner.java +++ b/src/main/java/com/tukorea/planding/domain/planner/entity/Planner.java @@ -1,7 +1,9 @@ package com.tukorea.planding.domain.planner.entity; +import com.tukorea.planding.domain.planner.PlannerRole; import com.tukorea.planding.domain.planner.PlannerStatus; import com.tukorea.planding.domain.schedule.entity.Schedule; +import com.tukorea.planding.domain.user.entity.User; import com.tukorea.planding.global.audit.BaseEntity; import jakarta.persistence.*; import lombok.*; @@ -60,7 +62,21 @@ public void update(String title, String content, PlannerStatus status, LocalDate this.deadline = deadline; } - public void updateManager() { - + public void updateManager(User newManager) { + this.users.stream() + .filter(plannerUser -> plannerUser.getRole() == PlannerRole.MANAGER) + .forEach(plannerUser -> plannerUser.updateRole(PlannerRole.GENERAL)); + + this.users.stream() + .filter(plannerUser -> plannerUser.getUser().equals(newManager)) + .findFirst() + .ifPresentOrElse( + plannerUser -> plannerUser.updateRole(PlannerRole.MANAGER), + () -> this.users.add(PlannerUser.builder() + .planner(this) + .user(newManager) + .role(PlannerRole.MANAGER) + .build()) + ); } } diff --git a/src/main/java/com/tukorea/planding/domain/planner/service/GroupPlannerService.java b/src/main/java/com/tukorea/planding/domain/planner/service/GroupPlannerService.java index 2a3c81d..1aa7c38 100644 --- a/src/main/java/com/tukorea/planding/domain/planner/service/GroupPlannerService.java +++ b/src/main/java/com/tukorea/planding/domain/planner/service/GroupPlannerService.java @@ -1,16 +1,18 @@ package com.tukorea.planding.domain.planner.service; import com.tukorea.planding.domain.group.service.query.UserGroupQueryService; +import com.tukorea.planding.domain.notify.entity.NotificationType; import com.tukorea.planding.domain.planner.PlannerRole; import com.tukorea.planding.domain.planner.dto.PlannerRequest; import com.tukorea.planding.domain.planner.dto.GroupPlannerResponse; +import com.tukorea.planding.domain.planner.dto.PlannerResponse; import com.tukorea.planding.domain.planner.dto.PlannerUpdateRequest; import com.tukorea.planding.domain.planner.dto.group.PlannerWeekResponse; +import com.tukorea.planding.domain.planner.dto.SchedulePlannerResponse; import com.tukorea.planding.domain.planner.entity.Planner; import com.tukorea.planding.domain.planner.entity.PlannerUser; import com.tukorea.planding.domain.planner.repository.PlannerRepository; import com.tukorea.planding.domain.planner.repository.PlannerUserRepository; -import com.tukorea.planding.domain.schedule.dto.response.ScheduleResponse; import com.tukorea.planding.domain.schedule.entity.Action; import com.tukorea.planding.domain.schedule.entity.Schedule; import com.tukorea.planding.domain.schedule.service.ScheduleQueryService; @@ -24,8 +26,8 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; -import java.time.LocalTime; import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @@ -113,12 +115,28 @@ private void adjustPlannerNumbers(Schedule schedule, int deletedPlannerNumber) { } } + public PlannerResponse getPlannerByGroup(UserInfo userInfo, String groupCode, Long plannerId) { + if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userInfo.getUserCode())) { + throw new BusinessException(ErrorCode.ACCESS_DENIED); + } + Planner planner = plannerRepository.findById(plannerId) + .orElseThrow(() -> new IllegalArgumentException("Planner not found with id: " + plannerId)); + + return PlannerResponse.fromEntity(planner); + } + public GroupPlannerResponse updateGroupPlanner(String userCode, String groupCode, PlannerUpdateRequest request) { if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userCode)) { throw new BusinessException(ErrorCode.ACCESS_DENIED); } Planner planner = plannerRepository.findById(request.plannerId()) .orElseThrow(() -> new IllegalArgumentException("Planner not found with id: " + request.plannerId())); + + User newManager = userQueryService.getUserByUserCode(request.managerCode()); + planner.updateManager(newManager); + + updateUsers(planner, request.userCodes()); + planner.update(request.title(), request.content(), request.status(), request.deadline()); return GroupPlannerResponse.fromEntity(planner, Action.UPDATE); @@ -147,21 +165,31 @@ private void updateUsers(Planner planner, List newUserCodes) { } } - public List getPlannersByGroup(UserInfo userInfo, String groupCode, Long scheduleId) { + public SchedulePlannerResponse getPlannersByGroup(UserInfo userInfo, String groupCode, Long scheduleId) { if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userInfo.getUserCode())) { throw new BusinessException(ErrorCode.ACCESS_DENIED); } Schedule schedule = scheduleQueryService.findScheduleById(scheduleId); List planners = plannerRepository.findBySchedule(schedule); - return planners.stream() - .map(GroupPlannerResponse::fromEntity) - .collect(Collectors.toList()); + return SchedulePlannerResponse.fromEntity(planners, schedule); } - public List getWeekPlannerByGroup(LocalDate startDate, LocalDate endDate, String groupCode, UserInfo userInfo) { - return plannerRepository.findAllByGroupAndDateRange(groupCode, startDate, endDate).stream() - .map(PlannerWeekResponse::fromEntity) + public List findPlannersByGroupCodeAndDateRange(LocalDate startDate, LocalDate endDate, String groupCode, UserInfo userInfo) { + List planners = plannerRepository.findAllByGroupAndDateRange(groupCode, startDate, endDate); + + Map> groupedPlanners = planners.stream().collect(Collectors.groupingBy(planner -> planner.getSchedule().getId())); + + return groupedPlanners.entrySet().stream() + .map(entry -> PlannerWeekResponse.builder() + .scheduleId(entry.getKey()) + .scheduleTitle(entry.getValue().get(0).getSchedule().getTitle()) + .scheduleDate(entry.getValue().get(0).getSchedule().getScheduleDate()) + .planners(entry.getValue().stream() + .map(PlannerResponse::fromEntity) + .collect(Collectors.toList())) + .type(NotificationType.PLANNER) + .build()) .collect(Collectors.toList()); } }