Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/controllers/api/v1/customers/wallets/alerts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def render_alert(alert)
end

def create_params
params.require(:alert).permit(:alert_type, :code, :name, thresholds: %i[code value recurring])
params.require(:alert).permit(:alert_type, :code, :name, thresholds: [:code, :value, :recurring, {notify_on: []}])
end

def update_params
params.require(:alert).permit(:code, :name, thresholds: %i[code value recurring])
params.require(:alert).permit(:code, :name, thresholds: [:code, :value, :recurring, {notify_on: []}])
end

def batch_create_params
params.permit(alerts: [:alert_type, :code, :name, {thresholds: %i[code value recurring]}])
params.permit(alerts: [:alert_type, :code, :name, {thresholds: [:code, :value, :recurring, {notify_on: []}]}])
end

def resource_name
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/subscriptions/alerts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ def render_alert(alert)
end

def create_params
params.require(:alert).permit(:alert_type, :code, :name, :billable_metric_code, thresholds: %i[code value recurring])
params.require(:alert).permit(:alert_type, :code, :name, :billable_metric_code, thresholds: [:code, :value, :recurring, {notify_on: []}])
end

def update_params
params.require(:alert).permit(:code, :name, :billable_metric_code, thresholds: %i[code value recurring])
params.require(:alert).permit(:code, :name, :billable_metric_code, thresholds: [:code, :value, :recurring, {notify_on: []}])
end

def batch_create_params
params.permit(alerts: [:alert_type, :code, :name, :billable_metric_code, {thresholds: %i[code value recurring]}])
params.permit(alerts: [:alert_type, :code, :name, :billable_metric_code, {thresholds: [:code, :value, :recurring, {notify_on: []}]}])
end

def resource_name
Expand Down
13 changes: 13 additions & 0 deletions app/graphql/types/usage_monitoring/alerts/notify_on_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Types
module UsageMonitoring
module Alerts
class NotifyOnEnum < Types::BaseEnum
::UsageMonitoring::AlertThreshold::NOTIFY_ON_VALUES.each do |transition|
value transition
end
end
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/usage_monitoring/alerts/threshold_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module UsageMonitoring
module Alerts
class ThresholdInput < BaseInputObject
argument :code, String, required: false
argument :notify_on, [NotifyOnEnum], required: false,
description: "Transitions this threshold notifies on. Must include triggered. Adding resolved requires a code that is unique within the alert, and is not supported on recurring thresholds."
argument :recurring, Boolean, required: false
argument :value, String, required: true
end
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/usage_monitoring/alerts/threshold_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ThresholdObject < Types::BaseObject
graphql_name "AlertThreshold"

field :code, String, null: true
field :notify_on, [NotifyOnEnum], null: false,
description: "Transitions this threshold notifies on. Always includes triggered; resolved is opt in."
field :recurring, Boolean, null: false
field :value, String, null: false
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/usage_monitoring/alert_threshold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class UsageMonitoring::AlertThreshold < ApplicationRecord
SOFT_LIMIT = 20
NOTIFY_ON_TRIGGERED = "triggered"
NOTIFY_ON_RESOLVED = "resolved"
NOTIFY_ON_VALUES = [NOTIFY_ON_TRIGGERED, NOTIFY_ON_RESOLVED].freeze

belongs_to :organization
belongs_to :alert,
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/v1/usage_monitoring/alert_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def formatted_thresholds
{
code: threshold.code,
value: threshold.value,
recurring: threshold.recurring
recurring: threshold.recurring,
notify_on: threshold.notify_on
}
end
end
Expand Down
8 changes: 6 additions & 2 deletions app/services/usage_monitoring/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ class BaseService < ::BaseService

def prepare_thresholds(thresholds, organization_id)
thresholds.map do |threshold_params|
{
row = {
organization_id:,
code: nil,
recurring: false
}.merge(threshold_params.to_h)
}.merge(threshold_params.to_h).with_indifferent_access

# An explicit null would break the NOT NULL column, so let its default stand instead
row.delete(:notify_on) if row[:notify_on].nil?
row
end
end
end
Expand Down
41 changes: 38 additions & 3 deletions app/services/usage_monitoring/concerns/create_or_update_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ def find_billable_metric_from_params!
end

def duplicate_threshold_values?(thresholds)
threshold_keys = thresholds.map { |t| [t[:value], ActiveModel::Type::Boolean.new.cast(t[:recurring]) || false] }
threshold_keys = thresholds.map { |t| [t[:value], recurring_param?(t)] }
threshold_keys.size != threshold_keys.uniq.size
end

