Skip to content

Commit

Permalink
Possibilitando reagendar apos cancelamento de D2 (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat-Bit authored Sep 8, 2021
1 parent 5016461 commit 913b18c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
29 changes: 18 additions & 11 deletions app/controllers/community/appointments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ def home

return unless current_patient.can_schedule?

@appointments_count = Appointment.available_doses
.where(start: from..to, ubs_id: allowed_ubs_ids)
.count
if current_patient.doses.exists?
@appointments_count = Appointment.waiting.not_scheduled
.where(start: from..to, ubs_id: allowed_ubs_ids)
.count
else
@appointments_count = Appointment.available_doses
.where(start: from..to, ubs_id: allowed_ubs_ids)
.count
end
end
# rubocop:enable Metrics/AbcSize

Expand All @@ -30,18 +36,15 @@ def index

# If patient already had a dose, show only UBS that are 'enabled_for_reschedule'.
if current_patient.doses.exists?
ubs_id = Ubs.where(enabled_for_reschedule: true).pluck(:id)
rescheduled = true
else
# Otherwise limit to where they can schedule
ubs_id = allowed_ubs_ids
rescheduled = false
end

@days = parse_days
@days = parse_days(rescheduled)
@appointments = scheduler.open_times_per_ubs(from: @days.days.from_now.beginning_of_day,
to: @days.days.from_now.end_of_day,
filter_ubs_id: ubs_id,
filter_ubs_id: allowed_ubs_ids,
reschedule: rescheduled)
.sort_by { |ubs, _appointments| ubs.name }
rescue AppointmentScheduler::NoFreeSlotsAhead
Expand Down Expand Up @@ -147,11 +150,11 @@ def success_message(desired_date, scheduled_date)
end

# Loads pages for the Index, between 0 and max possible allowed
def parse_days
def parse_days(reschedule)
[
[
0,
params[:page].presence&.to_i || scheduler.days_ahead_with_open_slot
params[:page].presence&.to_i || scheduler.days_ahead_with_open_slot(reschedule: reschedule)
].compact.max,
Rails.configuration.x.schedule_up_to_days
].min
Expand All @@ -164,7 +167,11 @@ def parse_start
end

def allowed_ubs_ids
current_patient.conditions.flat_map(&:ubs_ids).uniq
if current_patient.doses.exists?
Ubs.where(enabled_for_reschedule: true).pluck(:id)
else
current_patient.conditions.flat_map(&:ubs_ids).uniq
end
end

def create_params
Expand Down
12 changes: 7 additions & 5 deletions app/services/appointment_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ def open_times_per_ubs(from:, to:, filter_ubs_id: nil, reschedule: false)
end

# Returns how many days ahead there are available appointments
def days_ahead_with_open_slot
next_available_appointment = Appointment.available_doses
.where(start: earliest_allowed..latest_allowed)
.order(:start)
.pick(:start)
def days_ahead_with_open_slot(reschedule: false)
appointments = Appointment.waiting.not_scheduled if reschedule
appointments ||= Appointment.available_doses

next_available_appointment = appointments.where(start: earliest_allowed..latest_allowed)
.order(:start)
.pick(:start)

raise NoFreeSlotsAhead unless next_available_appointment

Expand Down

0 comments on commit 913b18c

Please sign in to comment.