Class: ReservationsController

Inherits:
ApplicationController show all
Includes:
QuickEditReservation
Defined in:
app/controllers/reservations_controller.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Methods included from QuickEditReservation

#validate_guest_edit

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#book_landing_pageObject

landing page for user after do booking table using find-table -> book-table



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/reservations_controller.rb', line 38

def book_landing_page
  @reservation = Reservation.includes(:restaurant).find params[:reservation_id]
  redirect_to(root_path) && return if @reservation.is_past?

  @phone = begin
    phone = @reservation.phone
    phone_length = @reservation.phone.length
    pl = phone_length - 3
    censored_phone = phone[0..pl]
    "#{censored_phone}XXX"
  rescue StandardError => e
    APMErrorHandler.report e
    @reservation.phone
  end
  @quantity = @reservation.party_size
  render layout: 'external'
end

#cancel_reservationObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/reservations_controller.rb', line 153

def cancel_reservation
  id = if Reservation.find_by id: params[:reservation_id]
         params[:reservation_id]
       else
         decode_reservation(:reservation_id)
       end
  service = CancelReservationService.new id, :user

  respond_to do |format|
    if service.execute
      format.html do
        flash[:notice] = t('reservation.marked_as_cancelled')
      end
      format.json do
        render json: {
          success: true,
          path: root_path,
          title: t('reservation.cancel_title.success'),
          message: t('reservation.marked_as_cancelled'),
        }
      end
    else
      format.html do
        flash[:notice] = service.error_message_simple
      end
      format.json do
        service.send :extend, ReservationsHelper
        message = service.error_message_simple.presence || t(
          'reservation.failed_to_cancel', phone: service.hotline(Reservation.find(id))
        )
        render json: {
          success: false,
          path: root_path,
          title: t('reservation.cancel_title.failed'),
          message: message,
        }
      end
    end
  end
end

#check_reservationObject



119
120
121
# File 'app/controllers/reservations_controller.rb', line 119

def check_reservation
  render layout: 'external'
end

#did_attendObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/reservations_controller.rb', line 83

def did_attend
  begin
    reservation = Reservation.find(decode_reservation(:hash))
  rescue StandardError, ActiveRecord::RecordNotFound => e
    HH_LOGGER.info e
    redirect_to(root_path, alert: 'Invalid Booking ID') && return
  end

  return render plain: t('reservation.marked_as_arrived1') if reservation.arrived?

  agent = Agents::UpdateBookingStatusForUser.new(reservation)
  unless agent.fix_incorrect_validation(:no_show)
    APMErrorHandler.report 'Can\'t mark reservation as arrived', errors_msgs: agent.errors, reservation_id: agent.id
    flash[:alert] = agent.errors.full_messages.join(', ')
  end
end

#did_not_attendObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/reservations_controller.rb', line 100

def did_not_attend
  begin
    reservation = Reservation.find(decode_reservation(:hash))
  rescue StandardError, ActiveRecord::RecordNotFound => e
    HH_LOGGER.info e
    redirect_to(root_path) && return
  end

  return render plain: t('reservation.marked_as_no_show') unless reservation.active?

  agent = Agents::UpdateBookingStatusForUser.new(reservation)

  unless agent.fix_incorrect_validation(:cancel)
    APMErrorHandler.report 'Can\'t mark reservation as cancelled', errors_msgs: builder.error_message,
                                                                   reservation_id: reservation.id
    flash[:alert] = builder.error_message
  end
end

#myObject



13
14
15
# File 'app/controllers/reservations_controller.rb', line 13

def my
  @reservations = current_owner.restaurant.reservations
end

#my_reservationObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/reservations_controller.rb', line 123

def my_reservation
  begin
    @reservation = Reservation.find(decode_reservation(:hash))
  rescue ActiveRecord::RecordNotFound => e
    HH_LOGGER.info e
    render('/404', layout: nil, status: :not_found, formats: :html)
    return
  end

  validation = validate_guest_edit(@reservation)

  unless validation[:success]
    redirect_to validation[:next_path], validation[:flash]
    return
  end

  if @reservation.package.present?
    if params[:partner].present?
      redirect_to quick_edit_bookings_path(booking_id: @reservation.to_url_hash, locale: params[:locale],
                                           partner: params[:partner], protocol: 'https://')
    else
      redirect_to quick_edit_bookings_path(booking_id: @reservation.to_url_hash,
                                           locale: MyLocaleManager.parse(@reservation.try(:user).try(:language)), protocol: 'https://')
    end
  else
    redirect_to quick_edit_non_package_bookings_path(booking_id: @reservation.to_url_hash,
                                                     locale: MyLocaleManager.parse(@reservation.try(:user).try(:language)), protocol: 'https://')
  end
end

#owner_cancelObject



287
288
289
290
291
292
293
294
295
296
297
# File 'app/controllers/reservations_controller.rb', line 287

def owner_cancel
  reservation_id = decode_reservation(:reservation_hash)
  service = CancelReservationService.new reservation_id, :owner
  service.cancel_reason = 'non package booking, skip asking cancel reason'
  if service.execute
    flash.now.notice = t('reservation.cancelled_for_owner')
  else
    flash.now.alert = service.error_message_simple
  end
  render layout: false
end

#owner_confirmObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/controllers/reservations_controller.rb', line 270

