Class: Api::Dashboard::V2::ReservationsController

Inherits:
BaseController show all
Includes:
MoneyRails::ActionViewExtension, ResponseCacheConcern
Defined in:
app/controllers/api/dashboard/v2/reservations_controller.rb

Instance Method Summary collapse

Methods included from ResponseCacheConcern

#my_response_cache

Methods inherited from BaseController

#set_options

Methods inherited from BaseController

#current_user, #identity_cache_memoization, #restaurants, #set_options

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params

Instance Method Details

#_action_for(action) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 71

def _action_for(action)
  service = ReservationConfirmationService.new(@reservation.id, confirmed_by: :owner)
  success = if action == 'confirm'
              message = 'Reservation confirmed successfully'
              service.accept
            else
              message = 'Reservation rejected successfully'
              service.reject
            end

  data = Api::Dashboard::V2::ReservationSerializer.new(@reservation.reload).as_json

  if success
    render json: { success: true, message: message }.merge(data), status: :ok
  else
    error(service.error_message_simple, :unprocessable_entity)
  end
end

#confirmObject



63
64
65
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 63

def confirm
  _action_for('confirm')
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 13

def index
  reservations = Reservation.joins(:restaurant).
    includes(:user, :customer, :property, :charges,
             :booking_channel, :reward,
             restaurant: :translations).
    where(restaurant_id: restaurants.ids).
    where(adjusted: false).
    exclude_temporary

  filter_params = params

  order_params = params.fetch(:order, '').split(',') || []

  filter = Api::Dashboard::V2::ReservationsFilter.new(restaurants, reservations)
  filter.order_by(order_params)
  filter.filter(filter_params) if filter_params.present?
  filter.build_collections

  reservations = nil

  begin
    pagy, reservations = pagy(filter.collections)
  rescue Pagy::OverflowError
    params[:page] = 1
    pagy, reservations = pagy(Reservation.where('1 = 0'))
  end

  total_sum_party_size = restaurants.map(&:sum_party_size).sum
  render json: filter.as_json(reservations, set_options(pagy)).merge({ total_covers: total_sum_party_size })
end

#rejectObject



67
68
69
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 67

def reject
  _action_for('reject')
end

#send_custom_smsObject



90
91
92
93
94
95
96
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 90

def send_custom_sms
  ids = params.require(:ids)
  message = params.require(:message)
  Workers::Reservations::SendCustomSmsWorker.perform_async(ids, message)
  render json: { success: true, message: 'Hungry Hub system will send your custom SMS if never been sent before' },
         status: :ok
end

#showObject



44
45
46
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 44

def show
  render json: Api::Dashboard::V2::ReservationSerializer.new(@reservation, set_options).as_json, status: :ok
end

#updateObject

Raises:

  • (Pundit::NotAuthorizedError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 48

def update
  model_policy = ::ReservationPolicy.new(current_staff, @reservation)
  raise Pundit::NotAuthorizedError unless model_policy.editable?

  params[:reservation][:active] = false if parse_param(:cancel)

  restaurant = restaurants.first

  if restaurant.any_offers?
    update_package_booking
  else
    update_normal_booking
  end
end

#update_normal_bookingObject



112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 112

def update_normal_booking
  @reservation.assign_attributes(format_reservation_params.permit!)
  builder = Agents::UpdateForOwner.new @reservation
  builder.status = reservation_params[:status] if reservation_params.try(:[], :status)
  if builder.update_booking
    render json: Api::Dashboard::V2::ReservationSerializer.new(builder.reservation).as_json, status: :ok
  else
    render_error(builder, :unprocessable_entity)
  end
end

#update_package_bookingObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/api/dashboard/v2/reservations_controller.rb', line 98

def update_package_booking
  operation = PackageBooking::Owners::Update.new(params.require(:id), format_reservation_params.permit!,
                                                 package_params,
                                                 add_on_params)
  if operation.update_booking
    render json: Api::Dashboard::V2::ReservationSerializer.new(Reservation.find(operation.reservation.id), set_options).as_json,
           status: :ok
  else
    render_error(operation, :unprocessable_entity)
  end
rescue InvalidPackageData => e
  render json: { success: false, message: e.message, data: {} }, status: :unprocessable_entity
end