Module: SpreeCmCommissioner::OrderStateMachine
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/spree_cm_commissioner/order_state_machine.rb
Instance Method Summary collapse
-
#accepted_by(user) ⇒ Object
allow authorized user to accept all requested line items.
-
#can_accept_all? ⇒ Boolean
can_accepted? already use by ransack/visitor.rb.
- #can_alert_request_to_vendor? ⇒ Boolean
- #can_reject_all? ⇒ Boolean
-
#confirmation_delivered? ⇒ Boolean
overrided not to send email yet to user if order needs confirmation it will send after vendors accepted.
- #generate_bib_number ⇒ Object
-
#has_incomplete_guest_info? ⇒ Boolean
rubocop:disable Naming/PredicateName.
- #mark_user_as_has_incomplete_guest_info ⇒ Object
- #need_confirmation? ⇒ Boolean
- #notify_order_complete_app_notification_to_user ⇒ Object
- #notify_order_complete_telegram_notification_to_user ⇒ Object
- #precalculate_conversion ⇒ Object
- #rejected_by(user) ⇒ Object
- #requested_state? ⇒ Boolean
- #restock_inventory! ⇒ Object
- #send_order_accepted_app_notification_to_user ⇒ Object
- #send_order_accepted_telegram_alert_to_store ⇒ Object
- #send_order_complete_telegram_alert_to_store ⇒ Object
- #send_order_complete_telegram_alert_to_vendors ⇒ Object
- #send_order_rejected_app_notification_to_user ⇒ Object
- #send_order_rejected_telegram_alert_to_store ⇒ Object
- #send_order_request_telegram_confirmation_alert_to_vendor ⇒ Object
- #send_order_requested_app_notification_to_user ⇒ Object
- #send_order_requested_telegram_alert_to_store ⇒ Object
- #send_transaction_email_to_user ⇒ Object
- #unstock_inventory! ⇒ Object
- #user_has_email? ⇒ Boolean
Instance Method Details
#accepted_by(user) ⇒ Object
allow authorized user to accept all requested line items
163 164 165 166 167 168 169 170 171 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 163 def accepted_by(user) transaction do accept! line_items.each do |line_item| line_item.accepted_by(user) end end end |
#can_accept_all? ⇒ Boolean
can_accepted? already use by ransack/visitor.rb
180 181 182 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 180 def can_accept_all? approved? && requested? end |
#can_alert_request_to_vendor? ⇒ Boolean
188 189 190 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 188 def can_alert_request_to_vendor? !accepted? && approved? && need_confirmation? end |
#can_reject_all? ⇒ Boolean
184 185 186 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 184 def can_reject_all? approved? && requested? end |
#confirmation_delivered? ⇒ Boolean
overrided not to send email yet to user if order needs confirmation it will send after vendors accepted.
175 176 177 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 175 def confirmation_delivered? confirmation_delivered || need_confirmation? end |
#generate_bib_number ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 123 def generate_bib_number line_items.find_each(&:generate_remaining_guests) line_items.with_bib_prefix.each do |line_item| line_item.guests.none_bib.each do |guest| guest.generate_bib next if guest.save guest.errors.each do |msg| errors.add(:guests, msg) end end end errors.empty? end |
#has_incomplete_guest_info? ⇒ Boolean
rubocop:disable Naming/PredicateName
77 78 79 80 81 82 83 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 77 def has_incomplete_guest_info? # rubocop:disable Naming/PredicateName line_items.any? do |line_item| line_item.guests.any? do |guest| !guest.post_registration_completed? end end end |
#mark_user_as_has_incomplete_guest_info ⇒ Object
85 86 87 88 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 85 def mark_user_as_has_incomplete_guest_info user&.&.[]=('has_incomplete_guest_info', true) user&.save! end |
#need_confirmation? ⇒ Boolean
196 197 198 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 196 def need_confirmation? line_items.any?(&:need_confirmation) end |
#notify_order_complete_app_notification_to_user ⇒ Object
213 214 215 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 213 def notify_order_complete_app_notification_to_user SpreeCmCommissioner::OrderCompleteNotificationSender.call(order: self) end |
#notify_order_complete_telegram_notification_to_user ⇒ Object
217 218 219 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 217 def notify_order_complete_telegram_notification_to_user SpreeCmCommissioner::OrderCompleteTelegramSenderJob.perform_later(id) if user_id.present? end |
#precalculate_conversion ⇒ Object
71 72 73 74 75 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 71 def precalculate_conversion line_items.each do |item| SpreeCmCommissioner::ConversionPreCalculatorJob.perform_later(item.product_id) end end |
#rejected_by(user) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 140 def rejected_by(user) transaction do reject! line_items.each do |line_item| line_item.rejected_by(user) end end end |
#requested_state? ⇒ Boolean
192 193 194 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 192 def requested_state? request_state == 'requested' end |
#restock_inventory! ⇒ Object
116 117 118 119 120 121 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 116 def restock_inventory! ActiveRecord::Base.transaction do cancel_blocks! restock_inventory_in_redis! end end |
#send_order_accepted_app_notification_to_user ⇒ Object
225 226 227 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 225 def send_order_accepted_app_notification_to_user SpreeCmCommissioner::OrderAcceptedNotificationSender.call(order: self) end |
#send_order_accepted_telegram_alert_to_store ⇒ Object
240 241 242 243 244 245 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 240 def send_order_accepted_telegram_alert_to_store title = '✅ --- [ORDER ACCEPTED BY VENDOR] ---' chat_id = store.preferred_telegram_order_request_alert_chat_id factory = OrderTelegramMessageFactory.new(title: title, order: self) TelegramNotificationSenderJob.perform_later(chat_id: chat_id, message: factory., parse_mode: factory.parse_mode) end |
#send_order_complete_telegram_alert_to_store ⇒ Object
254 255 256 257 258 259 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 254 def send_order_complete_telegram_alert_to_store title = '🎫 --- [NEW ORDER] ---' chat_id = store.preferred_telegram_order_alert_chat_id factory = OrderTelegramMessageFactory.new(title: title, order: self) TelegramNotificationSenderJob.perform_later(chat_id: chat_id, message: factory., parse_mode: factory.parse_mode) end |
#send_order_complete_telegram_alert_to_vendors ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 202 def send_order_complete_telegram_alert_to_vendors vendor_list.each do |vendor| title = '🎫 --- [NEW ORDER FROM BOOKME+] ---' chat_id = vendor.preferred_telegram_chat_id if chat_id.present? factory = OrderTelegramMessageFactory.new(title: title, order: self, vendor: vendor) TelegramNotificationSenderJob.perform_later(chat_id: chat_id, message: factory., parse_mode: factory.parse_mode) end end end |
#send_order_rejected_app_notification_to_user ⇒ Object
229 230 231 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 229 def send_order_rejected_app_notification_to_user SpreeCmCommissioner::OrderRejectedNotificationSender.call(order: self) end |
#send_order_rejected_telegram_alert_to_store ⇒ Object
247 248 249 250 251 252 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 247 def send_order_rejected_telegram_alert_to_store title = '❌ --- [ORDER REJECTED BY VENDOR] ---' chat_id = store.preferred_telegram_order_request_alert_chat_id factory = OrderTelegramMessageFactory.new(title: title, order: self) TelegramNotificationSenderJob.perform_later(chat_id: chat_id, message: factory., parse_mode: factory.parse_mode) end |
#send_order_request_telegram_confirmation_alert_to_vendor ⇒ Object
200 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 200 def send_order_request_telegram_confirmation_alert_to_vendor; end |
#send_order_requested_app_notification_to_user ⇒ Object
221 222 223 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 221 def send_order_requested_app_notification_to_user SpreeCmCommissioner::OrderRequestedNotificationSender.call(order: self) end |
#send_order_requested_telegram_alert_to_store ⇒ Object
233 234 235 236 237 238 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 233 def send_order_requested_telegram_alert_to_store title = '🔔 --- [NEW REQUESTED BY USER] ---' chat_id = store.preferred_telegram_order_request_alert_chat_id factory = OrderTelegramMessageFactory.new(title: title, order: self) TelegramNotificationSenderJob.perform_later(chat_id: chat_id, message: factory., parse_mode: factory.parse_mode) end |
#send_transaction_email_to_user ⇒ Object
150 151 152 153 154 155 156 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 150 def send_transaction_email_to_user line_items.each do |line_item| next if line_item.event.nil? SpreeCmCommissioner::EventTransactionalMailer.send_to_participant(line_item.event_id, user.id).deliver_later end end |
#unstock_inventory! ⇒ Object
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 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 90 def unstock_inventory! ActiveRecord::Base.transaction do yield # Equal to block.call # After the transition is complete, the following code will execute first before proceeding to other `after_transition` callbacks. # This ensures that if `reserve_blocks!` or `unstock_inventory_in_redis!` fails, the state will be rolled back, # and neither the `finalize!` method nor any notifications will be triggered. # The payment will be reversed in vPago gem, and `Spree::Checkout::Complete` will be called, which checks `order.reload.complete?`. # This is critical because if the order state is complete, the payment will be marked as paid. reserve_blocks! unstock_inventory_in_redis! # We rollback only order state, and we keep payment state as it is. # We implement payment in vPago gem, and it will be reversed in the gem. # Some bank has api for refund, but some don't have the api to refund yet. So we keep the payment state as it is and refund manually. end rescue StandardError => e CmAppLogger.log( label: 'SpreeCmCommissioner::OrderStateMachine#reserve_inventory failed', data: { order_id: id, error: e., type: e.class.name, backtrace: e.backtrace&.first(5)&.join("\n") } ) raise e end |
#user_has_email? ⇒ Boolean
158 159 160 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 158 def user_has_email? user.present? && user.email.present? end |