Class: PackageBooking::Users::Update

Inherits:
Agents::ModifyBookingForUser show all
Includes:
Components::ViewDecorator, CustomPackagePricing
Defined in:
app/my_lib/package_booking/users/update.rb

Overview

TODO: create `update_reservation_service.rb` to replace this class

Constant Summary

Constants inherited from Agents::Base

Agents::Base::WORKER_DELAY_TIME

Instance Attribute Summary collapse

Attributes inherited from Agents::Base

#audit_comment, #errors, #executor, #force_update, #owner, #reservation, #reservation_params, #restaurant, #user, #vendor_booking_id

Instance Method Summary collapse

Methods included from CustomPackagePricing

#apply_custom_package_pricing_feature

Methods included from Components::ViewDecorator

#origin_selected_packages, #reservation_attributes, #selected_packages, #state

Methods inherited from Agents::ModifyBookingForUser

#execute, #execute!

Methods included from RefundGuaranteeHelper

#calculate_min_refund_hours, #calculate_refundable_until_time

Methods included from Reservations::CheckSeatIncreased

#change_packages?, #increase_seat_only?

Methods inherited from Agents::Update

#after_initialize, #execute!, #update_booking, #update_booking!

Methods included from Agents::ErrorType

#fatal_error?, #inventory_error?, #normal_error?, #overwrite_error_type!

Methods inherited from Agents::Base

#error_message, #hotline, #save_reservation!, #status=

Methods included from Agents::SharedJobs

#give_campaign_reward, #send_rating_email

Constructor Details

#initialize(reservation_id, params, package:, vendor_reservation_params: nil) ⇒ Update

Returns a new instance of Update.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/my_lib/package_booking/users/update.rb', line 16

def initialize(reservation_id, params, package:, vendor_reservation_params: nil)
  self.reservation = ::Reservation.includes(:restaurant).find(reservation_id)
  self.reservation = reservation.decorate
  self.restaurant = reservation.restaurant
  self.reservation_params = normalize_parameters(params).merge(modified_by: :user)
  self.vendor_reservation_params = vendor_reservation_params

  reservation.build_property if reservation.property.blank?

  @new_reservation = reservation.object.dup
  @new_reservation.build_property if @new_reservation.property.blank?

  @old_reservation_attributes = reservation.attributes
  @old_reservation_property_attributes = reservation.property&.attributes

  reservation.assign_attributes(reservation_params)
  self.package = package
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Agents::Base

Instance Attribute Details

#packageObject

Returns the value of attribute package.



14
15
16
# File 'app/my_lib/package_booking/users/update.rb', line 14

def package
  @package
end

#vendor_reservation_paramsObject

Returns the value of attribute vendor_reservation_params.



14
15
16
# File 'app/my_lib/package_booking/users/update.rb', line 14

def vendor_reservation_params
  @vendor_reservation_params
end

Instance Method Details

#change_personal_data_or_special_request_only?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
# File 'app/my_lib/package_booking/users/update.rb', line 113

def change_personal_data_or_special_request_only?
  return false if has_change_package_attr?

  origin_change_personal_data_or_special_request_only?
end

#decrease_seat_only?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/my_lib/package_booking/users/update.rb', line 108

def decrease_seat_only?
  !package_attr_changed? && origin_decrease_seat_only?
end

#has_change_package_attr?Boolean

Returns:

  • (Boolean)


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
# File 'app/my_lib/package_booking/users/update.rb', line 45

def has_change_package_attr?
  return false if reservation.package.blank? && package.blank?

  return false if package.blank?

  # If reservation had no package before, adding a package is a change
  return true if reservation.package.blank?

  package.each do |param|
    existing_package_bought = reservation.package['package_data'].find do |pd|
      pd['restaurant_package_id'].to_i == param['id'].to_i
    end
    return true if existing_package_bought.blank?

    return true if existing_package_bought['quantity'].to_i != param['quantity'].to_i
  end

  return true if package.count != reservation.package['package_data'].count

  # check if group sections was change for DIY package
  if reservation.package[:package_type] == HhPackage::Package::Diy::TYPE_SHORT &&
      group_sections_changed?(package, reservation)
    return true
  end

  false
end

#inventory_available?(adult = reservation.adult, kids = reservation.kids) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'app/my_lib/package_booking/users/update.rb', line 97

def inventory_available?(adult = reservation.adult, kids = reservation.kids)
  origin_inventory_available?(adult, kids)
  package_available?
  adult_count_within_party_limit?
  package_bookable?(adult, kids)
end

#non_prepaid_packages?Boolean

check previous package is non_prepaid

Returns:

  • (Boolean)


74
75
76
77
78
# File 'app/my_lib/package_booking/users/update.rb', line 74

def non_prepaid_packages?
  return false if reservation.package.blank? || reservation.package['package_data'].blank?

  reservation.package['package_data'].any? { |h| h['prepayment_percent'].nil? }
end

#nonprepaid_to_prepaid_switch?Boolean

check if user switch from non_prepaid to prepaid package

Returns:

  • (Boolean)


91
92
93
# File 'app/my_lib/package_booking/users/update.rb', line 91

def nonprepaid_to_prepaid_switch?
  has_change_package_attr? && non_prepaid_packages? && prepaid_packages?
end

#origin_change_personal_data_or_special_request_only?Object



112
# File 'app/my_lib/package_booking/users/update.rb', line 112

alias origin_change_personal_data_or_special_request_only? change_personal_data_or_special_request_only?

#origin_decrease_seat_only?Object

override parent #decrease_seat_only? if customer updated packages data only, it should be true otherwise call super



107
# File 'app/my_lib/package_booking/users/update.rb', line 107

alias origin_decrease_seat_only? decrease_seat_only?

#origin_inventory_available?Object



95
# File 'app/my_lib/package_booking/users/update.rb', line 95

alias origin_inventory_available? inventory_available?

#origin_primary_attr_changed?Object

check whether user updated package data or booking details data



36
# File 'app/my_lib/package_booking/users/update.rb', line 36

alias origin_primary_attr_changed? primary_attr_changed?

#prepaid_packages?Boolean

check new package is prepaid

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'app/my_lib/package_booking/users/update.rb', line 81

def prepaid_packages?
  return false if package.blank?

  package.any? do |package_params|
    rest_pack = HhPackage::RestaurantPackage.fetch(package_params[:id])
    rest_pack.package.prepaid_package?
  end
end

#primary_attr_changed?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'app/my_lib/package_booking/users/update.rb', line 37

def primary_attr_changed?
  unless package_attr_changed? || origin_primary_attr_changed?
    errors.add(:base, 'We can not process your request if no data changed')
    return false
  end
  true
end

#validate_min_spending_tier(new_adult) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'app/my_lib/package_booking/users/update.rb', line 119

def validate_min_spending_tier(new_adult)
  return nil if reservation.package.blank?

  total_package_price = Money.new(reservation.package[:price_cents], reservation.package[:price_currency]).amount
  HhPackage::ReservationPackages::Validator.validate_minimum_spending(
    reservation.package[:package_data],
    total_package_price,
    new_adult,
  )
end