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
7 changes: 0 additions & 7 deletions app/models/medical_shift_recurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
19 changes: 18 additions & 1 deletion app/operations/medical_shift_recurrences/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def call
create_recurrence
initialize_shifts_array
generate_shifts
log_success
end

private
Expand All @@ -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
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions app/operations/medical_shift_recurrences/generate_pending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ 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 = []

MedicalShiftRecurrence.needs_generation(target_date:).find_each do |recurrence|
process_recurrence(recurrence)
end

log_summary
end

private
Expand All @@ -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

Expand All @@ -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
52 changes: 25 additions & 27 deletions spec/operations/medical_shift_recurrences/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
33 changes: 0 additions & 33 deletions spec/requests/api/v1/medical_shift_recurrences_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down