add slack notifications
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#
|
||||
class BuyCryptoOrder < ApplicationRecord
|
||||
include Processable
|
||||
include Notifiable
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :currency
|
||||
@@ -33,4 +34,14 @@ class BuyCryptoOrder < ApplicationRecord
|
||||
|
||||
validates :paid_amount_cents, presence: true, numericality: { greater_than: 0 }
|
||||
validates :received_amount, presence: true, if: :completed?
|
||||
|
||||
private
|
||||
|
||||
def notification_message
|
||||
"
|
||||
💸 New buy crypto order! 💸 \n
|
||||
user: #{user.email} \n
|
||||
amount: #{paid_amount.format}
|
||||
"
|
||||
end
|
||||
end
|
||||
|
||||
18
app/models/concerns/notifiable.rb
Normal file
18
app/models/concerns/notifiable.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
# frozen_string_literal: true
|
||||
module Notifiable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
after_commit on: :create do
|
||||
SlackNotifier.ping(notification_message)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def notification_message
|
||||
raise NotImplementedError, "method should be implemented"
|
||||
end
|
||||
end
|
||||
@@ -25,6 +25,7 @@
|
||||
#
|
||||
class SellCryptoOrder < ApplicationRecord
|
||||
include Processable
|
||||
include Notifiable
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :currency
|
||||
@@ -33,4 +34,14 @@ class SellCryptoOrder < ApplicationRecord
|
||||
|
||||
validates :paid_amount, presence: true, numericality: { greater_than: 0 }
|
||||
validates :received_amount_cents, presence: true, if: :completed?
|
||||
|
||||
private
|
||||
|
||||
def notification_message
|
||||
"
|
||||
💸 New sell crypto order! 💸\n
|
||||
user: #{user.email} \n
|
||||
amount: #{paid_amount} #{currency.name}
|
||||
"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
# index_users_on_reset_password_token (reset_password_token) UNIQUE
|
||||
#
|
||||
class User < ApplicationRecord
|
||||
include Notifiable
|
||||
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :validatable
|
||||
|
||||
@@ -43,4 +45,10 @@ class User < ApplicationRecord
|
||||
def create_balances
|
||||
CreateUserBalances.new(self).call
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def notification_message
|
||||
"🎉 New user: #{email} 🎉"
|
||||
end
|
||||
end
|
||||
|
||||
8
app/services/slack_notifier.rb
Normal file
8
app/services/slack_notifier.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
module SlackNotifier
|
||||
def self.ping(message)
|
||||
return if ENV["SLACK_WEBHOOK_URL"].blank?
|
||||
|
||||
Slack::Notifier.new(ENV["SLACK_WEBHOOK_URL"]).ping(message)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user