Module: SpreeCmCommissioner::OrderStateMachine
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/spree_cm_commissioner/order_state_machine.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
-
#accepted_by(user) ⇒ Object
allow authorized user to accept all requested line items.
- #allocate_resources ⇒ Object
-
#can_accept_all? ⇒ Boolean
can_accepted? already use by ransack/visitor.rb.
- #can_alert_request_to_vendor? ⇒ Boolean
- #can_reject_all? ⇒ Boolean
-
#clear_cancellation_details ⇒ Object
Reset the cancellation info when an order is resumed.
- #de_allocate_resources ⇒ Object
- #decrement_user_request_orders_count ⇒ Object
-
#deliver_order_confirmation_email ⇒ Object
override.
- #finalize_guests_and_bibs ⇒ Object
-
#generate_completion_steps! ⇒ Object
This is called before complete, which is already wrapped by the transaction of unstock_inventory!.
- #increment_user_request_orders_count ⇒ Object
- #need_confirmation? ⇒ Boolean
- #notify_order_complete_app_notification_to_user ⇒ Object
- #notify_order_complete_telegram_notification_to_user ⇒ Object
- #precalculate_conversion ⇒ Object
- #product_completion_steps_exist? ⇒ Boolean
- #rejected_by(user) ⇒ Object
- #requested_state? ⇒ Boolean
- #restock_inventory! ⇒ Object
- #run_order_complete_alerts ⇒ Object
- #send_dynamic_field_notification_to_guests ⇒ 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
-
#telegram_alert_enabled? ⇒ Boolean
Telegram order alerts are sent unless EVERY purchased product opts out.
- #unstock_inventory! ⇒ Object
- #user_has_email? ⇒ Boolean
-
#vendor_alert_title(vendor) ⇒ Object
White-label vendors announce their own brand; platform vendors stay BookMe+.
Instance Method Details
#accepted_by(user) ⇒ Object
allow authorized user to accept all requested line items
134 135 136 137 138 139 140 141 142 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 134 def accepted_by(user) transaction do accept! line_items.each do |line_item| line_item.accepted_by(user) end end end |
#allocate_resources ⇒ Object
378 379 380 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 378 def allocate_resources SpreeCmCommissioner::VotingCreditAllocationJob.perform_later(order_id: id) end |
#can_accept_all? ⇒ Boolean
can_accepted? already use by ransack/visitor.rb
240 241 242 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 240 def can_accept_all? approved? && requested? end |
#can_alert_request_to_vendor? ⇒ Boolean
248 249 250 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 248 def can_alert_request_to_vendor? !accepted? && approved? && need_confirmation? end |
#can_reject_all? ⇒ Boolean
244 245 246 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 244 def can_reject_all? approved? && requested? end |
#clear_cancellation_details ⇒ Object
Reset the cancellation info when an order is resumed. We only assign the values here; the resume transition already saves the order, so they get saved together with it (one validated save, no update_columns).
389 390 391 392 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 389 def clear_cancellation_details self.canceled_at = nil self.canceler_id = nil end |
#de_allocate_resources ⇒ Object
382 383 384 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 382 def de_allocate_resources SpreeCmCommissioner::VotingCreditDeAllocationJob.perform_later(order_id: id) end |
#decrement_user_request_orders_count ⇒ Object
107 108 109 110 111 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 107 def decrement_user_request_orders_count return if user.nil? SpreeCmCommissioner::UserCountersService.decrement_request_orders_count(user) end |
#deliver_order_confirmation_email ⇒ Object
override
94 95 96 97 98 99 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 94 def deliver_order_confirmation_email return if requested? return if any_app_only_products? super end |
#finalize_guests_and_bibs ⇒ Object
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 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 186 def finalize_guests_and_bibs ActiveRecord::Base.transaction do line_items.each do |line_item| line_item.generate_remaining_guests line_item.guests.each do |guest| result = SpreeCmCommissioner::Guests::Finalize.call( guest: guest, user: user, event: line_item.event, variant: line_item.variant ) unless result.success? guest.errors.add(:base, result.error.value || result.error.to_s) raise(ActiveRecord::RecordInvalid, guest) end end end end true rescue ActiveRecord::RecordInvalid => e e.record.errors.each do |msg| errors.add(:guests, msg) end false end |
#generate_completion_steps! ⇒ Object
This is called before complete, which is already wrapped by the transaction of unstock_inventory!
118 119 120 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 118 def generate_completion_steps! line_items.each(&:generate_completion_steps!) end |
#increment_user_request_orders_count ⇒ Object
101 102 103 104 105 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 101 def increment_user_request_orders_count return if user.nil? SpreeCmCommissioner::UserCountersService.increment_request_orders_count(user) end |
#need_confirmation? ⇒ Boolean
256 257 258 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 256 def need_confirmation? line_items.any?(&:need_confirmation) end |
#notify_order_complete_app_notification_to_user ⇒ Object
308 309 310 311 312 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 308 def notify_order_complete_app_notification_to_user return if products.all?(&:vote_package?) # disable app push notification for vote package orders SpreeCmCommissioner::OrderCompleteNotificationSender.call(order: self) end |
#notify_order_complete_telegram_notification_to_user ⇒ Object
314 315 316 317 318 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 314 def notify_order_complete_telegram_notification_to_user return unless telegram_alert_enabled? SpreeCmCommissioner::OrderCompleteTelegramSenderJob.perform_later(order_id: id) if user_id.present? end |
#precalculate_conversion ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 122 def precalculate_conversion event_ids = line_items.pluck(:event_id).compact.uniq event_ids.each do |event_id| # Just create task, CRON job will pick it up and execute. SpreeCmCommissioner::MaintenanceTasks::Event.pending.create_or_find_by( maintainable_type: 'Spree::Taxon', maintainable_id: event_id ) end end |
#product_completion_steps_exist? ⇒ Boolean
113 114 115 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 113 def product_completion_steps_exist? product_completion_steps.any? end |
#rejected_by(user) ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 216 def rejected_by(user) transaction do reject! line_items.each do |line_item| line_item.rejected_by(user) end end end |
#requested_state? ⇒ Boolean
252 253 254 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 252 def requested_state? request_state == 'requested' end |
#restock_inventory! ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 171 def restock_inventory! ActiveRecord::Base.transaction do restock_inventory_from_external_system! release_order_holds! restock_inventory_in_redis! end rescue StandardError => e CmAppLogger.log( label: 'SpreeCmCommissioner::OrderStateMachine#restock_inventory! failed', data: { order_id: id, error: e., type: e.class.name, backtrace: e.backtrace&.first(5)&.join("\n") } ) raise e end |
#run_order_complete_alerts ⇒ Object
359 360 361 362 363 364 365 366 367 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 359 def run_order_complete_alerts return unless ENV['ORDER_INTEGRITY_TELEGRAM_ALERT_ENABLED'] == 'yes' return unless telegram_alert_enabled? # Allow async payment processors time to settle before checking for alerts SpreeCmCommissioner::TelegramAlerts::OrderIntegrityCheckJob .set(wait: 2.minutes) .perform_later(order_id: id) end |
#send_dynamic_field_notification_to_guests ⇒ Object
271 272 273 274 275 276 277 278 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 271 def send_dynamic_field_notification_to_guests incomplete_guests = line_items.flat_map(&:guests).select(&:incomplete?) return if incomplete_guests.empty? incomplete_guests.each do |guest| SpreeCmCommissioner::Guests::SendDynamicFieldNotification.call(guest: guest) end end |
#send_order_accepted_app_notification_to_user ⇒ Object
324 325 326 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 324 def send_order_accepted_app_notification_to_user SpreeCmCommissioner::OrderAcceptedNotificationSender.call(order: self) end |
#send_order_accepted_telegram_alert_to_store ⇒ Object
341 342 343 344 345 346 347 348 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 341 def send_order_accepted_telegram_alert_to_store return unless telegram_alert_enabled? 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
369 370 371 372 373 374 375 376 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 369 def send_order_complete_telegram_alert_to_store return unless telegram_alert_enabled? 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
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 280 def send_order_complete_telegram_alert_to_vendors return unless telegram_alert_enabled? vendor_list.each do |vendor| # `next`, not `return`: the mask is per-vendor, so one opted-out vendor on a # multi-vendor order must not suppress the others. next unless vendor.alerts_telegram_for_channel?(channel_key) chat_id = vendor.preferred_telegram_chat_id next if chat_id.blank? factory = OrderTelegramMessageFactory.new(title: vendor_alert_title(vendor), order: self, vendor: vendor) TelegramNotificationSenderJob.perform_later(chat_id: chat_id, message: factory., parse_mode: factory.parse_mode) end end |
#send_order_rejected_app_notification_to_user ⇒ Object
328 329 330 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 328 def send_order_rejected_app_notification_to_user SpreeCmCommissioner::OrderRejectedNotificationSender.call(order: self) end |
#send_order_rejected_telegram_alert_to_store ⇒ Object
350 351 352 353 354 355 356 357 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 350 def send_order_rejected_telegram_alert_to_store return unless telegram_alert_enabled? 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
260 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 260 def send_order_request_telegram_confirmation_alert_to_vendor; end |
#send_order_requested_app_notification_to_user ⇒ Object
320 321 322 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 320 def send_order_requested_app_notification_to_user SpreeCmCommissioner::OrderRequestedNotificationSender.call(order: self) end |
#send_order_requested_telegram_alert_to_store ⇒ Object
332 333 334 335 336 337 338 339 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 332 def send_order_requested_telegram_alert_to_store return unless telegram_alert_enabled? 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
226 227 228 229 230 231 232 233 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 226 def send_transaction_email_to_user line_items.each do |line_item| next if line_item.event.nil? next unless line_item. SpreeCmCommissioner::EventTransactionalMailer.send_to_participant(line_item.event_id, user.id).deliver_later end end |
#telegram_alert_enabled? ⇒ Boolean
Telegram order alerts are sent unless EVERY purchased product opts out. Default is true (see product_decorator), so existing orders are unaffected. Checked at enqueue time so disabled products create no telegram jobs at all.
265 266 267 268 269 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 265 def telegram_alert_enabled? return @telegram_alert_enabled if defined?(@telegram_alert_enabled) @telegram_alert_enabled = line_items.any?(&:enable_telegram_alert?) end |
#unstock_inventory! ⇒ Object
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 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 144 def unstock_inventory! ActiveRecord::Base.transaction do yield if block_given? # 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_order_holds!` 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. unstock_inventory_from_external_system! reserve_order_holds! 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#unstock_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
235 236 237 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 235 def user_has_email? user.present? && user.email.present? end |
#vendor_alert_title(vendor) ⇒ Object
White-label vendors announce their own brand; platform vendors stay BookMe+.
vendor.name is operator-supplied data interpolated into an HTML parse_mode message (TelegramMessageFactory#header wraps the title in ), so it MUST be escaped — Telegram rejects unescaped &, < and > with "400 can't parse entities", and TelegramNotificationSender swallows that error, so the alert would vanish silently.
302 303 304 305 306 |
# File 'app/models/concerns/spree_cm_commissioner/order_state_machine.rb', line 302 def vendor_alert_title(vendor) return '🎫 --- [NEW ORDER FROM BOOKME+] ---' unless vendor.tenant_present? "🎫 --- [NEW ORDER FROM #{CGI.escapeHTML(vendor.name.to_s)}] ---" end |