Class: Api::Partner::V1::RestaurantsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/partner/v1/restaurants_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#default_restaurant, #identity_cache_memoization, #render_unauthorize_action, #restaurants, #set_options

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Methods included from ResponseCacheConcern

#my_response_cache

Instance Attribute Details

#restaurantObject (readonly)

Returns the value of attribute restaurant.



6
7
8
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 6

def restaurant
  @restaurant
end

Instance Method Details

#calculate_package_priceObject



151
152
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 151

def calculate_package_price
  calculator = HhPackage::ReservationPackages::ChargeCalculator.new
  message = ''
  success = false
  data = {}

  user = User.find_by(id: params[:user_id])
  restaurant = Restaurant.find(1198)

  # Use the PackageServices::Processor service for package processing
  params_package_bought = params.permit(package_bought: [:id, :quantity, {
                                          menu_sections: [:id, { menus: %i[id quantity] }],
                                          selected_special_menus: [:id, :quantity],
                                          group_sections: [:id, :quantity],
                                        }])

  processor = PackageServices::Processor.new(params_package_bought, restaurant, params[:adult])
  package_bought, is_valid_mix_n_match = processor.process_packages

  # Process add-ons
  params_add_on_bought = params.permit(add_on_bought: [:id, :quantity])
  add_on_processor = AddOnServices::Processor.new(params_add_on_bought, restaurant, params[:adult])
  add_on_bought = add_on_processor.process_add_ons

  unless is_valid_mix_n_match
    return render json: { success: false, message: 'Invalid mix of packages' }
  end

  calculator.set_delivery_pricing_tiers(restaurant.delivery_pricing_tiers) if restaurant
  calculator.set_user(user&.id)

  begin
    support_dynamic_pricing = true
    date = params[:reservation_date]
    if date.blank?
      HH_LOGGER.debug 'No reservation date provided', params: params
      date = Time.now_in_tz(restaurant.time_zone).to_date.tomorrow
      support_dynamic_pricing = false
    else
      date = date.to_date unless date.is_a?(Date)
    end

    calculator.use_dynamic_pricing = support_dynamic_pricing
    data = calculator.calculate(package_bought, params.require(:adult), params.require(:kids),
                                params.fetch(:distance, 0.0), restaurant, date, add_on_bought)
    if data.present?
      data = data.except(
        :charge_price_cents, :total_price_cents, :total_package_price_cents, :add_on_total_price_cents
      )
    end

    success = true
  rescue StandardError => e
    raise e if Rails.env.development?

    APMErrorHandler.report(e)
    Rails.logger.error e
    message = e.message.presence || 'Something went wrong'
  end

  render json: { success: success, message: message, data: data.merge(delivery_fee: data[:delivery_fee].to_s) }
rescue ActiveRecord::RecordNotFound
  raise RecordNotFoundButOk
rescue InvalidPackageData => e
  render json: { success: false, message: e.message, data: {} }
end

#createObject



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 10

def create
  result = PartnerService::Restaurant::Create.new(permitted_params).execute

  if result.success?
    Partner::StaffMailer.staff_getstarted(result.data[:staff][:id], I18n.locale.to_s).deliver_later!
    render json: { message: I18n.t('partner.restaurants.register_success'), success: true }
  else
    render json: { success: false, message: result.message }
  end
end

#create_documentsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 105

def create_documents
  restaurant = default_restaurant
  documents_params.each do |_k, restaurant_document|
    ActiveRecord::Base.transaction do
      if restaurant_document[:id].present? && restaurant_document[:is_destroy] == 'true'
        RestaurantDocument.find(restaurant_document[:id].to_i).delete
      elsif restaurant_document[:id].present? && restaurant_document[:is_destroy] == 'false'
        RestaurantDocument.find(restaurant_document[:id].to_i).update(name: restaurant_document[:name],
                                                                      file: restaurant_document[:file],
                                                                      restaurant_id: restaurant.id)
      else
        RestaurantDocument.create!(name: restaurant_document[:name],
                                   file: restaurant_document[:file],
                                   restaurant_id: restaurant.id)
      end
      restaurant.update!(type_of_enterprise: params.require(:type_of_enterprise))
    end
  end
  render json: { success: true, message: 'Successfully' }
