Module: SpreeCmCommissioner::Admin::OrdersControllerDecorator

Defined in:
app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 4

def self.prepended(base)
  # spree try to create an empty order in the #new action
  base.around_action :set_writing_role, only: i[new edit]

  base.before_action :load_order, only: i[
    edit update cancel resume approve resend open_adjustments
    close_adjustments cart channel set_channel
    accept_all reject_all alert_request_to_vendor
    notifications fire_notification queue_webhooks_requests update_order_status
  ]

  base.before_action :initialize_notification_methods, only: i[
    notifications
    fire_notification
    queue_webhooks_requests
  ]
end

Instance Method Details

#accept_allObject



22
23
24
25
26
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 22

def accept_all
  @order.accepted_by(try_spree_current_user)
  flash[:success] = Spree.t(:accepted)
  redirect_back fallback_location: spree.edit_admin_order_url(@order)
end

#alert_request_to_vendorObject



34
35
36
37
38
39
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 34

def alert_request_to_vendor
  @order.send_order_request_telegram_confirmation_alert_to_vendor

  flash[:success] = Spree.t(:alerted_to_vendor)
  redirect_back fallback_location: spree.edit_admin_order_url(@order)
end

#fire_notificationObject



41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 41

def fire_notification
  method = @notification_methods.find { |e| e == params['notification_method'] }

  if method.present? && @order.send(method)
    flash[:success] = Spree.t(:sent)
  else
    flash[:error] = Spree.t(:send_failed_or_method_not_support)
  end

  redirect_back fallback_location: notifications_admin_order_url(@order)
end

#initialize_notification_methodsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 98

def initialize_notification_methods
  @notification_methods = %w[
    send_order_complete_telegram_alert_to_vendors
    send_order_complete_telegram_alert_to_store

    send_order_requested_telegram_alert_to_store
    send_order_accepted_telegram_alert_to_store
    send_order_rejected_telegram_alert_to_store

    notify_order_complete_app_notification_to_user
    notify_order_complete_telegram_notification_to_user
  ]

  @webhook_events = [
    'order.create',
    'order.delete',
    'order.update',
    'order.canceled',
    'order.placed',
    'order.resumed',
    'order.shipped'
  ]
end

#initialize_order_eventsObject

override



94
95
96
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 94

def initialize_order_events
  @order_events = %w[alert_request_to_vendor approve cancel resume]
end

#notificationsObject



70
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 70

def notifications; end

#queue_webhooks_requestsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 53

def queue_webhooks_requests
  event = @webhook_events.find { |e| e == params['event'] }

  if event.present?
    Spree::Webhooks::Subscribers::QueueRequests.call(
      event_name: event,
      webhook_payload_body: @order.send(:webhook_payload_body),
      **@order.send(:webhooks_request_options)
    )
    flash[:success] = Spree.t(:sent)
  else
    flash[:error] = Spree.t(:send_failed_or_method_not_support)
  end

  redirect_back fallback_location: notifications_admin_order_url(@order)
end

#reject_allObject



28
29
30
31
32
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 28

def reject_all
  @order.rejected_by(try_spree_current_user)
  flash[:success] = Spree.t(:rejected)
  redirect_back fallback_location: spree.edit_admin_order_url(@order)
end

#resumeObject

override



83
84
85
86
87
88
89
90
91
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 83

def resume
  resumed = @order.resume
  if resumed
    flash[:success] = 'Order resumed' # rubocop:disable Rails/I18nLocaleTexts
  else
    flash[:error] = @order.errors.full_messages.to_sentence
  end
  redirect_back fallback_location: spree.edit_admin_order_url(@order)
end

#update_order_statusObject



72
73
74
75
76
77
78
79
80
# File 'app/controllers/spree_cm_commissioner/admin/orders_controller_decorator.rb', line 72

def update_order_status
  if @order.next
    flash[:success] = I18n.t('orders.update_order_status.success')
  else
    error_message = @order.errors.full_messages.to_sentence
    flash[:error] = (error_message.presence || I18n.t('orders.update_order_status.fail'))
  end
  redirect_back fallback_location: spree.edit_admin_order_url(@order)
end