Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public enum SosException implements BaseException {
ALREADY_RESPONDER_EXIST(HttpStatus.BAD_REQUEST, 5002, "이미 응답자가 존재합니다."),
CANNOT_RESPOND_TO_OWN_SOS(HttpStatus.FORBIDDEN, 5003, "자신이 요청한 SOS에는 응답할 수 없습니다."),
CANNOT_CONNECT_SSE(HttpStatus.BAD_REQUEST, 5004, "SSE 연결에 실패하였습니다."),
INSUFFICIENT_DATA(HttpStatus.BAD_REQUEST, 5005, "남아 있는 데이터량이 부족합니다.")
INSUFFICIENT_DATA(HttpStatus.BAD_REQUEST, 5005, "남아 있는 데이터량이 부족합니다."),
ALREADY_REQUEST_SOS(HttpStatus.BAD_REQUEST, 5006, "이번 달에는 이미 SOS를 신청하였습니다.")
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface SosRepository extends JpaRepository<Sos, Long>, SosQueryReposit
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT s FROM Sos s WHERE s.id = :id")
Optional<Sos> findByIdForUpdate(@Param("id") Long id);

Optional<Sos> findFirstByRequesterIdOrderByCreatedAtDesc(final Long requesterId);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.TwoSeaU.BaData.domain.sos.service;

import java.time.YearMonth;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.TwoSeaU.BaData.domain.sos.dto.response.RespondSosResponse;
Expand Down Expand Up @@ -31,6 +34,16 @@ public SaveSosResponse requestSos(final String username) {
final User user = userRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(UserException.USER_NOT_FOUND));

final Optional<Sos> latestSos = sosRepository.findFirstByRequesterIdOrderByCreatedAtDesc(user.getId());
if(latestSos.isPresent()) {
final YearMonth latestSosYearMonth = YearMonth.from(latestSos.get().getCreatedAt());
final YearMonth nowYearMonth = YearMonth.now();

if(latestSosYearMonth.equals(nowYearMonth) && latestSos.get().getResponder() != null) {
throw new GeneralException(SosException.ALREADY_REQUEST_SOS);
}
}

final Sos savedSos = sosRepository.save(Sos.of(user));

sseService.broadcast(sosMessage);
Expand Down