Class: Dashboard::V2::OwnerReviewsController

Inherits:
MainController show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/controllers/dashboard/v2/owner_reviews_controller.rb

Overview

typed: ignore frozen_string_literal: true

Direct Known Subclasses

Group::OwnerReviewsController

Instance Method Summary collapse

Methods inherited from MainController

#default_fallback_location, #default_url_options, #kiosque_callback

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from Stimulus::TagController

#stimulus

Methods included from OwnerLoginMode

#current_owner, #group_owner_type?, #request_path_contain_group?, #single_owner_type?

Methods included from OwnerDashboardsHelper

#events_ajax_previous_link, #show_source

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 ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/dashboard/v2/owner_reviews_controller.rb', line 6

def create
  permitted_params = params.require(:review).permit(:rating, :comment, :reservation_id)
  reservation = Reservation.fetch(permitted_params.require(:reservation_id))

  authorize reservation

  review = OwnerReview.create_by_owner(reservation_id: reservation.id,
                                       rating: permitted_params[:rating],
                                       comment: permitted_params[:comment])
  if review.persisted?
    review = {
      id: review.id,
      user_id: review.user_id,
      rating: review.rating,
      comment: review.comment,
      user_avg_rating: number_with_precision(OwnerReview.where(user: review.user).average(:rating), precision: 2,  strip_insignificant_zeros: true)
    }
    render json: { success: true, review: review }
  else
    render json: { success: false, error_message: review.errors.full_messages.to_sentence }
  end
end

#historyObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/dashboard/v2/owner_reviews_controller.rb', line 45

def history
  user_type = params.require(:user_type)
  user = if user_type == 'user'
            User.fetch(params.require(:user_id))
          else
            Guest.find_by(id: params.require(:user_id))
          end
  reviews = user.owner_reviews
  avg_score = reviews.select('AVG(rating) as avg')[0]['avg'].to_i rescue 0
  render json: reviews,
         each_serializer: ::Dashboard::V2::OwnerReviewSerializer,
         adapter: :json_api,
         meta: {
           avg_score: avg_score,
           total_arrived: reviews.joins(:reservation).where(reservations: { active: true, no_show: false }).count,
           total_no_show: reviews.joins(:reservation).where(reservations: { no_show: true }).count,
           total_reviews: reviews.count
         }
end

#indexObject



29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/dashboard/v2/owner_reviews_controller.rb', line 29

def index
  ids = params.fetch(:ids, []).select { |id| id.present? }
  return render json: { success: true, data: [] } if ids.blank?

  cache_key = CityHash.hash64([Date.today, ids, Time.now.hour])
  return unless stale?(cache_key, template: false)

  reviews = ::OwnerReview.get_by_reservation_ids(ids)
  render json: { success: true, data: reviews }
end

#showObject



40
41
42
43
# File 'app/controllers/dashboard/v2/owner_reviews_controller.rb', line 40

def show
  review = OwnerReview.find(params.require(:id))
  render json: review
end