Class: ReservationService::Form::HungryHub
- Includes:
- Api::Vendor::V1::Concerns::ReservationUtils, ElasticAPM::SpanHelpers, Validators
- Defined in:
- app/services/reservation_service/form/hungry_hub.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#skip_prepayment ⇒ Object
readonly
Returns the value of attribute skip_prepayment.
-
#tmp_reservation ⇒ Object
Returns the value of attribute tmp_reservation.
Attributes inherited from Base
Instance Method Summary collapse
-
#cc_payment_provider_param ⇒ Object
this method for checking cc provider based on request payload.
-
#initialize(params, tmp_reservation = nil) ⇒ HungryHub
constructor
A new instance of HungryHub.
-
#prepaid_token ⇒ Object
if CC prepayment, then this parameter is required.
- #to_reservation ⇒ Object
- #validate ⇒ Object
-
#vendor_payment ⇒ Object
bypass the pre-payment if vendor payment present.
Methods included from Validators
#accept_refund_guarantee_param, #alipay?, #check_skip_prepayment?, #created_by_admin?, #eligible_to_use_refund_guarantee?, #shopee_pay?, #true_wallet?, #wechat_pay?
Methods included from EncryptableHelper
#decrypt, #encrypt, #generate_signature
Methods included from DuplicateBookingChecker
#duplicate_error_message, #multiple_bookings_allowed?
Methods included from DefaultErrorContainer
#error, #error_message_simple, #merge_errors
Constructor Details
#initialize(params, tmp_reservation = nil) ⇒ HungryHub
Returns a new instance of HungryHub.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 11 def initialize(params, tmp_reservation = nil) self.params = params @vendor_service = ReservationService::Vendor.new(params[:vendor_name], params[:web_v2_host]) setup_tmp_reservation(tmp_reservation) if tmp_reservation @voucher_form = setup_voucher_form @skip_prepayment = check_skip_prepayment? if tmp_reservation.present? BUSINESS_LOGGER.set_business_context({ tmp_reservation_id: tmp_reservation.id }) end BUSINESS_LOGGER.info(I18n.t('business_logger.reservation_service.form.hungry_hub.initialize'), reservation_id: tmp_reservation&.id, params: params.merge(payment_provider: payment_provider, payment_type: payment_type)) end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
8 9 10 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 8 def params @params end |
#skip_prepayment ⇒ Object (readonly)
Returns the value of attribute skip_prepayment.
8 9 10 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 8 def skip_prepayment @skip_prepayment end |
#tmp_reservation ⇒ Object
Returns the value of attribute tmp_reservation.
9 10 11 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 9 def tmp_reservation @tmp_reservation end |
Instance Method Details
#cc_payment_provider_param ⇒ Object
this method for checking cc provider based on request payload
535 536 537 538 539 540 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 535 def cc_payment_provider_param return :gb_primepay if cc_gb_primepay_provider? return :omise if cc_omise_provider? restaurant&.selected_cc_payment_provider&.to_sym end |
#prepaid_token ⇒ Object
if CC prepayment, then this parameter is required
530 531 532 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 530 def prepaid_token gb_primepay_card_param.presence || omise_token_param end |
#to_reservation ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 426 def to_reservation attrs = { channel: channel_params, medium: medium_params, group_booking: group_booking_params, created_by: created_by_params, business_booking: business_booking_params, guests_attributes: guests_attributes, reservation_we_travel_together_attributes: reservation_we_travel_together_attributes, corporate_event_id: corporate_event_id_param, driving_duration: get_driving_duration, driving_duration_checked: true, for_locking_system: false, reservation_special_menus_attributes: , aoa_reservation_attributes: aoa_reservation_attributes, }.merge(reservation_params).merge(user_params) if created_by_admin? && vendor_service.partner_vendor? BUSINESS_LOGGER.info( I18n.t('business_logger.reservation_service.form.hungry_hub.to_reservation.partner_vendor_by_admin'), params: params.merge(payment_provider: payment_provider, payment_type: payment_type), ) attrs[:vendor_reservation_attributes] = vendor_service.vendor_reservations_attributes_from_admin(params[:reservation]) elsif vendor_service.partner_vendor? BUSINESS_LOGGER.info(I18n.t('business_logger.reservation_service.form.hungry_hub.to_reservation.partner_vendor'), params: params.merge(payment_provider: payment_provider, payment_type: payment_type)) vendor_user_params = if user_id_param.present? User.fetch(user_params[:user_id]).slice(:name, :email, :phone) else guest_user_param || {} end # Add rwg_token to guest_user_attrs only for Google Reserve partner if vendor_service.partner_vendor&.name == ApiVendorV1::Constants::GOOGLE_RESERVE_VENDOR_NAME && params[:rwg_token].present? vendor_user_params[:rwg_token] = params[:rwg_token] end vendor_service.guest_user_attrs = vendor_user_params attrs[:vendor_reservation_attributes] = vendor_service.reservation_vendor_attributes end section_params = SectionTrees.build_section_trees(restaurant_package_params) attrs[:menu_sections_attributes] = section_params if section_params.present? group_section_params = SectionTrees.build_group_section_trees(restaurant_package_params) attrs[:group_sections_attributes] = group_section_params if group_section_params.present? reservation = if tmp_reservation tmp_reservation.assign_attributes(attrs) tmp_reservation else Reservation.new(attrs) end if @additional_special_request.present? reservation.additional_special_request = @additional_special_request.join(',') end if !reservation.group_booking && restaurant.any_offers? && reservation.created_by.to_s.to_sym == :user validate_reservation_packages(reservation) return false if errors.present? end if restaurant.any_offers? || (created_by_admin? && restaurant_package_params.present?) setup_reservation_packages(reservation) setup_reservation_add_ons(reservation) if restaurant_add_on_params.present? setup_accept_refund_property(reservation) end setup_tracking(reservation) store_lat_lng(reservation) if guest_user_param.present? prepayment_attr(reservation) if created_by_admin? set_default_start_time(reservation) if restaurant.any_offers? unless created_by_admin? reservation.valid? reservation.property.valid? end errors.add(:base, reservation.errors..to_sentence) if reservation.errors.present? if reservation.property&.errors&.present? errors.add(:base, reservation.property.errors..to_sentence) end return false if errors.present? reservation.dining_occasion = @dining_occasion if @dining_occasion.present? reservation.end_time = Receptionist::GetEndTime.determine_end_time_from_reservation(reservation) BUSINESS_LOGGER.info(I18n.t('business_logger.reservation_service.form.hungry_hub.to_reservation.success'), params: params.merge(payment_provider: payment_provider, payment_type: payment_type)) reservation end |
#validate ⇒ Object
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 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 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 26 def validate return true if params[:is_load_test] errors.clear BUSINESS_LOGGER.info(I18n.t('business_logger.reservation_service.form.hungry_hub.validate'), reservation_id: tmp_reservation&.id) unless reservation_params_valid? BUSINESS_LOGGER.error('Invalid reservation params', reservation_id: tmp_reservation&.id) return false end # Validate if the restaurant is for testing restaurant_testing_ids = AdminSetting.restaurant_ids_for_testing.split(',').map(&:to_i) if restaurant_testing_ids.include?(params.dig(:reservation, :restaurant_id).to_i) vendor_source = Doorkeeper::Application.find_by(uid: params.dig(:reservation, :special_request)) if vendor_source.blank? BUSINESS_LOGGER.error('This restaurant is for testing only', reservation_id: tmp_reservation&.id) errors.add(:base, 'This restaurant is for hungryhub internal team only') return false end end unless valid_creator? BUSINESS_LOGGER.error('Invalid creator', reservation_id: tmp_reservation&.id) errors.add(:base, 'invalid creator') return false end if user_params.blank? BUSINESS_LOGGER.error('User params is blank', reservation_id: tmp_reservation&.id) errors.add(:base, 'Invalid user params') return false end if created_by_admin? channel = Channel.find_by(channel_id: params[:reservation][:channel]) if params[:reservation][:vendor_booking_id].present? && channel&.oauth_application&.blank? BUSINESS_LOGGER.error('Invalid channel for 3rd party partner booking', reservation_id: tmp_reservation&.id) errors.add(:base, 'Please select the correct channel for 3rd party partner booking.') return false elsif channel&.oauth_application&.present? && params[:reservation][:vendor_booking_id].blank? BUSINESS_LOGGER.error('3rd party partner booking id is blank', reservation_id: tmp_reservation&.id) errors.add(:base, '3rd party partner booking id cannot be blank.') return false end end if voucher_param.present? && !created_by_admin? full_prepayment_vouchers = voucher_form.vouchers.select(&:require_full_prepayment?) if full_prepayment_vouchers.present? && !has_prepaid_param? && !skip_prepayment BUSINESS_LOGGER.error('Voucher require pre-payment', reservation_id: tmp_reservation&.id) errors.add(:base, 'Sorry, the voucher require pre-payment, Please select payment method') return false end end unless channel_params.is_a?(Integer) BUSINESS_LOGGER.error('Invalid channel', reservation_id: tmp_reservation&.id) errors.add(:base, 'Channel can not be blank') return false end unless reservation_params.fetch(:adult, 0).to_i.positive? BUSINESS_LOGGER.error('Adult can not be blank', reservation_id: tmp_reservation&.id) errors.add(:base, 'Party size cannot be 0') return false end if gb_primepay_card_param.present? && gb_primepay_card_param[:token].blank? BUSINESS_LOGGER.error('credit card token has a problem', reservation_id: tmp_reservation&.id) errors.add(:base, 'Something went wrong when processing your credit card, please re-enter your credit card') return false end unless restaurant.allow_booking? BUSINESS_LOGGER.error('Restaurant does not accept booking', reservation_id: tmp_reservation&.id) errors.add(:base, "This restaurant doesn't accept booking") return false end if voucher_param.present? && !voucher_form.validate BUSINESS_LOGGER.error(voucher_form., reservation_id: tmp_reservation&.id, voucher_params: voucher_param) errors.add(:base, voucher_form.) return false end unless Reservation.enumerized_attributes.attributes['medium'].values.include?(medium_params) errors.add(:base, 'Medium is not valid') return false end if created_by_params.blank? errors.add(:base, 'Created by can not be blank') return false end if group_booking_params.nil? errors.add(:base, 'Group booking can not be blank') return false end if invalid_dining_occasion? errors.add(:base, 'dining occasion not found') return false end if business_booking_params.nil? errors.add(:base, 'Business booking can not be blank') return false end unless valid_user_country? BUSINESS_LOGGER.error('Invalid user country', reservation_id: tmp_reservation&.id, user_country: user_country_param) APMErrorHandler.report("Invalid user country: #{user_country_param}", reservation_id: tmp_reservation&.id) end if !group_booking_params && restaurant_package_params.blank? && restaurant.active? && restaurant.any_offers? && !restaurant.allow_booking_without_package? BUSINESS_LOGGER.error('Restaurant has offers but no package selected', reservation_id: tmp_reservation&.id) errors.add(:base, 'Restaurant Package can not be blank') return false end # Additional validation to prevent different package types if restaurant_package_params.present? selected_package_types = restaurant_package_params.map { |rp_param| rp_param[:package].class.to_s }.uniq if selected_package_types.length > 1 BUSINESS_LOGGER.error('Booking cannot have different package types', reservation_id: tmp_reservation&.id) errors.add(:base, 'Booking cannot have different package types') return false end selected_packages_by_type = restaurant_package_params.group_by { |rp_param| rp_param[:package].class.to_s } selected_packages_by_type.each do |_package_type, packages| next if packages.length == 1 packages.each do |package| if !package[:package].allow_mix_and_match? BUSINESS_LOGGER.error("Mix and match is not allowed for #{package[:package].name}", reservation_id: tmp_reservation&.id) errors.add(:base, "Mix and match is not allowed for #{package[:package].name}") return false end end rescue StandardError => e APMErrorHandler.report(e, restaurant_package_params: restaurant_package_params, params: params) BUSINESS_LOGGER.error('Error when processing selected packages', reservation_id: tmp_reservation&.id) errors.add(:base, 'Something went wrong when processing selected packages') return false end end if corporate_event_id_param.present? corporate_event = Corporates::Event.find_by(id: corporate_event_id_param) if corporate_event.blank? errors.add(:base, 'Invalid Corporate Event') return false end event_package_ids = corporate_event.event_packages.map(&:restaurant_package_id).map(&:to_i) valid_selected_restaurant_package_ids = restaurant_package_params.select do |rp| event_package_ids.include?(rp[:id].to_i) end if (corporate_event.restaurant_id != restaurant.id) || (valid_selected_restaurant_package_ids.length != restaurant_package_params.length) errors.add(:base, 'Invalid Corporate Event data') return false end end if time_has_been_past? && !created_by_admin? && reservation_params[:is_order_now].to_s != 'true' && !reservation_meets_min_booking_time?(reservation_params[:service_type]) BUSINESS_LOGGER.error('Time has passed', reservation_id: tmp_reservation&.id) errors.add(:base, I18n.t('inventory.time_has_passed')) return false end if less_than_start_date? && !created_by_admin? BUSINESS_LOGGER.error('Booking dining time is less than restaurant start date', reservation_id: tmp_reservation&.id) errors.add(:base, "We open reservation after #{restaurant.start_date} onwards") return false end if invalid_special_promo? BUSINESS_LOGGER.error('Invalid promo code', reservation_id: tmp_reservation&.id) errors.add(:base, 'promo code is invalid') return false end # Check if the reservation status is arrived or pending_arrival if tmp_reservation.present? && (tmp_reservation.status_as_symbol == :arrived || tmp_reservation.status_as_symbol == :pending_arrival) BUSINESS_LOGGER.error('Invalid booking ID: Booking has already arrived or is pending arrival', reservation_id: tmp_reservation&.id) errors.add(:base, 'Invalid booking ID: Booking has already arrived or is pending arrival') return false end reservation = Reservation.new reservation_params.merge(channel: channel_params).merge(user_params) # validate vendor payment amount should be equal to or greater than the minimum price defined by HungryHub if has_prepaid_param? && vendor_payment.present? calc_package_price_result = calc_package_price( reservation.adult, reservation.kids, reservation.date.to_s, reservation.booking_channel, reservation.restaurant_id, ) if calc_package_price_result[:total_price].present? vendor_payment_amount = params[:vendor_payment][:amount_cents]&.to_i || params[:vendor_payment][:amount_subunits]&.to_i if vendor_payment_amount.present? && vendor_payment_amount == 0 BUSINESS_LOGGER.info( 'Vendor payment amount is zero in vendor reservation payment attributes', { tmp_reservation_id: tmp_reservation&.id, vendor: tmp_reservation&.booking_channel&.name, params: params.merge(payment_provider: payment_provider, payment_type: payment_type), }, ) elsif vendor_payment_amount.present? && calc_package_price_result[:total_price].to_i > vendor_payment_amount subject = "Vendor Price Mismatch | #{tmp_reservation.booking_channel.name} | restaurant_package_id: #{restaurant_package_params.first[:id]}" = 'Vendor payment amount is less than the minimum price, please check the payment amount.' end end if body = <<~EOF #{} <br> <b>Expected Vendor Payment Amount:</b> #{params[:vendor_payment][:amount_cents]} <br> <b>Calculated Price:</b> #{calc_package_price_result[:total_price]} <br> <b>Restaurant:</b> #{restaurant.name} (#{restaurant.id}) <br> <b>Reservation ID:</b> #{tmp_reservation.id} <br> <b>Reservation Date:</b> #{tmp_reservation.date} <br> <b>Reservation Time:</b> #{tmp_reservation.start_time} <br> <b>Reservation Channel:</b> #{tmp_reservation.channel} <br> EOF receivers = [SUPPORT_EMAIL_SLACK, VENDOR_TEAM_EMAIL] StaffMailer.notify(subject, body, receivers).deliver_later! APMErrorHandler.report(, params: params) end end unless Channel.manual.include?(channel_params) unless EmailValidator.valid?(reservation.email) BUSINESS_LOGGER.error('Invalid email', reservation_id: tmp_reservation&.id) errors.add(:email, 'is invalid') return false end if reservation.delivery? && !Phonelib.valid?(reservation.phone) BUSINESS_LOGGER.error('Invalid phone', reservation_id: tmp_reservation&.id) errors.add(:phone, 'is invalid') return false elsif reservation.user_id.present? && reservation.user.phone_v2_full.blank? BUSINESS_LOGGER.error('User\'s phone is blank', reservation_id: tmp_reservation&.id) errors.add(:phone, 'is blank, please open your profile page, and update your phone number') return false end end if guest_user_param.present? && guest_user_param[:phone].present? && !reservation&.booking_channel&.skip_vendor_phone_validation? && !Phonelib.valid?(guest_user_param[:phone]) BUSINESS_LOGGER.error('Invalid phone', reservation_id: tmp_reservation&.id) errors.add(:phone, 'is invalid') return false end if reservation.delivery? && !Phonelib.valid?(reservation.phone) errors.add(:phone, 'is invalid') return false end if reservation.is_order_now? && !reservation.restaurant.support_order_now? errors.add(:base, "This restaurant doesn't support order now") return false end if %i[delivery pick_up].include?(reservation.service_type.to_sym) && !reservation_phone_is_verified?(reservation) && with_phone_verification errors.add(:phone, I18n.t('reservation.phone_verification.failed')) return false end if restaurant_package_params.present? restaurant_package_params.each do |rp_param| package = rp_param[:package] next unless package.respond_to?(:delivery_fee) && !%i[delivery pick_up].include?(reservation.service_type.to_sym) errors.add(:base, 'you have to choose Delivery or Self Pick-Up method') break end if reservation.service_type.delivery? && !created_by_admin? delivery_packages = restaurant_package_params.select do |rp_param| rp_param[:package].respond_to?(:available_methods) && rp_param[:package].available_methods.to_s.include?('delivery') end if delivery_packages.blank? package_names = restaurant_package_params.map do |rp_param| rp_param[:package].name end errors.add(:base, I18n.t('errors.only_supports_self_pick_up', package_name: package_names.to_sentence)) return false end end end if restaurant_package_params.present? && reached_limit_to_buy_package_per_day?(reservation, restaurant_package_params) BUSINESS_LOGGER.error('Reached limit to buy package per day', reservation_id: tmp_reservation&.id) errors.add :base, 'You have reached the limit to buy this package per day' return false end if reservation.service_type.to_sym == :delivery && reservation.address.blank? errors.add(:delivery_address, 'can not be blank') return false end if !restaurant.any_offers? && reservation.start_time.blank? BUSINESS_LOGGER.error('Start time is blank', reservation_id: tmp_reservation&.id) errors.add(:base, 'Please select start time') return false end if !params[:is_load_test] && !skip_prepayment && !multiple_bookings_allowed?( reservation.user, reservation ) BUSINESS_LOGGER.error('Multiple bookings are not allowed', reservation_id: tmp_reservation&.id) errors.add(:base, ) return false end if params[:group_booking].to_s == 'true' && !restaurant.accept_group_booking? BUSINESS_LOGGER.error('Restaurant does not accept group booking', reservation_id: tmp_reservation&.id) errors.add(:base, "We're sorry, #{restaurant.name} doesn't accept group booking") return false end if accept_refund? && reservation.user_id.blank? BUSINESS_LOGGER.error('Refund guarantee is only for registered members', reservation_id: tmp_reservation&.id) errors.add(:base, I18n.t('errors.refund_guarantee.only_for_member')) return false end unless eligible_to_use_refund_guarantee? BUSINESS_LOGGER.error('Refund guarantee validation failed', reservation_id: tmp_reservation&.id) errors.add(:base, I18n.t('errors.refund_guarantee.not_eligible')) return false end if accept_refund? && refundable_period_has_passed? max_refund_hours = calculate_max_refund_hours_from_params BUSINESS_LOGGER.error( 'Refundable period has already passed', reservation_id: tmp_reservation&.id, max_refund_hours: max_refund_hours, reservation_date: reservation_params[:date], reservation_time: reservation_params[:start_time], ) errors.add(:base, I18n.t('errors.refund_guarantee.out_of_period', hours: max_refund_hours)) return false end if accept_refund? && !has_prepaid_param? && !skip_prepayment if created_by_admin? && params.dig(:reservation, :property_attributes, :is_prepayment).to_s == 'true' # Proceed to next step else BUSINESS_LOGGER.error('Refund guarantee requires prepayment', reservation_id: tmp_reservation&.id) errors.add(:base, I18n.t('errors.refund_guarantee.requires_prepayment')) return false end end return false if errors.present? BUSINESS_LOGGER.info( I18n.t('business_logger.reservation_service.form.hungry_hub.validate_success'), params: params.merge(payment_provider: payment_provider, payment_type: payment_type), ) true end |
#vendor_payment ⇒ Object
bypass the pre-payment if vendor payment present
525 526 527 |
# File 'app/services/reservation_service/form/hungry_hub.rb', line 525 def vendor_payment params[:vendor_payment].present? end |