Class: V2Users::ReviewsController

Inherits:
BaseController show all
Defined in:
app/controllers/v2_users/reviews_controller.rb

Overview

Responsible to display and store reservation's review

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#activate_cache?, #current_user_ck, default_url_options, #identity_cache_memoization, #set_csrf_token

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from UpdateLocaleConcern

#setup_locale

Methods included from ResponseCacheConcern

#my_response_cache

Methods included from AdminHelper

#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?

Methods inherited from ApplicationV2Controller

#identity_cache_memoization

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



8
9
10
# File 'app/controllers/v2_users/reviews_controller.rb', line 8

def reservation
  @reservation
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



8
9
10
# File 'app/controllers/v2_users/reviews_controller.rb', line 8

def restaurant
  @restaurant
end

#reviewObject

Returns the value of attribute review.



9
10
11
# File 'app/controllers/v2_users/reviews_controller.rb', line 9

def review
  @review
end

Instance Method Details

#createObject

POST (/:locale)/bookings/:booking_id/reviews(.:format)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/v2_users/reviews_controller.rb', line 80

def create
  BUSINESS_LOGGER.set_business_context(
    reservation_id: reservation.id,
    restaurant_id: reservation.restaurant_id,
    user_id: reservation.user_id,
  )
  BUSINESS_LOGGER.set_request_context(
    request_id: request.request_id,
    user_agent: request.user_agent,
    ip_address: request.remote_ip,
  )
  BUSINESS_LOGGER.info('Review creation request started', {
                         rating_param: params[:rating],
                         review_text_param: params[:review],
                         disclose_review_param: params[:disclose_review],
                         reservation_status: reservation.status,
                         reservation_dining_date: reservation.date,
                         existing_review_present: reservation.review.present?,
                       })

  unless permitted_to_review?
    BUSINESS_LOGGER.warn('Review creation denied - not permitted', {
                           reservation_can_be_rated: (reservation.review || reservation.build_review).can_be_rated?,
                           reservation_status: reservation.status,
                         })
    return redirect_to root_path, alert: not_allowed_to_give_review_message
  end

  review = reservation.review
  review.attributes = {
    rating: params.require(:rating),
    review: params.fetch(:review, nil),
    restaurant_id: reservation.restaurant_id,
    branch_id: reservation.restaurant&.branch_id,
  }

  unless review.new_record?
    BUSINESS_LOGGER.info('Review already exists, skipping creation', {
                           review_id: review.id,
                           review_persisted: review.persisted?,
                           review_complete: decorate(review).complete?,
                         })
    next_step_path = edit_booking_review_path(id: review.encrypted_id, booking_id: reservation.encrypted_id)
    render json: { success: true, message: nil, next_step: next_step_path }
    return
  end

  BUSINESS_LOGGER.info('Attempting to complete first step', {
                         review_new_record: review.new_record?,
                         review_rating: review.rating,
                         review_text: review.review.present? ? 'present' : 'empty',
                         disclose_review_required_param: params[:disclose_review],
                       })

  operation = complete_first_step(review, params.require(:disclose_review))
  next_step_path = if operation.success?
                     edit_booking_review_path(id: review.encrypted_id,
                                              booking_id: reservation.encrypted_id)
                   end
  message = operation.errors&.full_messages&.to_sentence

  if operation.success?
    BUSINESS_LOGGER.info('Review creation successful', {
                           review_id: review.id,
                           rating: review.rating,
                           review_persisted: review.persisted?,
                           disclose_review: params[:disclose_review],
                           next_step_path: next_step_path,
                         })
  else
    BUSINESS_LOGGER.error('Review creation failed', {
                            rating: review.rating,
                            disclose_review: params[:disclose_review],
                            errors: message,
                            operation_errors: operation.errors&.messages,
                            review_errors: review.errors.messages,
                            review_valid: review.valid?,
                            review_persisted: review.persisted?,
                          })
  end

  render json: { success: operation.success?, message: message, next_step: next_step_path }
