Class: Agents::Update

Inherits:
Base
  • Object
show all
Includes:
ErrorType
Defined in:
app/my_lib/agents/update.rb

Overview

This builder will update reservation as usual

Constant Summary

Constants inherited from Base

Base::WORKER_DELAY_TIME

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods included from ErrorType

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

Methods inherited from Base

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

Methods included from SharedJobs

#give_campaign_reward, #send_rating_email

Constructor Details

#initialize(reservation, reservation_params = nil, force_update = nil) ⇒ Update

Returns a new instance of Update.



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

def initialize(reservation, reservation_params = nil, force_update = nil)
  @reservation = if reservation.is_a?(::Reservation)
                   reservation
                 else
                   ::Reservation.find(reservation)
                 end

  self.vendor_booking_id = reservation_params.delete(:vendor_booking_id) if reservation_params.present?
  @reservation_params = reservation_params
  @reservation.assign_attributes(reservation_params) if reservation_params.present?
  @force_update = force_update.to_s == 'true'

  # Set audit comment
  after_initialize
end

Dynamic Method Handling

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

Instance Method Details

#after_initializeObject



32
33
34
# File 'app/my_lib/agents/update.rb', line 32

def after_initialize
  reservation.audit_comment = 'updated by user'
end

#execute!Object



40
41
42
# File 'app/my_lib/agents/update.rb', line 40

def execute!
  update_booking!
end

#update_bookingObject Also known as: execute



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
# File 'app/my_lib/agents/update.rb', line 51

def update_booking
  successfully_updated = update_booking!

  # TODO: move below code to child class
  if reservation.by_marketplace?
    vendor_name = reservation.vendor_reservation.oauth_application.name
    case vendor_name
    when ApiVendorV1::Constants::OPEN_RICE_VENDOR_NAME
      status = if !confirmed? && !arrived? && !cancelled? && !no_show_changed? && !reservation.no_show? && !reservation.arrived? && !reservation.cancelled? && !reservation.rejected?
                 ApiVendorV1::Constants::UPDATE
               elsif arrived? || reservation.arrived?
                 ApiVendorV1::Constants::CONFIRM
               elsif cancelled? || reservation.cancelled? || reservation.rejected?
                 ApiVendorV1::Constants::CANCEL
               elsif no_show_changed? || reservation.no_show?
                 ApiVendorV1::Constants::NO_SHOW
               else
                 raise NotImplementedError
               end
      send_to_openrice_webhook(status) if status.present?
    when ApiVendorV1::Constants::TAG_THAI_VENDOR_NAME
      tagthai_status = if !confirmed? && !arrived? && !cancelled? && !no_show_changed? && !reservation.no_show? && !reservation.arrived? && !reservation.cancelled? && !reservation.rejected?
                         ApiVendorV1::Constants::CANCEL_MODIFIED
                       elsif arrived? || reservation.arrived?
                         ApiVendorV1::Constants::CONFIRM
                       elsif cancelled? || reservation.cancelled? || reservation.rejected?
                         ApiVendorV1::Constants::CANCEL
                       elsif no_show_changed? || reservation.no_show?
                         ApiVendorV1::Constants::NO_SHOW
                       else
                         raise NotImplementedError
                       end
      send_to_tagthai_webhook(tagthai_status) if tagthai_status.present?
    when ApiVendorV1::Constants::GOOGLE_RESERVE_VENDOR_NAME
      google_reserve_status = if cancelled? || reservation.cancelled? || reservation.rejected?
                                ApiVendorV1::Constants::RWG_DECLINED_BY_MERCHANT
                              elsif reservation.party_size_changed? || reservation.start_time_and_date_changed?
                                ApiVendorV1::Constants::RWG_CONFIRMED
                              end
      send_to_google_reserve_webhook(google_reserve_status) if google_reserve_status.present?
    else
      APMErrorHandler.report("Invalid vendor name: #{vendor_name}")
    end
  end

  if successfully_updated
    # Use context-aware sync strategy: immediate for admin/staff/owner, delayed for users
    if reservation.created_by_admin_staff_or_owner?
      reservation.trigger_immediate_sync
    else
      reservation.trigger_summary_sync
    end
  end

  successfully_updated
rescue InvalidReservation => e
  APMErrorHandler.report(e)
  false
rescue ActiveRecord::ActiveRecordError, StandardError => e
  APMErrorHandler.report(e)
  false
end

#update_booking!Object



36
37
38
# File 'app/my_lib/agents/update.rb', line 36

def update_booking!
  raise 'Not implemented error'
end