Class: Dashboard::V2::InventoriesController

Inherits:
MainController show all
Includes:
CountryEmailHelper
Defined in:
app/controllers/dashboard/v2/inventories_controller.rb

Overview

typed: ignore frozen_string_literal: true

Direct Known Subclasses

Group::InventoriesController

Instance Method Summary collapse

Methods included from CountryEmailHelper

#merchant_email_for_country

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

#activate_order_nowObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 207

def activate_order_now
  restaurant = get_restaurant
  success = false
  order_now = params.require(:order_now).to_s == 'true'
  if restaurant.present? && restaurant.has_delivery_inventories? &&
      restaurant.update(support_order_now: order_now)
    success = true

    message = if order_now
                I18n.t('views.d.order_now_activated')
              else
                I18n.t('views.d.order_now_disabled')
              end
  else
    message = 'Please contact Hungry hub team to activate Order now feature'
  end
  render json: { success: success, message: message }
end

#block(restaurant_id = nil) ⇒ Object



25
26
27
28
29
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 25

def block(restaurant_id = nil)
  restaurant = if restaurant_id.nil?
                 get_restaurant
               else
                 Restaurant.find(restaurant_id)
               end
  if restaurant.use_third_party_inventory?
    render json: { message: "#{restaurant.name}:" + I18n.t('inventory.tc_inventory.tc_dashboard') },
           status: :unprocessable_entity
    return
  end

  start_time = Time.use_zone(restaurant.time_zone) do
    Time.zone.parse "#{valid_params[:start_date]} #{valid_params[:start_time]}"
  end
  end_time = Time.use_zone(restaurant.time_zone) do
    Time.zone.parse "#{valid_params[:end_date]} #{valid_params[:end_time]}"
  end
  inventory_type = params.require(:inventory).require(:inventory_type)
  start_time_blank = valid_params[:start_time].blank? || valid_params[:start_time] == 'Choose time'
  end_time_blank = valid_params[:end_time].blank? || valid_params[:end_time] == 'Choose time'

  if start_time_blank || end_time_blank
    render json: { message: "Time can't be blank" }, status: :unprocessable_entity
    return
  elsif start_time >= end_time
    render json: { message: 'Please provide valid time' }, status: :unprocessable_entity
    return
  end
  if valid_params[:reason].blank?
    render json: { message: 'Please provide the reason' }, status: :unprocessable_entity
    return
  end

  # if restaurant.flag? then start date only valid if today or tommorow
  if restaurant.flag? && [Date.restaurant_today(restaurant),
                          (Date.restaurant_today(restaurant) + 1.day)].exclude?(valid_params[:start_date].to_date)
    render json: { message: 'Invalid date' }, status: :unprocessable_entity
    return
  end

  start_date = valid_params[:start_date].to_date
  end_date = valid_params[:end_date].to_date

  restaurant_id = current_owner.restaurant.id if restaurant_id.nil?

  operation = InventoryUpdater.call({
                                      restaurant_id: restaurant_id,
                                      start_date: start_date,
                                      end_date: end_date,
                                      start_time: valid_params[:start_time],
                                      end_time: valid_params[:end_time],
                                      quantity_available: 0,
                                      reason: valid_params[:reason],
                                    }, settings: {
                                      notify_staff: true,
                                    }, inventory_type: inventory_type)

  if operation.success?
    if restaurant.flag?
      blocked_by = valid_params[:staff_id].presence ? Staff.find(valid_params[:staff_id]).name : current_owner.name
      StaffMailer.notify_block_restaurant(restaurant_id, start_date.to_s, valid_params[:start_time],
                                          valid_params[:end_time], blocked_by).deliver_later(wait: 15.minutes)
    end
    @operation = operation
    render 'success_block'
  else
    render json: { message: operation['result.fail_message'] }, status: :unprocessable_entity
  end
rescue ArgumentError, NoMethodError => e
  APMErrorHandler.report e
  render json: { message: 'Invalid date or time' }, status: :unprocessable_entity
end

#block_formObject



9
10
11
12
13
14
15
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 9

def block_form
  if params[:partner_app_embed]
    render layout: 'partner_app'
  else
    render layout: 'dashboard_v2_pop_up'
  end
end

#create_or_updateObject

Currently it's only for updating



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
193
194
195
196
197
198
199
200
201
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 155

