Skip to content

Commit

Permalink
[FIX] 브리핑 보드 알람 타임라인 정렬 에러 해결 (#168)
Browse files Browse the repository at this point in the history
* [FIX] 쿼리 조건 간단화 및 cross join 방지

* [FIX] cross join 방지 및 미션 기록 생성일자로 정렬 순서 변경

* #133 [refactor, feat] 개인/그룹 알람 목록 가져오기 쿼리를 동적으로 생성 및 활성화 알람을 상단으로 표시하기

* #133 [feat] 알람 목록 쿼리 조건 메서드 분리 및 alarm type 에러 처리 세분화

* #133 [refactor] 필요없는 주석 제거

* #133 [FIX] 알람 활성화 여부에 따른 정렬 삭제
  • Loading branch information
05AM authored Sep 18, 2024
1 parent 8f1cb42 commit 73a08b6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package com.wakeUpTogetUp.togetUp.api.alarm.domain;

import com.wakeUpTogetUp.togetUp.common.Status;
import com.wakeUpTogetUp.togetUp.exception.BaseException;
import java.util.Arrays;

public enum AlarmType {
PERSONAL,
GROUP;

public static AlarmType getByName(String name) {
return Arrays.stream(values())
.filter(value -> value.name().equals(name))
.findFirst()
.orElseThrow(
() -> new BaseException(Status.ALARM_TYPE_NOT_FOUND)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.wakeUpTogetUp.togetUp.api.alarm.repository;

import com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.AlarmSimpleRes;
import com.wakeUpTogetUp.togetUp.api.alarm.domain.AlarmType;
import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm;
import java.time.LocalDateTime;
import java.util.List;

public interface AlarmQueryRepository {

List<AlarmSimpleRes> findAllUserTodayActiveAlarmsAfterNow(Integer userId, LocalDateTime now);

List<Alarm> findUserAlarmsByType(Integer userId, AlarmType type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.AlarmSimpleRes;
import com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.QAlarmSimpleRes;
import com.wakeUpTogetUp.togetUp.api.alarm.domain.AlarmType;
import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm;
import com.wakeUpTogetUp.togetUp.common.Status;
import com.wakeUpTogetUp.togetUp.common.annotation.LogExecutionTime;
import com.wakeUpTogetUp.togetUp.exception.BaseException;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand All @@ -27,17 +31,20 @@ public List<AlarmSimpleRes> findAllUserTodayActiveAlarmsAfterNow(Integer userId,
.select(
new QAlarmSimpleRes(
alarm.id,
alarm.missionObject.icon,
missionObject.icon,
alarm.alarmTime,
alarm.name,
missionObject.kr,
alarm.room.id
)
)
.from(alarm)
.innerJoin(missionObject).on(alarm.missionObject.id.eq(missionObject.id))
.join(alarm.missionObject, missionObject)
.where(
alarm.user.id.eq(userId)
.and(alarm.isDeleted.eq(false))
.and(alarm.isActivated.eq(true))
.and(alarmTimeGoe(now.toLocalTime()))
.and(isDayOrOneTimeAlarm(now))
)
.orderBy(
Expand All @@ -47,6 +54,19 @@ public List<AlarmSimpleRes> findAllUserTodayActiveAlarmsAfterNow(Integer userId,
.fetch();
}

@Override
public List<Alarm> findUserAlarmsByType(Integer userId, AlarmType type) {
return query.selectFrom(alarm)
.where(
alarm.user.id.eq(userId),
alarm.isDeleted.eq(false),
getAlarmTypeCondition(type)
)
.orderBy(
alarm.alarmTime.asc()
)
.fetch();
}

private BooleanExpression alarmTimeGoe(LocalTime now) {
return alarm.alarmTime.goe(now);
Expand All @@ -55,8 +75,7 @@ private BooleanExpression alarmTimeGoe(LocalTime now) {
@LogExecutionTime
private BooleanExpression isDayOrOneTimeAlarm(LocalDateTime now) {
return isDayAlarm(now.getDayOfWeek())
.or(isOneTimeAlarm())
.and(isActiveAlarm(now.toLocalTime()));
.or(isOneTimeAlarm());
}

private BooleanExpression isDayAlarm(DayOfWeek dayOfWeek) {
Expand Down Expand Up @@ -90,9 +109,14 @@ private BooleanExpression isOneTimeAlarm() {
.and(alarm.sunday.eq(false));
}

private BooleanExpression isActiveAlarm(LocalTime now) {
return alarm.isActivated.eq(true)
.and(alarm.isDeleted.eq(false))
.and(alarmTimeGoe(now));
private BooleanExpression getAlarmTypeCondition(AlarmType type) {
switch (type) {
case PERSONAL:
return alarm.room.isNull();
case GROUP:
return alarm.room.isNotNull();
default:
throw new BaseException(Status.BAD_REQUEST_PARAM);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,20 @@ public interface AlarmRepository extends JpaRepository<Alarm, Integer>, AlarmQue
@Query("SELECT a.id FROM Alarm a WHERE a.user.id = :userId")
Set<Integer> findUserAlarmIds(@Param("userId") Integer userId);

List<Alarm> findAllByUser_IdAndRoom_IdIsNullOrderByAlarmTime(Integer userId);

@Query("SELECT a "
+ "FROM Alarm a "
+ "WHERE a.user.id = :userId "
+ "AND a.room.id != null "
+ "ORDER BY a.alarmTime")
List<Alarm> findRoomAlarmByUserId(@Param("userId") Integer userId);

@LogExecutionTime
@Query("SELECT new com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.AlarmSimpleRes("
+ "a.id, "
+ "a.missionObject.icon, "
+ "mo.icon, "
+ "ml.createdAt, "
+ "a.name, "
+ "mo.kr, "
+ "a.room.id) "
+ "FROM Alarm a "
+ "JOIN MissionObject mo ON mo.id = a.missionObject.id "
+ "JOIN MissionLog ml ON a.id = ml.alarm.id "
+ "INNER JOIN MissionLog ml ON a.id = ml.alarm.id "
+ "INNER JOIN MissionObject mo ON mo.id = a.missionObject.id "
+ "WHERE a.user.id = :userId "
+ "AND ml.createdAt >= :baseTime "
+ "ORDER BY a.alarmTime ASC, a.id ASC")
+ "ORDER BY ml.createdAt ASC, a.id ASC")
List<AlarmSimpleRes> findAllUserAlarmsWithTodayLog(@Param("userId") Integer userId, @Param("baseTime") LocalDateTime baseTime);

@Query("SELECT a.missionObject FROM Alarm a WHERE a.room.id = :roomId ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,8 @@ public AlarmDetailRes getAlarmById(Integer alarmId) {
public List<AlarmDetailRes> getAlarmsByUserIdOrderByDate(Integer userId, String type) {
userValidationService.validateUserExist(userId);

List<Alarm> alarms;
switch (AlarmType.valueOf(type)) {
case PERSONAL:
alarms = alarmRepository.findAllByUser_IdAndRoom_IdIsNullOrderByAlarmTime(userId);
break;
case GROUP:
alarms = alarmRepository.findRoomAlarmByUserId(userId);
break;
default:
throw new BaseException(Status.BAD_REQUEST_PARAM);
}
AlarmType alarmType = AlarmType.getByName(type);
List<Alarm> alarms = alarmRepository.findUserAlarmsByType(userId, alarmType);

return EntityDtoMapper.INSTANCE.toAlarmDetailResList(alarms);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum Status {
INVALID_FILE_CONTENT_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "이미지 컨텐트 타입이 유효하지 않습니다."),
APP_VERSION_HIGHER_THAN_LATEST_EXCEPTION(HttpStatus.BAD_REQUEST, "데이터 내의 최신 버전보다 높은 버전입니다."),
TIME_ZONE_ID_NOT_EXIST(HttpStatus.BAD_REQUEST, "유효하지 않은 시간대입니다."),
INVALID_ALARM_TYPE(HttpStatus.BAD_REQUEST, "알람 타입이 유효하지 않습니다."),

// FORBIDDEN
USER_AVATAR_LOCKED(HttpStatus.FORBIDDEN, "유저가 보유하지 않은 아바타 ID 입니다."),
Expand All @@ -66,6 +67,7 @@ public enum Status {
FILE_NOT_FOUND(HttpStatus.NOT_FOUND, "파일을 찾을 수 없습니다."),
ACCOUNT_DOESNT_EXISTS(HttpStatus.NOT_FOUND, "계정이 존재하지 않습니다."),
ROOM_USER_NOT_FOUND(HttpStatus.NOT_FOUND, "그룹의 해당 멤버가 없습니다."),
ALARM_TYPE_NOT_FOUND(HttpStatus.BAD_REQUEST, "존재하지 않는 알람 타입입니다."),

// CONFLICT
USER_AVATAR_ALREADY_EXIST(HttpStatus.CONFLICT, "이미 보유한 아바타 입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface EntityDtoMapper {
GetMissionWithObjectListRes toGetMissionRes(Mission mission);


// GetMissionLogRes
// MissionLog
@Mapping(target = "userId", source = "user.id")
@Mapping(target = "roomId", source = "room.id")
GetMissionLogRes toMissionLogRes(MissionLog missionLog);
Expand Down

0 comments on commit 73a08b6

Please sign in to comment.