Skip to content

Commit 5072e5c

Browse files
refactor: remove start_date_not_is_past_validation (#334)
1 parent a549718 commit 5072e5c

File tree

5 files changed

+54
-68
lines changed

5 files changed

+54
-68
lines changed

app/models/medical_shift_recurrence.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class MedicalShiftRecurrence < ApplicationRecord
3030
validate :day_of_month_blank_for_weekly
3131
validate :day_of_week_blank_for_monthly
3232
validate :end_date_after_start_date
33-
validate :start_date_not_in_past
3433

3534
scope :active, -> { where(deleted_at: nil) }
3635
scope :needs_generation, MedicalShiftRecurrences::NeedsGenerationQuery
@@ -54,10 +53,4 @@ def end_date_after_start_date
5453

5554
errors.add(:end_date, "End date must be after start date.")
5655
end
57-
58-
def start_date_not_in_past
59-
return unless start_date.present? && start_date < Time.zone.today
60-
61-
errors.add(:start_date, "Start date cannot be in the past.")
62-
end
6356
end

app/operations/medical_shift_recurrences/create.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def call
1414
create_recurrence
1515
initialize_shifts_array
1616
generate_shifts
17+
log_success
1718
end
1819

1920
private
@@ -23,7 +24,10 @@ def create_recurrence
2324
attributes.reverse_merge(user_id: user_id)
2425
)
2526

26-
fail!(error: medical_shift_recurrence.errors.full_messages) unless medical_shift_recurrence.save
27+
return if medical_shift_recurrence.save
28+
29+
log_error(medical_shift_recurrence.errors.full_messages)
30+
fail!(error: medical_shift_recurrence.errors.full_messages)
2731
end
2832

2933
def initialize_shifts_array
@@ -59,5 +63,18 @@ def shift_attributes(date)
5963
paid: false
6064
}
6165
end
66+
67+
def log_success
68+
Rails.logger.info(
69+
">>> MedicalShiftRecurrence created successfully. ID: #{medical_shift_recurrence.id}, " \
70+
"User ID: #{user_id}, Shifts Created: #{shifts_created.count}"
71+
)
72+
end
73+
74+
def log_error(errors)
75+
Rails.logger.error(
76+
">>> Failed to create MedicalShiftRecurrence. User ID: #{user_id}, Errors: #{errors.join(', ')}"
77+
)
78+
end
6279
end
6380
end

app/operations/medical_shift_recurrences/generate_pending.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ class GeneratePending < Actor
99
output :errors, type: Array, default: -> { [] }
1010

1111
def call
12+
Rails.logger.info(">>> Starting MedicalShiftRecurrences::GeneratePending for target_date: #{target_date}")
1213
self.processed = 0
1314
self.shifts_created = 0
1415
self.errors = []
1516

1617
MedicalShiftRecurrence.needs_generation(target_date:).find_each do |recurrence|
1718
process_recurrence(recurrence)
1819
end
20+
21+
log_summary
1922
end
2023

2124
private
@@ -37,6 +40,7 @@ def process_recurrence(recurrence)
3740
self.processed += 1
3841
self.shifts_created += created_count
3942
rescue StandardError => e
43+
Rails.logger.error(">>> Error processing recurrence #{recurrence.id}: #{e.message}")
4044
errors << { recurrence_id: recurrence.id, error: e.message }
4145
end
4246

@@ -51,5 +55,12 @@ def shift_attributes(recurrence, date)
5155
paid: false
5256
}
5357
end
58+
59+
def log_summary
60+
Rails.logger.info(
61+
">>> Finished MedicalShiftRecurrences::GeneratePending. " \
62+
"Processed: #{processed}, Shifts Created: #{shifts_created}, Errors: #{errors.count}"
63+
)
64+
end
5465
end
5566
end

spec/operations/medical_shift_recurrences/create_spec.rb

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,29 @@
6969
end
7070
end
7171