rescue StandardError => e
  render json: { success: false, message: e.message }
end


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 74

def delete_gallery
  picture_ids = params[:picture_ids].is_a?(Array) ? params[:picture_ids] : [params[:picture_ids]]
  return error('Picture ID is required', :not_found) if picture_ids.blank?

  pictures = default_restaurant.pictures.where(id: picture_ids)
  return error('Picture not found', :not_found) if pictures.blank?

  change_tracker.track_deleted_picture(pictures)
  if pictures.destroy_all
    change_tracker.notify_managers(default_restaurant)
    success(I18n.t('partner.restaurants.delete_photo_success'), :ok)
  else
    error(I18n.t('partner.restaurants.delete_photo_fail'), :bad_request)
  end
end

#restaurant_tagsObject



90
91
92
93
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 90

def restaurant_tags
  restaurant_tags = RestaurantTag.where_category(params[:category].to_s)
  render json: ::Api::Partner::RestaurantTagSerializer.new(restaurant_tags).as_json
end

#reverse_geocodeObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 128

def reverse_geocode
  unless params[:long].present? && params[:lat].present?
    render(json: { success: false, message: 'Longitude and latitude are required.' }) && return
  end

  longitude = params[:long].to_f
  latitude = params[:lat].to_f

  unless longitude.between?(-180, 180) && latitude.between?(-90, 90)
    render(json: { success: false, message: 'Invalid longitude or latitude.' }) && return
  end

  mapbox_service = MapboxService.new

  begin
    response_json = mapbox_service.reverse_geocode(longitude, latitude)
  rescue StandardError => e
    render(json: { success: false, message: e.message }) && (return)
  end

  render json: response_json
end


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 52

def sort_gallery
  picture_ids = params.require(:picture_ids)
  pictures = default_restaurant.pictures.where(id: picture_ids)

  if pictures.size != picture_ids.size
    return error('Invalid picture ids', :bad_request)
  end

  picture_data = []
  ActiveRecord::Base.transaction do
    picture_ids.each_with_index do |id, index|
      picture = pictures.find(id)
      picture.priority = index + 1
      picture_data << picture
    end

    Restaurants::Picture.import!(picture_data, on_duplicate_key_update: [:priority], raise_error: true)
  end

  success(I18n.t('partner.restaurants.sort_photo_success'), :ok)
end


40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 40

def update_gallery
  picture = default_restaurant.pictures.new(picture_params)
  if picture.save
    change_tracker.track_added_picture(picture)
    change_tracker.notify_managers(default_restaurant)

    success(I18n.t('partner.restaurants.upload_photo_sucess'), :ok)
  else
    error(I18n.t('partner.restaurants.upload_photo_fail'), :bad_request)
  end
end

#update_steps_verifyObject



95
96
97
98
99
100
101
102
103
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 95

def update_steps_verify
  steps_verify = params.require(:steps_verify).permit(:vouchers, :packages)
  restaurant = default_restaurant
  restaurant.vouchers = steps_verify[:vouchers]
  restaurant.packages = steps_verify[:packages]
  return success(I18n.t('partner.restaurants.update_success'), :ok) if restaurant.save

  error(I18n.t('partner.restaurants.update_fail'), :bad_request)
end

#update_storeObject



27
28
29
30
31
32
33
34
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 27

def update_store
  result = PartnerService::Restaurant::Update.new(current_staff, default_restaurant, params).update_restaurant
  if result.success?
    render json: { message: I18n.t('partner.restaurants.update_success'), success: true }
  else
    render json: { success: false, message: result.message }
  end
end


36
37
38
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 36

def view_gallery
  render json: ::Api::Partner::PictureSerializer.new(default_restaurant.pictures.order_by_priority).as_json
end

#view_storeObject



21
22
23
24
25
# File 'app/controllers/api/partner/v1/restaurants_controller.rb', line 21

def view_store
  render json: ::Api::Partner::RestaurantSerializer.new(default_restaurant, {
                                                          params: { current_staff: current_staff },
                                                        }).as_json
end