def owner_confirm
  owner = Owner.find(decode_owner(:owner_hash))
  reservation = Reservation.includes(restaurant: :owner).find decode_reservation(:reservation_hash)
  render_not_found && return if owner.id != reservation.restaurant.owner.id

  reservation_params = params.require(:reservation).permit!
  reservation.attributes = reservation_params
  reservation.save if reservation.changed?
  service = ReservationConfirmationService.new(reservation.id, confirmed_by: :owner)
  if service.accept
    flash.now.notice = t('reservation.updated_for_owner')
  else
    flash.now.alert = service.error_message_simple
  end
  render layout: false
end

#owner_confirmation_pageObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/reservations_controller.rb', line 254

def owner_confirmation_page
  owner_id = decode_owner(:owner_hash)
  owner = Owner.find(owner_id)
  reservation_id = decode_reservation(:reservation_hash)
  new_reservation = Reservation.includes(restaurant: :owner).find_by(old_reservation_id: reservation_id)
  @reservation = new_reservation.presence || Reservation.includes(restaurant: :owner).find(reservation_id)
  render_not_found && return if owner.id != @reservation.restaurant.owner.id

  if @reservation.package.present?
    redirect_to(owner_quick_edit_bookings_path(encode_owner(owner.id),
                                               encode_reservation(@reservation.id))) && return
  end

  render layout: 'external'
end

#owner_updateObject

used in owner email, so owner can update reservation directly



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/reservations_controller.rb', line 195

def owner_update
  reservation_id = decode_reservation(:reservation_id)
  update_type = params.require(:type)
  if update_type == 'cancel'
    service = CancelReservationService.new reservation_id, :owner, { require_reason: true }
    service.cancel_reason = params.fetch(:reason) do
      reservation = Reservation.fetch reservation_id
      url = owner_confirmation_reservation_path(owner_hash: reservation.restaurant.owner.to_url_hash,
                                                reservation_hash: reservation.to_url_hash)
      return redirect_to url
    end

    if service.execute
      render json: {
        message: t('reservation.cancelled_for_owner'), success: true
      }
    else
      render json: {
        message: service.error_message_simple,
        success: false,
      }
    end
  else
    reservation = Reservation.find(reservation_id)
    owner = Owner.find(decode_owner(:owner))

    if !reservation.is_past? && !dashboard_v2_owner_signed_in?
      if owner.email == params[:email] && owner.valid_password?(params[:password])
        (:dashboard_v2_owner, owner)
      else
        render plain: 'Invalid email or password', status: :unprocessable_entity
        return
      end
    end

    render_not_found unless reservation.restaurant_id == owner.restaurant.id

    agent = case update_type
            when 'arrive'
              Agents::OwnerUpdateArrived.new reservation
            when 'no-show'
              reservation.mark_as_no_show!
              # Trigger sync for owner no-show update (immediate for owner operations)
              reservation.trigger_immediate_sync
              Agents::UpdateAsNoShow.new reservation
            else
              APMErrorHandler.report 'Illegal action found', params: params
              return render_not_found
            end

    if agent.update_booking
      render plain: t('reservation.updated_for_owner')
    else
      message = agent.error_message.presence || t('reservation.failed_to_update', phone: agent.hotline)
      render plain: message
    end
  end
end

#rating_landing_pageObject



79
80
81
# File 'app/controllers/reservations_controller.rb', line 79

def rating_landing_page
  redirect_to new_booking_review_path(booking_id: params.require(:hash), rating: params.fetch(:rate, 0))
end

#update_anonymObject

modify booking for reservation without packages



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/reservations_controller.rb', line 18

def update_anonym
  reservation = Reservation.find(params[:reservation_id])
  if reservation.is_past?
    return render(json: { success: false, title: 'Sorry, update failed',
                          message: t('reservation.editing_past_reservation2') })
  end

  @updater = Agents::ModifyBookingForUser.new(reservation, params[:reservation])
  if @updater.execute
    # Trigger reservation summary sync after successful modify booking
    sync_reservations(@updater.reservation)

    render json: { success: true, path: check_reservations_path, title: t('reservation.modified_title.success'),
                   message: t('reservation.modified') }
  else
    render json: { success: false, title: t('reservation.modified_title.failed'), message: @updater.error_message }
  end
end

#validate_before_manageObject

for validate given email and reservation id, if true, user can edit his booked reservation



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/reservations_controller.rb', line 57

def validate_before_manage
  reservation_param = params.require(:reservation)
  id = reservation_param.require(:reservation_id)
  email = reservation_param.require(:email)
  @reservation = Reservation.fetch(id)
  if @reservation.is_past?
    redirect_back(fallback_location: root_path(locale: I18n.locale),
                  notice: t('reservation.editing_past_reservation1')) && return
  end
  unless @reservation.email == email
    redirect_to(check_reservations_path,
                notice: t('reservation.incorrect_credentials')) && return
  end

  url = encode_reservation(@reservation.id)
  redirect_to(manage_my_reservations_path(url)) && return
rescue ActiveRecord::RecordNotFound
  redirect_to(check_reservations_path, notice: t('reservation.not_found')) && return
rescue ActiveModel::RangeError
  redirect_back(fallback_location: root_path(locale: I18n.locale), notice: 'Invalid Reservation ID')
end