From 170649d8b676f8d9fa68e5de9aae00056514de8a Mon Sep 17 00:00:00 2001 From: Daniel Nottingham Date: Fri, 5 Dec 2025 09:48:42 -0300 Subject: [PATCH] refactor: remove start_date_not_is_past_validation --- app/models/medical_shift_recurrence.rb | 7 --- .../medical_shift_recurrences/create.rb | 19 ++++++- .../generate_pending.rb | 11 ++++ .../medical_shift_recurrences/create_spec.rb | 52 +++++++++---------- .../medical_shift_recurrences_request_spec.rb | 33 ------------ 5 files changed, 54 insertions(+), 68 deletions(-) diff --git a/app/models/medical_shift_recurrence.rb b/app/models/medical_shift_recurrence.rb index 8316b2c..9af09d4 100644 --- a/app/models/medical_shift_recurrence.rb +++ b/app/models/medical_shift_recurrence.rb @@ -30,7 +30,6 @@ class MedicalShiftRecurrence < ApplicationRecord validate :day_of_month_blank_for_weekly validate :day_of_week_blank_for_monthly validate :end_date_after_start_date - validate :start_date_not_in_past scope :active, -> { where(deleted_at: nil) } scope :needs_generation, MedicalShiftRecurrences::NeedsGenerationQuery @@ -54,10 +53,4 @@ def end_date_after_start_date errors.add(:end_date, "End date must be after start date.") end - - def start_date_not_in_past - return unless start_date.present? && start_date < Time.zone.today - - errors.add(:start_date, "Start date cannot be in the past.") - end end diff --git a/app/operations/medical_shift_recurrences/create.rb b/app/operations/medical_shift_recurrences/create.rb index bb1a614..d9c0749 100644 --- a/app/operations/medical_shift_recurrences/create.rb +++ b/app/operations/medical_shift_recurrences/create.rb @@ -14,6 +14,7 @@ def call create_recurrence initialize_shifts_array generate_shifts + log_success end private @@ -23,7 +24,10 @@ def create_recurrence attributes.reverse_merge(user_id: user_id) ) - fail!(error: medical_shift_recurrence.errors.full_messages) unless medical_shift_recurrence.save + return if medical_shift_recurrence.save + + log_error(medical_shift_recurrence.errors.full_messages) + fail!(error: medical_shift_recurrence.errors.full_messages) end def initialize_shifts_array @@ -59,5 +63,18 @@ def shift_attributes(date) paid: false } end + + def log_success + Rails.logger.info( + ">>> MedicalShiftRecurrence created successfully. ID: #{medical_shift_recurrence.id}, " \ + "User ID: #{user_id}, Shifts Created: #{shifts_created.count}" + ) + end + + def log_error(errors) + Rails.logger.error( + ">>> Failed to create MedicalShiftRecurrence. User ID: #{user_id}, Errors: #{errors.join(', ')}" + ) + end end end diff --git a/app/operations/medical_shift_recurrences/generate_pending.rb b/app/operations/medical_shift_recurrences/generate_pending.rb index 5c92609..2ca956f 100644 --- a/app/operations/medical_shift_recurrences/generate_pending.rb +++ b/app/operations/medical_shift_recurrences/generate_pending.rb @@ -9,6 +9,7 @@ class GeneratePending < Actor output :errors, type: Array, default: -> { [] } def call + Rails.logger.info(">>> Starting MedicalShiftRecurrences::GeneratePending for target_date: #{target_date}") self.processed = 0 self.shifts_created = 0 self.errors = [] @@ -16,6 +17,8 @@ def call MedicalShiftRecurrence.needs_generation(target_date:).find_each do |recurrence| process_recurrence(recurrence) end + + log_summary end private @@ -37,6 +40,7 @@ def process_recurrence(recurrence) self.processed += 1 self.shifts_created += created_count rescue StandardError => e + Rails.logger.error(">>> Error processing recurrence #{recurrence.id}: #{e.message}") errors << { recurrence_id: recurrence.id, error: e.message } end @@ -51,5 +55,12 @@ def shift_attributes(recurrence, date) paid: false } end + + def log_summary + Rails.logger.info( + ">>> Finished MedicalShiftRecurrences::GeneratePending. " \ + "Processed: #{processed}, Shifts Created: #{shifts_created}, Errors: #{errors.count}" + ) + end end end diff --git a/spec/operations/medical_shift_recurrences/create_spec.rb b/spec/operations/medical_shift_recurrences/create_spec.rb index 93a7a47..cc9f0cb 100644 --- a/spec/operations/medical_shift_recurrences/create_spec.rb +++ b/spec/operations/medical_shift_recurrences/create_spec.rb @@ -69,12 +69,29 @@ end end + it "is successful when start_date is in the past" do + attributes = { + frequency: "weekly", + day_of_week: 1, + start_date: Time.zone.yesterday, + workload: "twelve", + start_hour: "19:00", + hospital_name: "Hospital Teste", + amount_cents: 120_000 + } + + result = described_class.result(attributes: attributes, user_id: user.id) + + expect(result.success?).to be true + expect(result.medical_shift_recurrence).to be_persisted + end + context "when creating biweekly recurrence" do let(:attributes) do { frequency: "biweekly", day_of_week: 5, - start_date: Date.current, + start_date: Time.zone.today, workload: "twenty_four", start_hour: "07:00", hospital_name: "Hospital Central", @@ -164,7 +181,7 @@ it "fails when frequency is missing" do attributes = { day_of_week: 1, - start_date: Date.tomorrow, + start_date: Time.zone.tomorrow, workload: "12h", start_hour: "19:00", hospital_name: "Hospital Teste", @@ -188,7 +205,7 @@ it "returns an error for weekly without day_of_week" do attributes = { frequency: "weekly", - start_date: Date.tomorrow, + start_date: Time.zone.tomorrow, workload: MedicalShifts::Workloads::TWELVE, start_hour: "19:00", hospital_name: "Hospital Teste", @@ -207,7 +224,7 @@ attributes = { frequency: "weekly", day_of_month: 15, - start_date: Date.tomorrow, + start_date: Time.zone.tomorrow, workload: "12h", start_hour: "19:00", hospital_name: "Hospital Teste", @@ -226,7 +243,7 @@ attributes = { frequency: "monthly_fixed_day", day_of_week: 2, - start_date: Date.tomorrow, + start_date: Time.zone.tomorrow, workload: "12h", start_hour: "19:00", hospital_name: "Hospital Teste", @@ -244,7 +261,7 @@ it "returns an error for monthly_fixed_day without day_of_month" do attributes = { frequency: "monthly_fixed_day", - start_date: Date.tomorrow, + start_date: Time.zone.tomorrow, workload: MedicalShifts::Workloads::TWELVE, start_hour: "19:00", hospital_name: "Hospital Teste", @@ -259,31 +276,12 @@ ) end - it "returns an error when start_date is in the past" do - attributes = { - frequency: "weekly", - day_of_week: 1, - start_date: Date.yesterday, - workload: "12h", - start_hour: "19:00", - hospital_name: "Hospital Teste", - amount_cents: 120_000 - } - - result = described_class.result(attributes: attributes, user_id: user.id) - - expect(result.error).to be_present - expect(result.medical_shift_recurrence.errors.full_messages).to include( - match(/Start date/) - ) - end - it "returns an error when end_date is before start_date" do attributes = { frequency: "weekly", day_of_week: 1, - start_date: Date.tomorrow, - end_date: Date.current, + start_date: Time.zone.tomorrow, + end_date: Time.zone.today, workload: "12h", start_hour: "19:00", hospital_name: "Hospital Teste", diff --git a/spec/requests/api/v1/medical_shift_recurrences_request_spec.rb b/spec/requests/api/v1/medical_shift_recurrences_request_spec.rb index 6d1413f..57b97e6 100644 --- a/spec/requests/api/v1/medical_shift_recurrences_request_spec.rb +++ b/spec/requests/api/v1/medical_shift_recurrences_request_spec.rb @@ -372,39 +372,6 @@ end end - context "when start_date is in the past" do - let(:invalid_params) do - { - medical_shift_recurrence: { - frequency: "weekly", - day_of_week: 1, - start_date: Time.zone.yesterday, - workload: MedicalShifts::Workloads::SIX, - start_hour: "19:00:00", - hospital_name: "Hospital Teste", - amount_cents: 120_000 - } - } - end - - it "returns unprocessable content status" do - post "/api/v1/medical_shift_recurrences", - params: invalid_params, - headers: auth_headers - - expect(response).to have_http_status(:unprocessable_content) - end - - it "returns validation error" do - post "/api/v1/medical_shift_recurrences", - params: invalid_params, - headers: auth_headers - - body = response.parsed_body - expect(body["errors"]).to include(match(/Start date/)) - end - end - context "when end_date is before start_date" do let(:invalid_params) do {