Class: PackageBooking::Owners::Update

Inherits:
Agents::Update show all
Includes:
Agents::SectionTrees, Components::ViewDecorator, CustomPackagePricing
Defined in:
app/my_lib/package_booking/owners/update.rb

Overview

Responsible to update reservation + package from Owner perspective

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 Agents::SectionTrees

#build_group_section_trees, #build_section_trees

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::Update

#after_initialize, #execute!, #update_booking

Methods included from Agents::ErrorType

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

Methods inherited from Agents::Base

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

Methods included from Agents::SharedJobs

#give_campaign_reward, #send_rating_email

Constructor Details

#initialize(reservation_id, reservation_params = nil, package = nil, add_on = nil, force_update = nil) ⇒ Update

Returns a new instance of Update.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/my_lib/package_booking/owners/update.rb', line 18

def initialize(reservation_id, reservation_params = nil, package = nil, add_on = nil, force_update = nil)
  # force_update is for updating reservation without checking inventory of TableCheck/SevenRooms
  # if fore_update is true, it will skip the inventory check
  @force_update = force_update.to_s == 'true'

  @reservation = ::Reservation.find(reservation_id)
  @invalid_date = false
  if reservation_params.present?
    @reservation_params = reservation_params.merge(modified_by: :owner).tap do |param|
      if param[:address_attributes].present? && param[:address_attributes][:delivery_channel_id] == '0' && @reservation.address.present?
        param[:address_attributes][:delivery_channel_id] = @reservation.restaurant.self_delivery_channel.id
        param[:address_attributes][:id] = @reservation.address.id
      end
    end

    @invalid_date = (@reservation.reservation_time + 24.hours).past?

    @reservation.assign_attributes @reservation_params
  end
  self.package = package if package.present?
  self.add_on = add_on

  @reservation.end_time = Receptionist::GetEndTime.determine_end_time_from_reservation(@reservation)
  @reservation.assign_charged_data
  @reservation.audit_comment = if @reservation.use_third_party_reservation?
                                 @reservation.note
                               else
                                 'updated by owner'
                               end
end

Dynamic Method Handling

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

Instance Attribute Details

#add_onObject

Returns the value of attribute add_on.



16
17
18
# File 'app/my_lib/package_booking/owners/update.rb', line 16

def add_on
  @add_on
end

#packageObject

Returns the value of attribute package.



16
17
18
# File 'app/my_lib/package_booking/owners/update.rb', line 16

def package
  @package
end

Instance Method Details

#update_booking!Object



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

def update_booking!
  if @invalid_date
    errors.add(:base, I18n.t('views.booking.cant_edit_has_24_hours'))
    raise Agents::InvalidReservation
  end

  if reservation.changes.keys == ['table']
    reservation.save!
    return true
  end
  validate_owner_parameters!
  check_no_show!
  package_available?

  begin
     if package.present?

    if add_on.present?
      add_on_params = Array(add_on).map do |param|
        if param[:id].to_i.positive?
          restaurant_add_on = AddOns::Restaurant.fetch(param[:id])
          param[:restaurant_add_on] = restaurant_add_on
          param[:add_on] = restaurant_add_on.add_on
        else
          errors.add(:base, 'invalid add on')
        end

        param
      end

      (add_on_params) if add_on_params.present?
    end
  rescue InvalidPackageData => e
    errors.add(:base, e.message)
    raise Agents::InvalidReservation
  end

  ::Reservation.transaction do
    # skip check inventory TableCheck/SevenRooms if force_update is true
    # so the reservation in their side will not be updated
    unless @force_update
      update_sr_result = update_seven_rooms_reservation
      unless update_sr_result.success?
        error_msg = SevenRooms::ErrorMessages.error_message('owner', update_sr_result.message)
        errors.add(:base, error_msg)
        raise Agents::InvalidReservation, error_msg
      end

      update_tc_result = update_tablecheck_reservation
      unless update_tc_result.success?
        error_msg = Tablecheck::ErrorMessages.error_message('owner', update_tc_result.message)
        errors.add(:base, error_msg)
        raise Agents::InvalidReservation, error_msg
      end

      update_weeloy_result = update_weeloy_reservation
      unless update_weeloy_result.success?
        error_msg = Weeloy::ErrorMessages.error_message('owner', update_weeloy_result.message)
        errors.add(:base, error_msg)
        raise Agents::InvalidReservation, error_msg
      end

      update_tc_result = update_bistrochat_reservation
      unless update_tc_result.success?
        errors.add(:base, Bistrochat::ErrorMessages.error_message('owner', update_tc_result.message))
        raise Agents::InvalidReservation
      end

      update_mm_result = update_mymenu_reservation
      unless update_mm_result.success?
        errors.add(:base, MyMenu::ErrorMessages.error_message('owner', update_mm_result.message))
        raise Agents::InvalidReservation
      end
    end

    raise Agents::InvalidReservation unless update_status

    begin
      reservation.assign_charged_data
      if reservation.saved_change_to_no_show? || reservation.saved_change_to_arrived? || !reservation.saved_change_to_active?
        reservation.save(validate: false)
      else
        reservation.save!
      end
      reservation.property.save! if package.present?
      update_order_now_reminder(reservation)
      give_rewards(reservation)
    rescue StandardError => e
      errors.add(:base, e.message)
      raise Agents::InvalidReservation
    end
  end

  true
end