Module: Spree::Admin::WebhookEndpointsHelper

Defined in:
app/helpers/spree/admin/webhook_endpoints_helper.rb

Constant Summary collapse

EVENTABLE_MODELS =

Models that have publishes_lifecycle_events enabled These models publish created/updated/deleted events

%w[
  asset
  customer_return
  digital
  digital_link
  export
  gift_card
  gift_card_batch
  import
  line_item
  newsletter_subscriber
  order
  payment
  post
  post_category
  price
  product
  promotion
  refund
  report
  return_authorization
  shipment
  stock_item
  stock_movement
  stock_transfer
  store_credit
  user
  variant
  wished_item
  wishlist
].freeze

Instance Method Summary collapse

Instance Method Details

#available_webhook_eventsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/spree/admin/webhook_endpoints_helper.rb', line 40

def available_webhook_events
  @available_webhook_events ||= begin
    events = []

    # Add lifecycle events from eventable models
    EVENTABLE_MODELS.each do |model_name|
      events << "#{model_name}.created"
      events << "#{model_name}.updated"
      events << "#{model_name}.deleted"
    end

    # Add custom events
    events += custom_webhook_events

    events.sort.uniq
  end
end

#webhook_delivery_status_badge(delivery) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'app/helpers/spree/admin/webhook_endpoints_helper.rb', line 58

def webhook_delivery_status_badge(delivery)
  if delivery.pending?
    (:span, Spree.t(:pending), class: 'badge badge-light')
  elsif delivery.successful?
    (:span, Spree.t(:success), class: 'badge badge-success')
  else
    (:span, Spree.t('state_machine_states.failed'), class: 'badge badge-danger')
  end
end

#webhook_endpoint_health_badge(endpoint) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/spree/admin/webhook_endpoints_helper.rb', line 74

def webhook_endpoint_health_badge(endpoint)
  if endpoint.auto_disabled?
    (:span, Spree.t('admin.webhook_endpoints.health.disabled'), class: 'badge badge-danger')
  elsif endpoint.webhook_deliveries.none?
    (:span, Spree.t('admin.webhook_endpoints.health.no_deliveries'), class: 'badge badge-light')
  else
    pct = webhook_endpoint_success_percentage(endpoint)
    if pct >= 95
      (:span, Spree.t('admin.webhook_endpoints.health.healthy', percentage: pct), class: 'badge badge-success')
    elsif pct >= 80
      (:span, Spree.t('admin.webhook_endpoints.health.degraded', percentage: pct), class: 'badge badge-warning')
    else
      (:span, Spree.t('admin.webhook_endpoints.health.failing', percentage: pct), class: 'badge badge-danger')
    end
  end
end

#webhook_endpoint_success_percentage(webhook_endpoint) ⇒ Object



68
69
70
71
72
# File 'app/helpers/spree/admin/webhook_endpoints_helper.rb', line 68

def webhook_endpoint_success_percentage(webhook_endpoint)
  return nil if webhook_endpoint.webhook_deliveries.none?

  (webhook_endpoint.webhook_deliveries.successful.count / webhook_endpoint.webhook_deliveries.count.to_f * 100).round(2)
end