Skip to content

Commit

Permalink
fix: 그룹스케줄 알림 기능 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 19, 2024
1 parent 6fdc07c commit 5c5f74c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.tukorea.planding.domain.notify.entity.NotificationType;
import com.tukorea.planding.domain.notify.repository.EmitterRepositoryImpl;
import com.tukorea.planding.domain.notify.repository.NotificationRepository;
import com.tukorea.planding.domain.schedule.service.GroupScheduleCreatedEvent;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.repository.UserRepository;
Expand Down Expand Up @@ -41,6 +42,10 @@ public void notifyInvitation(final String userCode, final String groupName) {
eventPublisher.publishEvent(event);
}

public void notifyGroupSchedule(final String userCode, final String schedule) {

}

@EventListener
public void handleGroupInvitedEvent(GroupInviteEvent event) {
NotificationScheduleRequest request = NotificationScheduleRequest.builder()
Expand All @@ -53,6 +58,19 @@ public void handleGroupInvitedEvent(GroupInviteEvent event) {
send(request);
}

@EventListener
public void handleGroupScheduleCreatedEvent(GroupScheduleCreatedEvent event) {
NotificationScheduleRequest request = NotificationScheduleRequest.builder()
.type(NotificationType.GROUP_SCHEDULE)
.groupName(event.getGroupName())
.message("새로운 그룹 스케줄이 생성되었습니다: " + event.getScheduleTitle())
.receiverCode(event.getUserCode())
.url(event.getUrl())
.build();

send(request);
}

public SseEmitter subscribe(String userCode, String lastEventId) {
String emitterId = makeTimeIncludeId(userCode);
SseEmitter emitter = emitterRepository.save(emitterId, new SseEmitter(DEFAULT_TIMEOUT));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tukorea.planding.domain.schedule.service;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class GroupScheduleCreatedEvent extends ApplicationEvent {
private final String userCode;
private final String groupName;
private final String scheduleTitle;
private final String url;

public GroupScheduleCreatedEvent(Object source, String userCode, String groupName, String scheduleTitle, String url) {
super(source);
this.userCode = userCode;
this.groupName = groupName;
this.scheduleTitle = scheduleTitle;
this.url = url;
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package com.tukorea.planding.domain.schedule.service;

import com.tukorea.planding.domain.group.service.UserGroupService;
import com.tukorea.planding.domain.group.service.query.UserGroupQueryService;
import com.tukorea.planding.domain.schedule.dto.response.GroupScheduleResponse;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.entity.UserGroup;
import com.tukorea.planding.domain.group.repository.usergroup.UserGroupRepository;
import com.tukorea.planding.domain.group.service.query.GroupQueryService;
import com.tukorea.planding.domain.group.service.query.UserGroupQueryService;
import com.tukorea.planding.domain.notify.dto.NotificationScheduleRequest;
import com.tukorea.planding.domain.notify.entity.NotificationType;
import com.tukorea.planding.domain.notify.service.NotificationService;
import com.tukorea.planding.domain.schedule.dto.request.ScheduleRequest;
import com.tukorea.planding.domain.schedule.dto.response.GroupScheduleResponse;
import com.tukorea.planding.domain.schedule.dto.response.ScheduleResponse;
import com.tukorea.planding.domain.schedule.dto.response.UserScheduleAttendance;
import com.tukorea.planding.domain.schedule.entity.*;
import com.tukorea.planding.domain.schedule.repository.GroupScheduleAttendanceRepository;
import com.tukorea.planding.domain.schedule.repository.GroupScheduleRepository;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.UserQueryService;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import com.tukorea.planding.domain.schedule.dto.response.ScheduleResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tukorea.planding.domain.group.entity.QGroupRoom.groupRoom;

@Service
@Slf4j
@RequiredArgsConstructor
Expand All @@ -42,12 +36,13 @@ public class GroupScheduleService {
private final ScheduleQueryService scheduleQueryService;
private final GroupQueryService groupQueryService;
private final UserQueryService userQueryService;
private final NotificationService notificationService;
private final UserGroupRepository userGroupRepository;

private final GroupScheduleRepository groupScheduleRepository;
private final GroupScheduleAttendanceRepository groupScheduleAttendanceRepository;
private final UserGroupQueryService userGroupQueryService;
private final ApplicationEventPublisher eventPublisher;


@Transactional
public ScheduleResponse createGroupSchedule(String groupCode, ScheduleRequest requestSchedule) {
Expand Down Expand Up @@ -76,19 +71,11 @@ public ScheduleResponse createGroupSchedule(String groupCode, ScheduleRequest re
Schedule savedSchedule = scheduleQueryService.save(newSchedule);

List<User> notificationUsers = userGroupRepository.findUserByIsConnectionFalse(groupRoom.getId());
String createUrl = groupRoom.getGroupCode() + "/schedule/" + savedSchedule.getId();

for (User member : notificationUsers) {
NotificationScheduleRequest request = NotificationScheduleRequest.builder()
.receiverCode(member.getUserCode())
.createdAt(LocalDateTime.now())
.message(savedSchedule.getTitle())
.groupName(groupRoom.getName())
.type(NotificationType.GROUP_SCHEDULE)
.url(createUrl)
.build();
notificationService.send(request);
}

notificationUsers.forEach(member -> {
GroupScheduleCreatedEvent event = new GroupScheduleCreatedEvent(this, member.getUserCode(), groupRoom.getName(), savedSchedule.getTitle(), "/groupRoom/" + groupRoom.getId() + "/" + savedSchedule.getId());
eventPublisher.publishEvent(event);
});

return ScheduleResponse.from(savedSchedule);
}
Expand All @@ -102,77 +89,35 @@ public ScheduleResponse createGroupSchedule(String groupCode, ScheduleRequest re
// 스케줄과 유저의 참여여부를 비교
// 현재 쿼리 8번
// 유저 , 스케줄, 그룹, 그룹유저, 참여여부 2명 조회
@Transactional(readOnly = true)
public GroupScheduleResponse getGroupScheduleById(UserInfo userInfo, Long groupRoomId, Long scheduleId) {
// 유저 프로필을 조회합니다.
User user = userQueryService.getUserProfile(userInfo.getId());
checkUserAccessToGroupRoom(groupRoomId, userInfo.getId());

// 해당 스케줄의 그룹룸을 가져옵니다.
GroupRoom groupRoom = groupQueryService.getGroupById(groupRoomId);

// 스케줄을 조회합니다.
Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);

// 그룹룸에 속한 유저 그룹들의 목록을 가져옵니다.
Set<UserGroup> userGroups = groupRoom.getUserGroups();
List<UserScheduleAttendance> attendances = getUserScheduleAttendances(groupRoom, scheduleId);

// 유저가 해당 그룹에 속해 있는지 확인합니다.
boolean isUserInGroup = userGroups.stream()
.anyMatch(userGroup -> userGroup.getUser().getId().equals(userInfo.getId()));

if (!isUserInGroup) {
// 유저가 그룹에 속해 있지 않다면, 예외 처리를 합니다.
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}

// 유저가 그룹에 속해 있다면, 필요한 스케줄 정보를 반환합니다.
// GroupScheduleResponse 구성 로직은 여기에 추가합니다.
return GroupScheduleResponse.from(schedule, groupRoom.getName(), getUserScheduleAttendances(groupRoom, userInfo.getId(), scheduleId));
return GroupScheduleResponse.from(schedule, groupRoom.getName(), attendances);
}

private List<UserScheduleAttendance> getUserScheduleAttendances(GroupRoom groupRoom, Long userId, Long scheduleId) {
return groupRoom.getUserGroups().stream()
.map(userGroup -> {
// 실제 출석 상태를 데이터베이스에서 조회하거나 다른 로직을 통해 결정합니다.
Optional<GroupScheduleAttendance> attendance = groupScheduleAttendanceRepository.findByUserIdAndScheduleIdAndStatusNot(userId, scheduleId, ScheduleStatus.UNDECIDED);

ScheduleStatus status = attendance
.map(GroupScheduleAttendance::getStatus)
.orElse(ScheduleStatus.UNDECIDED);

return new UserScheduleAttendance(
userGroup.getUser().getUserCode(),
userGroup.getUser().getUsername(),
status
);
})
// UNDECIDED인 상태 제외
.collect(Collectors.toList());
}


@Transactional(readOnly = true)
public List<GroupScheduleResponse> getSchedulesByGroupRoom(Long groupRoomId, UserInfo userInfo) {
// [1] 유저가 그룹룸에 접근할 권리가있는지 확인
checkUserAccessToGroupRoom(groupRoomId, userInfo.getId());
// [2] 그룹룸 ID를 기반으로 스케줄을 조회
List<Schedule> schedules = scheduleQueryService.findByGroupRoomId(groupRoomId);
GroupRoom groupRoom = groupQueryService.getGroupById(groupRoomId);
// [3] dto 반환
return scheduleQueryService.findByGroupRoomId(groupRoomId).stream()
.map(schedule -> {
List<UserScheduleAttendance> attendances = getUserScheduleAttendances(groupRoom, userInfo.getId(), schedule.getId());
return GroupScheduleResponse.from(schedule, groupRoom.getName(), attendances);
})

return schedules.stream()
.map(schedule -> GroupScheduleResponse.from(schedule, groupRoom.getName(), getUserScheduleAttendances(groupRoom, schedule.getId())))
.collect(Collectors.toList());
}

public ScheduleResponse updateScheduleByGroupRoom(Long groupRoomId, Long scheduleId, ScheduleRequest scheduleRequest, UserInfo userInfo) {
// [1] 그룹룸에 수정하려는 유저가 존재하는지 확인
checkUserAccessToGroupRoom(groupRoomId, userInfo.getId());
// [2] 스케줄을 업데이트

Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);
schedule.update(scheduleRequest.title(), scheduleRequest.content(), scheduleRequest.startTime(), scheduleRequest.endTime());
// [3] dto 반환

return ScheduleResponse.from(schedule);
}

Expand All @@ -182,9 +127,21 @@ public void deleteScheduleByGroupRoom(Long groupRoomId, Long scheduleId, UserInf
scheduleQueryService.deleteById(scheduleId);
}

// 유저가 그룹룸에 접근할 권리가있는지 확인
private void checkUserAccessToGroupRoom(Long groupRoomId, Long userId) {
userGroupQueryService.checkUserAccessToGroupRoom(groupRoomId, userId);
}

private List<UserScheduleAttendance> getUserScheduleAttendances(GroupRoom groupRoom, Long scheduleId) {
return groupRoom.getUserGroups().stream()
.map(userGroup -> {
Optional<GroupScheduleAttendance> attendance = groupScheduleAttendanceRepository.findByUserIdAndScheduleIdAndStatusNot(userGroup.getUser().getId(), scheduleId, ScheduleStatus.UNDECIDED);
ScheduleStatus status = attendance.map(GroupScheduleAttendance::getStatus).orElse(ScheduleStatus.UNDECIDED);
return new UserScheduleAttendance(userGroup.getUser().getUserCode(), userGroup.getUser().getUsername(), status);
})
.filter(attendance -> attendance.status() != ScheduleStatus.UNDECIDED)
.collect(Collectors.toList());
}


}

0 comments on commit 5c5f74c

Please sign in to comment.