Class: ReportService::Dashboard
- Inherits:
-
ApplicationService
- Object
- ApplicationService
- ReportService::Dashboard
- Defined in:
- app/services/report_service/dashboard.rb
Overview
typed: ignore frozen_string_literal: true
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#reservations ⇒ Object
readonly
Returns the value of attribute reservations.
-
#restaurants ⇒ Object
readonly
Returns the value of attribute restaurants.
Attributes inherited from ApplicationService
Instance Method Summary collapse
- #build_reports_by_day ⇒ Object
- #build_reports_by_hour ⇒ Object
- #calculate_seat_available(date, start_time) ⇒ Object
-
#initialize(restaurants, params) ⇒ Dashboard
constructor
A new instance of Dashboard.
- #to_json(*_args) ⇒ Object
Methods inherited from ApplicationService
Constructor Details
#initialize(restaurants, params) ⇒ Dashboard
Returns a new instance of Dashboard.
7 8 9 10 11 |
# File 'app/services/report_service/dashboard.rb', line 7 def initialize(restaurants, params) @restaurants = restaurants @reservations = Reservation.joins(:restaurant).where(restaurant_id: restaurants.ids) @params = params end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'app/services/report_service/dashboard.rb', line 5 def params @params end |
#reservations ⇒ Object (readonly)
Returns the value of attribute reservations.
5 6 7 |
# File 'app/services/report_service/dashboard.rb', line 5 def reservations @reservations end |
#restaurants ⇒ Object (readonly)
Returns the value of attribute restaurants.
5 6 7 |
# File 'app/services/report_service/dashboard.rb', line 5 def restaurants @restaurants end |
Instance Method Details
#build_reports_by_day ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/services/report_service/dashboard.rb', line 49 def build_reports_by_day reservations.where('date between ? and ?', report_params[:start_date], report_params[:end_date]).order('date ASC').map do |r| { date: r.date, reservation_time: r.reservation_time.iso8601, arrived: r.reached_goal? && r.is_past?, pending: r.pending? || r.upcoming?, rejected: r.rejected?, no_show: r.no_show?, cancel: !r.active, adult: r.adult, kids: r.kids, } end end |
#build_reports_by_hour ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/services/report_service/dashboard.rb', line 17 def build_reports_by_hour date = report_params[:start_date].is_a?(Date) ? report_params[:start_date] : report_params[:start_date].to_date @reservations = reservations.exclude_temporary.where(date: date, active: true, no_show: false).group_by(&:start_time_format) hours = reservations.keys.sort hours.map do |hour| data = { count: reservations[hour].size, adult: reservations[hour].sum(&:adult), kids: reservations[hour].sum(&:kids), seat_available: calculate_seat_available(date, hour), } [ hour, data, ] end end |
#calculate_seat_available(date, start_time) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/report_service/dashboard.rb', line 37 def calculate_seat_available(date, start_time) seat_left = 0 restaurants.each do |restaurant| inv_checker = InvCheckerFactory.new(restaurant.id, restaurant.time_zone).create_inv_checker_service today = Time.now_in_tz(restaurant.time_zone).to_date return '-' if date < today seat_left += inv_checker.seat_left(date, start_time) end seat_left end |
#to_json(*_args) ⇒ Object
13 14 15 |
# File 'app/services/report_service/dashboard.rb', line 13 def to_json(*_args) report_params[:is_by_hour] == 'true' ? build_reports_by_hour : build_reports_by_day end |