def recurring_param?(threshold)
ActiveModel::Type::Boolean.new.cast(threshold[:recurring]) || false
end

def all_threshold_values_present?(thresholds)
thresholds.none? { it[:value].nil? }
end
Expand All @@ -30,15 +34,46 @@ def all_threshold_values_numeric?(thresholds)
thresholds.all? { |t| valid_numeric_value?(t[:value]) }
end

def validate_notify_on!(thresholds)
if thresholds.any? { notify_on_for(it).any? { |value| AlertThreshold::NOTIFY_ON_VALUES.exclude?(value) } }
return result.single_validation_failure!(field: "thresholds:notify_on", error_code: "value_is_invalid")
end

if thresholds.any? { notify_on_for(it).exclude?(AlertThreshold::NOTIFY_ON_TRIGGERED) }
return result.single_validation_failure!(field: "thresholds:notify_on", error_code: "triggered_is_mandatory")
end

opting_in = thresholds.select { notify_on_for(it).include?(AlertThreshold::NOTIFY_ON_RESOLVED) }

if opting_in.any? { recurring_param?(it) }
result.single_validation_failure!(field: "thresholds:notify_on", error_code: "recurring_not_supported")
elsif opting_in.any? { it[:code].blank? }
result.single_validation_failure!(field: "thresholds:code", error_code: "value_is_mandatory")
elsif opted_in_code_not_unique?(thresholds, opting_in)
result.single_validation_failure!(field: :thresholds, error_code: "duplicate_threshold_codes")
end
end

def all_recurring_threshold_values_positive?(thresholds)
thresholds.all? do |t|
recurring = ActiveModel::Type::Boolean.new.cast(t[:recurring])
value = ActiveModel::Type::Decimal.new.cast(t[:value])

!recurring || value.positive?
!recurring_param?(t) || value.positive?
end
end

def notify_on_for(threshold)
values = threshold[:notify_on]
return [AlertThreshold::NOTIFY_ON_TRIGGERED] if values.nil?

Array(values).map(&:to_s)
end

def opted_in_code_not_unique?(thresholds, opting_in)
codes = thresholds.filter_map { it[:code].presence }
opting_in.any? { codes.count(it[:code]) > 1 }
end

def valid_numeric_value?(value)
case value
when Numeric
Expand Down
3 changes: 3 additions & 0 deletions app/services/usage_monitoring/create_alert_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def call
return result.single_validation_failure!(field: "thresholds:value", error_code: "recurring_value_is_negative")
end

validate_notify_on!(params[:thresholds])
return result unless result.success?

billable_metric = find_billable_metric_from_params!
return result unless result.success?

Expand Down
3 changes: 3 additions & 0 deletions app/services/usage_monitoring/update_alert_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def call
if !all_recurring_threshold_values_positive?(params[:thresholds])
return result.single_validation_failure!(field: "thresholds:value", error_code: "recurring_value_is_negative")
end

validate_notify_on!(params[:thresholds])
return result unless result.success?
end

result.alert = alert
Expand Down
17 changes: 17 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions spec/graphql/mutations/wallets/alerts/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,50 @@
{"code" => nil, "value" => "1000.0", "recurring" => true}
)
end

context "with notifyOn" do
let(:mutation) do
<<-GQL
mutation ($input: CreateCustomerWalletAlertInput!) {
createCustomerWalletAlert(input: $input) {
thresholds { code notifyOn }
}
}
GQL
end

let(:input) do
{
walletId: wallet.id,
code: "wallet_balance_alert",
alertType: "wallet_balance_amount",
thresholds: [
{code: "warn", value: "5000", notifyOn: %w[triggered resolved]},
{code: "alert", value: "2500"}
]
}
end

it "round-trips the opted-in transitions" do
expect(result["data"]["createCustomerWalletAlert"]["thresholds"]).to contain_exactly(
{"code" => "warn", "notifyOn" => %w[triggered resolved]},
{"code" => "alert", "notifyOn" => %w[triggered]}
)
end

context "when a threshold opts in without a code" do
let(:input) do
{
walletId: wallet.id,
code: "wallet_balance_alert",
alertType: "wallet_balance_amount",
thresholds: [{value: "5000", notifyOn: %w[triggered resolved]}]
}
end

it "returns a validation error" do
expect(result["errors"].first["extensions"]["details"]).to eq({"thresholds:code" => ["value_is_mandatory"]})
end
end
end
end
Loading
Loading