72+
it "is successful when start_date is in the past" do
73+
attributes = {
74+
frequency: "weekly",
75+
day_of_week: 1,
76+
start_date: Time.zone.yesterday,
77+
workload: "twelve",
78+
start_hour: "19:00",
79+
hospital_name: "Hospital Teste",
80+
amount_cents: 120_000
81+
}
82+
83+
result = described_class.result(attributes: attributes, user_id: user.id)
84+
85+
expect(result.success?).to be true
86+
expect(result.medical_shift_recurrence).to be_persisted
87+
end
88+
7289
context "when creating biweekly recurrence" do
7390
let(:attributes) do
7491
{
7592
frequency: "biweekly",
7693
day_of_week: 5,
77-
start_date: Date.current,
94+
start_date: Time.zone.today,
7895
workload: "twenty_four",
7996
start_hour: "07:00",
8097
hospital_name: "Hospital Central",
@@ -164,7 +181,7 @@
164181
it "fails when frequency is missing" do
165182
attributes = {
166183
day_of_week: 1,
167-
start_date: Date.tomorrow,
184+
start_date: Time.zone.tomorrow,
168185
workload: "12h",
169186
start_hour: "19:00",
170187
hospital_name: "Hospital Teste",
@@ -188,7 +205,7 @@
188205
it "returns an error for weekly without day_of_week" do
189206
attributes = {
190207
frequency: "weekly",
191-
start_date: Date.tomorrow,
208+
start_date: Time.zone.tomorrow,
192209
workload: MedicalShifts::Workloads::TWELVE,
193210
start_hour: "19:00",
194211
hospital_name: "Hospital Teste",
@@ -207,7 +224,7 @@
207224
attributes = {
208225
frequency: "weekly",
209226
day_of_month: 15,
210-
start_date: Date.tomorrow,
227+
start_date: Time.zone.tomorrow,
211228
workload: "12h",
212229
start_hour: "19:00",
213230
hospital_name: "Hospital Teste",
@@ -226,7 +243,7 @@
226243
attributes = {
227244
frequency: "monthly_fixed_day",
228245
day_of_week: 2,
229-
start_date: Date.tomorrow,
246+
start_date: Time.zone.tomorrow,
230247
workload: "12h",
231248
start_hour: "19:00",
232249
hospital_name: "Hospital Teste",
@@ -244,7 +261,7 @@
244261
it "returns an error for monthly_fixed_day without day_of_month" do
245262
attributes = {
246263
frequency: "monthly_fixed_day",
247-
start_date: Date.tomorrow,
264+
start_date: Time.zone.tomorrow,
248265
workload: MedicalShifts::Workloads::TWELVE,
249266
start_hour: "19:00",
250267
hospital_name: "Hospital Teste",
@@ -259,31 +276,12 @@
259276
)
260277
end
261278

262-
it "returns an error when start_date is in the past" do
263-
attributes = {
264-
frequency: "weekly",
265-
day_of_week: 1,
266-
start_date: Date.yesterday,
267-
workload: "12h",
268-
start_hour: "19:00",
269-
hospital_name: "Hospital Teste",
270-
amount_cents: 120_000
271-
}
272-
273-
result = described_class.result(attributes: attributes, user_id: user.id)
274-
275-
expect(result.error).to be_present
276-
expect(result.medical_shift_recurrence.errors.full_messages).to include(
277-
match(/Start date/)
278-
)
279-
end
280-
281279
it "returns an error when end_date is before start_date" do
282280
attributes = {
283281
frequency: "weekly",
284282
day_of_week: 1,
285-
start_date: Date.tomorrow,
286-
end_date: Date.current,
283+
start_date: Time.zone.tomorrow,
284+
end_date: Time.zone.today,
287285
workload: "12h",
288286
start_hour: "19:00",
289287
hospital_name: "Hospital Teste",

spec/requests/api/v1/medical_shift_recurrences_request_spec.rb

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -372,39 +372,6 @@
372372
end
373373
end
374374

375-
context "when start_date is in the past" do
376-
let(:invalid_params) do
377-
{
378-
medical_shift_recurrence: {
379-
frequency: "weekly",
380-
day_of_week: 1,
381-
start_date: Time.zone.yesterday,
382-
workload: MedicalShifts::Workloads::SIX,
383-
start_hour: "19:00:00",
384-
hospital_name: "Hospital Teste",
385-
amount_cents: 120_000
386-
}
387-
}
388-
end
389-
390-
it "returns unprocessable content status" do
391-
post "/api/v1/medical_shift_recurrences",
392-
params: invalid_params,
393-
headers: auth_headers
394-
395-
expect(response).to have_http_status(:unprocessable_content)
396-
end
397-
398-
it "returns validation error" do
399-
post "/api/v1/medical_shift_recurrences",
400-
params: invalid_params,
401-
headers: auth_headers
402-
403-
body = response.parsed_body
404-
expect(body["errors"]).to include(match(/Start date/))
405-
end
406-
end
407-
408375
context "when end_date is before start_date" do
409376
let(:invalid_params) do
410377
{

0 commit comments

Comments
 (0)