end

#create_accountObject



274
275
276
277
278
279
280
281
282
283
284
# File 'app/controllers/v2_users/reviews_controller.rb', line 274

def 
  email = params.fetch(:email, nil)
  password = params.fetch(:password, nil)
  service = ReviewService::SecondHalfStep.new(review.id, email, password)
  result = service.execute
  if result.success?
    render json: { success: true, message: nil, next_step: service.next_step_path }
  else
    render json: { success: false, message: result.message }
  end
end

#editObject

GET (/:locale)/bookings/:booking_id/reviews/:id/edit(.:format) Second step and last step



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'app/controllers/v2_users/reviews_controller.rb', line 239

def edit
  # Redirect when reservation belongs to a registered user
  if reservation.user.present? && Flipper.enabled?(:enable_new_review_url_redirect)
    service = RatingUrlService.new(reservation)
    redirect_to service.member_url, status: :see_other
    return
  end

  r = decorate(review)
  if r.complete?
    redirect_to root_path, alert: 'Feedback for this reservation has already been completed'
    return
  end
  render html: cell(ReviewCpt::Cell::SecondStep, review, reservation: reservation), layout: true
end

#indexObject

GET (/:locale)/restaurants/:restaurant_id/reviews(.:format)

Moved permanently to :web_v2_url/:locale/restaurants/:restaurant_slug/reviews/all



20
21
22
23
24
25
26
# File 'app/controllers/v2_users/reviews_controller.rb', line 20

def index
  web_v2_url = AdminSetting.web_v2_host
  restaurant_slug = restaurant.slug

  redirect_to "#{web_v2_url}/#{I18n.locale}/restaurants/#{restaurant_slug}/reviews/all",
              status: :moved_permanently
end

#landing_pageObject



255
256
257
258
259
260
261
262
263
# File 'app/controllers/v2_users/reviews_controller.rb', line 255

def landing_page
  EarlyBird::RewardOperation.delay.call('review_id' => review.id)
  render html: cell(
    ReviewCpt::Cell::LandingPage,
    review,
    reservation: reservation,
    customer: params.fetch(:customer, 'guest'),
  ), layout: true
end

#newObject

First step GET (/:locale)/bookings/:booking_id/reviews/new(.:format)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/v2_users/reviews_controller.rb', line 30

def new
  unless permitted_to_review?
    return redirect_to root_path, alert: not_allowed_to_give_review_message
  end

  rating = params.fetch(:rating, 0).to_i

  # Redirect when reservation belongs to a registered user
  if reservation.user.present? && Flipper.enabled?(:enable_new_review_url_redirect)
    service = RatingUrlService.new(reservation)
    rate_param = rating.positive? ? rating : nil
    redirect_to service.member_url(rate: rate_param), status: :see_other
    return
  end

  # Meta tags are only set for guest reservations (members redirected above)
  set_meta_tags description: 'Review your reservation', title: 'Review your reservation'
  self.review = reservation.review

  if review.new_record?
    if rating.positive? && rating.between?(3, 5)
      review.attributes = {
        restaurant_id: reservation.restaurant_id,
        branch_id: reservation.restaurant&.branch_id,
        rating: rating,
      }

      operation = complete_first_step(review, params.fetch(:disclose_review, nil))
      if operation.success?
        redirect_to edit_booking_review_path(
          id: reservation.review.encrypted_id,
          booking_id: reservation.encrypted_id,
          locale: MyLocaleManager.normalize_locale,
        )
      else
        flash.now[:alert] = operation.errors&.full_messages&.to_sentence
        render html: cell(ReviewCpt::Cell::FirstStep, review, reservation: reservation), layout: true
      end
    else
      render html: cell(ReviewCpt::Cell::FirstStep, review, reservation: reservation), layout: true
    end
  elsif review.persisted? && decorate(review).complete?
    redirect_to root_path, alert: 'Feedback for this reservation has already been completed'
  else
    redirect_to edit_booking_review_path(id: review.encrypted_id, booking_id: reservation.encrypted_id,
                                         locale: MyLocaleManager.normalize_locale)
  end
