Skip to content
Closed
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
15 changes: 7 additions & 8 deletions app/controllers/account/appointments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Account
class AppointmentsController < BaseController
include AppointmentSlots

before_action :load_appointment_for_editing, only: [:edit, :update, :destroy]
before_action :load_appointment_for_editing, only: [:edit, :update]
before_action :are_appointments_enabled?

def index
Expand Down Expand Up @@ -49,23 +49,22 @@ def update
end
end

def destroy
@appointment.destroy
redirect_to account_appointments_path, flash: {success: "Appointment cancelled."}, status: :see_other
end

private

def appointment_params
form_params = params.require(:appointment).permit(:comment, :time_range_string, hold_ids: [], loan_ids: [])

{
result = {
holds: @member.holds.where(id: form_params[:hold_ids]),
loans: @member.loans.where(id: form_params[:loan_ids]),
comment: form_params[:comment],
time_range_string: form_params[:time_range_string],
member_updating: true
}

# Only allow time_range_string for new appointments
result[:time_range_string] = form_params[:time_range_string] if @appointment.new_record?

result
end

def load_holds_and_loans
Expand Down
29 changes: 14 additions & 15 deletions app/views/account/appointments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,21 @@

<div class="columns">
<div class="column col-8 col-sm-12 col-mr-auto">
<div class="form-group" data-controller="appointment-date">
<label class="form-label h3" for="time_range_string">Select a time for the appointment</label>
<%= select("appointment", "time_range_string", grouped_options_for_select(@appointment_slots, @appointment.time_range_string), {include_blank: "Select a Date"},
class: "form-select", data: {action: "appointment-date#sync", "appointment-date-target": "select"}, disabled: !can_schedule_appointment) %>
<span data-appointment-date-target="display"></span>
<% if @appointment.new_record? %>
<div class="form-group" data-controller="appointment-date">
<label class="form-label h3" for="time_range_string">Select a time for the appointment</label>
<%= select("appointment", "time_range_string", grouped_options_for_select(@appointment_slots, @appointment.time_range_string), {include_blank: "Select a Date"},
class: "form-select", data: {action: "appointment-date#sync", "appointment-date-target": "select"}, disabled: !can_schedule_appointment) %>
<span data-appointment-date-target="display"></span>
<p>Don't worry if you arrive a little bit early or late, as long as it's within the library's open hours.</p>
</div>
<% else %>
<div class="form-group">
<label class="form-label h3">Appointment Time</label>
<p class="text-bold"><%= @appointment.starts_at.strftime("%A, %B %-d, %Y") %> between <%= appointment_time(@appointment) %></p>
<p>Don't worry if you arrive a little bit early or late, as long as it's within the library's open hours.</p>
</div>
</div>
<% end %>

<div class="form-group mb-2">
<label class="form-label" for="appointment_comment">Tell us about the project you're working on. We may be able to recommend a different or additional tool. If you're dropping off, please let us know if you had any issues with the items you're returning.</label>
Expand All @@ -100,15 +108,6 @@
</div>
<% end %>

<% unless @appointment.new_record? %>
<div class="columns cancel-appointment">
<div class="column col-8 col-sm-12 col-mr-auto">
<h4 class="h5">Need to cancel?</h4>
<p>You can make a new appointment to pick up or return items at a later time.</p>
<%= button_to "Cancel Appointment", account_appointment_path(@appointment), class: "btn", method: :delete, data: {turbo_submits_with: "Canceling appointment...", turbo_confirm: "Are you sure to cancel this appointment?"} %>
</div>
</div>
<% end %>
<% else %>
<div class="toast toast-warning">
<p>You can only schedule an appointment if you have at least one item that is:</p>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

namespace :account do
resources :appointments, only: [:index, :new, :create, :edit, :update, :destroy]
resources :appointments, only: [:index, :new, :create, :edit, :update]
resources :borrow_policy_approvals, only: [:create]
resources :holds, only: [:index, :create, :destroy] do
get :history, to: "holds#history", on: :collection
Expand Down
16 changes: 7 additions & 9 deletions test/controllers/account/appointments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,26 @@ class AppointmentsControllerTest < ActionDispatch::IntegrationTest
assert_equal 1, @member.appointments.count
end

test "should cancel appointment without affecting holds" do
create_appointment
delete account_appointment_path(@appointment)
assert_equal 1, @member.holds.count, "Member holds should not be deleted when an appointment is cancelled"
assert_redirected_to account_appointments_path
end

test "should get edit appointment" do
create_appointment
get edit_account_appointment_path(@appointment)
assert_response :success
end

test "should update appointment" do
test "should update appointment items but not time" do
create_appointment
assert_equal 1, @appointment.holds.count
original_starts_at = @appointment.starts_at
original_ends_at = @appointment.ends_at

@hold2 = FactoryBot.create(:hold, member: @member)
put account_appointment_path(@appointment), params: {appointment: {hold_ids: [@hold.id, @hold2.id], time_range_string: @appointment.time_range_string, comment: @appointment.comment}}
@event = FactoryBot.create(:appointment_slot_event)
put account_appointment_path(@appointment), params: {appointment: {hold_ids: [@hold.id, @hold2.id], time_range_string: "#{@event.start}..#{@event.finish}", comment: @appointment.comment}}
@appointment.reload

assert_equal 2, @appointment.holds.count
assert_equal original_starts_at, @appointment.starts_at, "Appointment time should not change"
assert_equal original_ends_at, @appointment.ends_at, "Appointment time should not change"
assert_redirected_to account_appointments_path
end

Expand Down
3 changes: 0 additions & 3 deletions test/system/appointments_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,10 @@ def fill_in_optional_field(text)
check_list_item_with_name(@held_item2.complete_number)
check_list_item_with_name(@borrowed_item2.complete_number)

selected_date = select_first_available_date

fill_in_optional_field("Updated appointment")
click_on "Update Appointment"

assert_text "Appointments"
assert_text selected_date

assert_text "Updated appointment"

Expand Down
Loading