add slack notifications

This commit is contained in:
João Geonizeli
2021-08-17 10:04:24 -03:00
parent 559b239c02
commit 0b7cdeb236
9 changed files with 68 additions and 10 deletions

View File

@@ -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

View 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

View File

@@ -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

View File

@@ -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

View 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