end

#new_accountObject



265
266
267
268
269
270
271
272
# File 'app/controllers/v2_users/reviews_controller.rb', line 265

def 
  if reservation.user_id.present?
    return redirect_to booking_review_landing_page_path(booking_id: reservation.encrypted_id,
                                                        review_id: review.encrypted_id,
                                                        locale: MyLocaleManager.normalize_locale)
  end
  render html: cell(ReviewCpt::Cell::NewAccount, review, reservation: reservation), layout: true
end

#permitted_paramsObject



230
231
232
233
234
235
# File 'app/controllers/v2_users/reviews_controller.rb', line 230

def permitted_params
  recommend_for_params = ::Review::AVAILABLE_RECOMMENDATIONS + ::Review::RECOMMENDATION_TEXT_FIELDS
  params.permit(:overall, :food, :ambience, :service, :value, :delivery_service, :packaging,
                :overall_cleanliness, :staff_protection, :social_distancing,
                :occasion, :review, recommend_for: recommend_for_params)
end

#showObject



286
287
288
289
290
291
292
# File 'app/controllers/v2_users/reviews_controller.rb', line 286

def show
  web_v2_url = AdminSetting.web_v2_host
  restaurant_slug = review.restaurant&.slug

  redirect_to "#{web_v2_url}/#{I18n.locale}/restaurants/#{restaurant_slug}/reviews/#{review.encrypted_id}",
              status: :moved_permanently
end

#updateObject

PUT/PATCH (/:locale)/bookings/:booking_id/reviews/:id(.:format) guest user allowed to register



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
193
194
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
# File 'app/controllers/v2_users/reviews_controller.rb', line 166

def update
  BUSINESS_LOGGER.set_business_context(
    reservation_id: reservation.id,
    restaurant_id: reservation.restaurant_id,
    review_id: review.id,
    user_id: reservation.user_id,
  )
  BUSINESS_LOGGER.set_request_context(
    request_id: request.request_id,
    user_agent: request.user_agent,
    ip_address: request.remote_ip,
  )
  BUSINESS_LOGGER.info('Review update request started', {
                         review_current_rating: review.rating,
                         review_persisted: review.persisted?,
                         review_complete: decorate(review).complete?,
                         params_overall: params[:overall],
                         params_food: params[:food],
                         params_ambience: params[:ambience],
                         params_service: params[:service],
                         params_register: params[:register],
                         reservation_status: reservation.status,
                       })

  unless permitted_to_review?
    BUSINESS_LOGGER.warn('Review update denied - not permitted', {
                           reservation_can_be_rated: (reservation.review || reservation.build_review).can_be_rated?,
                           reservation_status: reservation.status,
                         })
    return redirect_to root_path, alert: not_allowed_to_give_review_message
  end

  service = ReviewService::SecondStep.new(review.id, permitted_params.merge(params.permit(:register)))
  result = service.execute

  if result.success?
    BUSINESS_LOGGER.info('Review update successful', {
                           overall: permitted_params[:overall],
                           food: permitted_params[:food],
                           ambience: permitted_params[:ambience],
                           service: permitted_params[:service],
                           value: permitted_params[:value],
                           register: params[:register],
                           next_step_path: service.next_step_path,
                           review_complete: decorate(review.reload).complete?,
                         })
    render json: { success: true, next_step: service.next_step_path }
  else
    BUSINESS_LOGGER.error('Review update failed', {
                            overall: permitted_params[:overall],
                            food: permitted_params[:food],
                            ambience: permitted_params[:ambience],
                            service: permitted_params[:service],
                            value: permitted_params[:value],
                            register: params[:register],
                            error_message: service.error_message_simple,
                            result_message: result.message,
                            review_errors: review.reload.errors.messages,
                            review_valid: review.valid?,
                          })
    render json: { success: false, message: service.error_message_simple }
  end
end