Skip to content

Commit 2c8dffa

Browse files
committed
운영시간 시간 저장 타입 수정
1 parent 78962fe commit 2c8dffa

1 file changed

Lines changed: 27 additions & 35 deletions

File tree

grpc_service/service/reservation_service.rb

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,31 @@ def parse_datetime(str)
2929
Time.zone.parse(str)
3030
end
3131

32-
# 날짜 + HH:mm → Time
32+
# 날짜 + HH:mm → Time (기존 그대로 둠 — 예외시간용)
3333
def combine_date_time(date, hhmm)
3434
h, m = hhmm.split(":").map(&:to_i)
3535
Time.new(date.year, date.month, date.day, h, m, 0, "+09:00")
3636
end
3737

38-
# 운영시간 체크
38+
# HH:mm → 총 minutes 변환 (⭐ 새로 추가된 함수)
39+
def hhmm_to_minutes(hhmm)
40+
return Float::INFINITY if hhmm.nil? || hhmm.strip == ""
41+
h, m = hhmm.split(":").map(&:to_i)
42+
(h * 60) + m
43+
end
44+
45+
# 운영시간 체크 (⭐ 날짜 기반 Time 비교 → 분 비교로 변경)
3946
def within_operating_hours?(start_time, end_time, op)
40-
date = start_time.to_date
41-
op_start = combine_date_time(date, op.opening_time)
42-
op_end = combine_date_time(date, op.closing_time)
47+
start_m = start_time.hour * 60 + start_time.min
48+
end_m = end_time.hour * 60 + end_time.min
4349

44-
start_time >= op_start && end_time <= op_end
50+
op_start = hhmm_to_minutes(op.opening_time)
51+
op_end = hhmm_to_minutes(op.closing_time)
52+
53+
start_m >= op_start && end_m <= op_end
4554
end
4655

47-
# 예외시간 체크
56+
# 예외시간 체크 (기존 유지)
4857
def conflict_with_exception?(start_time, end_time, ex)
4958
date = start_time.to_date
5059
ex_start = combine_date_time(date, ex.opening_time)
@@ -60,23 +69,6 @@ def conflict_with_exception?(start_time, end_time, ex)
6069
def fetch_users(user_codes)
6170
return [] if user_codes.blank?
6271

63-
# =======================
64-
# (1) 실서비스 연동 버전
65-
# =======================
66-
# begin
67-
# stub = Bannote::Userservice::User::V1::UserService::Stub.new(
68-
# "user-service:50051", :this_channel_is_insecure
69-
# )
70-
# resp = stub.get_users(GetUsersRequest.new(user_codes: user_codes))
71-
# return resp.users
72-
# rescue => e
73-
# Rails.logger.error("[UserService] fetch_users failed: #{e.message}")
74-
# return []
75-
# end
76-
77-
# =======================
78-
# (2) 현재 mock 데이터 버전
79-
# =======================
8072
user_codes.map do |code|
8173
::Bannote::Studyroomservice::Reservation::V1::User.new(
8274
user_code: code,
@@ -111,15 +103,19 @@ def create_reservation(request, _call)
111103
)
112104
raise_precondition("No operating hour defined") unless op
113105

106+
# 운영시간 체크 (⭐ 수정된 함수 사용)
114107
unless within_operating_hours?(start_time, end_time, op)
115108
raise_precondition("Reservation time is outside operating hours")
116109
end
117110

118-
# 최대 운영시간 체크
119-
if op.day_maximum_time.present?
120-
max_hours = op.day_maximum_time.to_i
121-
duration_hours = (end_time - start_time) / 3600.0
122-
raise_precondition("Reservation exceeds maximum operating time (#{max_hours} hours)") if duration_hours > max_hours
111+
# 최대 운영시간 체크 (⭐ HH:mm 기반으로 수정)
112+
max_minutes = hhmm_to_minutes(op.day_maximum_time)
113+
duration_minutes = ((end_time - start_time) / 60).to_i
114+
115+
if duration_minutes > max_minutes
116+
raise_precondition(
117+
"Reservation exceeds maximum operating time (#{op.day_maximum_time})"
118+
)
123119
end
124120

125121
# 예외시간 확인
@@ -179,17 +175,15 @@ def get_reservation(request, _call)
179175
end
180176

181177
# =====================================================================
182-
# 3. 목록 조회 (여러 개 room_id + 범위)
178+
# 3. 목록 조회
183179
# =====================================================================
184180
def list_reservations(request, _call)
185181
authorize!("student")
186182

187183
reservations = ::Reservation.where(deleted_at: nil)
188184

189-
# 여러 개 room_ids 처리
190185
reservations = reservations.where(room_id: request.room_ids.to_a) if request.room_ids.any?
191186

192-
# 시간 범위
193187
reservations = reservations.where("start_time >= ?", parse_datetime(request.start_time_after)) if request.start_time_after.present?
194188
reservations = reservations.where("end_time <= ?", parse_datetime(request.end_time_before)) if request.end_time_before.present?
195189

@@ -215,7 +209,7 @@ def update_reservation(request, _call)
215209
room = ::Room.find_by(id: request.room_id || reservation.room_id)
216210
raise_not_found("Room") unless room
217211

218-
# 운영시간 검사
212+
# 운영시간 검사 (⭐ 수정된 함수 사용)
219213
op = ::RoomOperatingHour.find_by(
220214
room_id: room.id,
221215
day_of_week: start_time.wday,
@@ -235,7 +229,6 @@ def update_reservation(request, _call)
235229
.exists?
236230
raise_precondition("Reservation time overlaps") if overlap
237231

238-
# user_codes 업데이트
239232
reservation.user_codes = request.user_codes.to_a if request.user_codes.any?
240233

241234
reservation.update!(
@@ -297,7 +290,6 @@ def raise_permission(msg)
297290
raise GRPC::BadStatus.new(GRPC::Core::StatusCodes::PERMISSION_DENIED, msg)
298291
end
299292

300-
301293
# ------------------------------------------------------------
302294
# Reservation → proto 변환 (users 포함)
303295
# ------------------------------------------------------------

0 commit comments

Comments
 (0)