def create_or_update
  if valid_params[:quantity_available].to_i < 1
    render json: { success: false, message: 'You cannot edit alotment to "0" please use block inventory button' }
    return
  end

  restaurant = get_restaurant
  restaurant_id = restaurant.id
  inventory_type = params.require(:inventory).require(:inventory_type)
  quantity_available = params.require(:inventory).require(:quantity_available)

  if restaurant.use_third_party_inventory?
    render json: { success: false, message: "#{restaurant.name}:" + I18n.t('inventory.tc_inventory.tc_dashboard') }
    return
  end

  if restaurant.flag?
    if valid_params[:quantity_available].to_i < restaurant.minimum_seat_allotment
      render json: { success: false,
                     message: "You cannot edit alotment less than #{restaurant.minimum_seat_allotment}" }
      return
    end

    if valid_params[:start_date].to_date != valid_params[:end_date].to_date
      render json: { success: false, message: 'You cannot edit alotment for multiple days' }
      return
    end
  end
  start_date = valid_params[:start_date].to_date
  end_date = valid_params[:end_date].to_date

  operation = InventoryUpdater.call({
                                      restaurant_id: restaurant_id,
                                      start_date: start_date,
                                      end_date: end_date,
                                      start_time: valid_params[:start_time],
                                      end_time: valid_params[:end_time],
                                      quantity_available: quantity_available,
                                    }, inventory_type: inventory_type)

  if operation.success?
    send_notification_to_error_staff(params.require(:inventory))
    render json: { success: true, message: operation['result.success_message'] }
  else
    render json: { success: false, message: operation['result.fail_message'] }
  end
end

#editObject



203
204
205
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 203

def edit
  render layout: 'dashboard_v2_pop_up'
end

#flag_data(restaurant = get_restaurant) ⇒ Object



17
18
19
20
21
22
23
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 17

def flag_data(restaurant = get_restaurant)
  render json: {
    is_flagged: restaurant.flag?,
    available_seat_after_blocking: restaurant.minimum_seat_allotment,
    active: restaurant.active && !restaurant.date_offer_expired?(Date.restaurant_today(restaurant)),
  }
end

#indexObject



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
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 99

def index
  @service_type = params.fetch(:service_type, 'dine_in')
  respond_to do |format|
    format.json do
      @restaurant = get_restaurant
      @date = params.require(:date).to_date
      @inv_checker = InvCheckerFactory.new(@restaurant.id, @restaurant.time_zone).create_inv_checker_service
      if @restaurant.has_delivery_inventories?
        if @service_type == 'dine_in'
          @inv_checker.for_dine_in = true
          @inv_checker.for_delivery = false
        else
          @inv_checker.for_dine_in = false
          @inv_checker.for_delivery = true
        end
      end
    end
    format.html do
      if current_owner.many_restaurants?
        @reservation_durations = current_owner.restaurants.map { |r| { id: r.to_url_hash, duration: r.res_duration } }
      else
        @reservation_duration = get_restaurant.res_duration
      end
    end
  end
end

#override_reportObject



126
127
128
129
130
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 126

def override_report
  @grid = Dashboard::V2::OverrideInventoriesGrid.new(default_params.merge(search_params)) do |scope|
    scope.where(restaurant_id: current_owner.restaurant.id).page(params[:page])
  end
end

#undoObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/dashboard/v2/inventories_controller.rb', line 132

def undo
  inventory_type = params.require(:service_type)
  UpdatedInventory.transaction do
    upd_inventory = UpdatedInventory.find(params[:inventory_id])
    restaurant = upd_inventory.restaurant
    if restaurant.use_third_party_inventory?
      flash[:alert] = "#{restaurant.name}:" + I18n.t('inventory.tc_inventory.tc_dashboard')
    else
      HH_LOGGER.info('inventory:undo_changes', upd_inventory.attributes)
      upd_inventory.destroy!
      restaurant.touch
      Restaurant.generate_schedule restaurant.id, { inventory_type: inventory_type }
      flash[:notice] = 'System is recreating the inventories right now, please wait for a minute'
    end
  rescue StandardError => e
    APMErrorHandler.report 'Unable to restore inventory', error: e
    flash[:alert] = 'Something went wrong'
    raise ActiveRecord::Rollback, e
  end
  redirect_back fallback_location: back_fallback_location
end