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

2
.env
View File

@@ -4,7 +4,7 @@ MAILTRAP_ADDRESS=smtp.mailtrap.io
MAILTRAP_DOMAIN=smtp.mailtrap.io
MAILTRAP_PORT=2525
MAILER_DEFAULT_URL_HOST=localhost:5000
# SLACK_WEBHOOK_URL=https://hooks.slack.com/services/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
# production
# MAILGUN_DOMAIN=

View File

@@ -17,7 +17,7 @@ gem "image_processing", "~> 1.12"
gem "devise"
gem "devise-i18n"
gem "slack-notifier"
gem "administrate-field-active_storage"
gem "administrate-field-enumerize"
gem "tailwindcss-rails"

View File

@@ -297,6 +297,7 @@ GEM
semantic_range (3.0.0)
shoulda-matchers (5.0.0)
activesupport (>= 5.2.0)
slack-notifier (2.4.0)
spring (2.1.1)
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
@@ -367,6 +368,7 @@ DEPENDENCIES
rubocop-shopify
sass-rails (>= 6)
shoulda-matchers (~> 5.0)
slack-notifier
spring
tailwindcss-rails
turbolinks (~> 5)

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

16
erd.svg
View File

@@ -67,13 +67,13 @@
<text text-anchor="start" x="44" y="-458.5" font-family="Arial Italic" font-size="10.00" fill="#999999">string</text>
</g>
<!-- m_Currency&#45;&gt;m_Balance -->
<g id="edge8" class="edge">
<g id="edge9" class="edge">
<title>m_Currency&#45;&gt;m_Balance</title>
<path fill="none" stroke="black" d="M120.56,-492.09C146.51,-506.62 181.46,-526.2 212.21,-543.42"/>
<polygon fill="black" stroke="black" points="210.95,-546.32 220.34,-547.97 214.03,-540.83 210.95,-546.32"/>
</g>
<!-- m_Currency&#45;&gt;m_BuyCryptoOrder -->
<g id="edge5" class="edge">
<g id="edge6" class="edge">
<title>m_Currency&#45;&gt;m_BuyCryptoOrder</title>
<path fill="none" stroke="black" d="M153.08,-470.5C164.62,-470.5 176.72,-470.5 188.7,-470.5"/>
<polygon fill="black" stroke="black" points="188.82,-473.65 197.82,-470.5 188.82,-467.35 188.82,-473.65"/>
@@ -96,7 +96,7 @@
<text text-anchor="start" x="245" y="-307.5" font-family="Arial Italic" font-size="10.00" fill="#999999">integer (8) FK</text>
</g>
<!-- m_Currency&#45;&gt;m_SellCryptoOrder -->
<g id="edge7" class="edge">
<g id="edge8" class="edge">
<title>m_Currency&#45;&gt;m_SellCryptoOrder</title>
<path fill="none" stroke="black" d="M116.84,-448.73C139.24,-434.72 169.37,-415.89 197.77,-398.15"/>
<polygon fill="black" stroke="black" points="199.76,-400.62 205.72,-393.18 196.42,-395.27 199.76,-400.62"/>
@@ -151,30 +151,30 @@
<text text-anchor="start" x="109" y="-182.5" font-family="Arial Italic" font-size="10.00" fill="#999999">string</text>
</g>
<!-- m_User&#45;&gt;m_Balance -->
<g id="edge2" class="edge">
<g id="edge3" class="edge">
<title>m_User&#45;&gt;m_Balance</title>
<path fill="none" stroke="black" d="M106.63,-294.01C122.7,-333.81 143.96,-387.11 162,-434.5 178.66,-478.25 167.99,-497.56 198,-533.5 200.43,-536.42 203.09,-539.2 205.91,-541.85"/>
<polygon fill="black" stroke="black" points="203.89,-544.27 212.76,-547.79 208.02,-539.51 203.89,-544.27"/>
</g>
<!-- m_User&#45;&gt;m_BuyCryptoOrder -->
<g id="edge4" class="edge">
<g id="edge5" class="edge">
<title>m_User&#45;&gt;m_BuyCryptoOrder</title>
<path fill="none" stroke="black" d="M115.16,-294.4C136.17,-329.91 165.48,-374.29 198,-408.5 200.6,-411.23 203.33,-413.93 206.16,-416.59"/>
<polygon fill="black" stroke="black" points="204.11,-418.98 212.9,-422.68 208.34,-414.31 204.11,-418.98"/>
</g>
<!-- m_User&#45;&gt;m_FiatBalance -->
<g id="edge3" class="edge">
<g id="edge4" class="edge">
<title>m_User&#45;&gt;m_FiatBalance</title>
<path fill="none" stroke="black" d="M162.2,-233.5C178.28,-233.5 195.08,-233.5 210.83,-233.5"/>
</g>
<!-- m_User&#45;&gt;m_SellCryptoOrder -->
<g id="edge6" class="edge">
<g id="edge7" class="edge">
<title>m_User&#45;&gt;m_SellCryptoOrder</title>
<path fill="none" stroke="black" d="M162.2,-278.41C172.76,-284.33 183.63,-290.41 194.32,-296.4"/>
<polygon fill="black" stroke="black" points="193.05,-299.3 202.44,-300.95 196.12,-293.8 193.05,-299.3"/>
</g>
<!-- m_User&#45;&gt;m_StakeOrder -->
<g id="edge9" class="edge">
<g id="edge2" class="edge">
<title>m_User&#45;&gt;m_StakeOrder</title>
<path fill="none" stroke="black" d="M162.2,-191C175.51,-183.94 189.32,-176.62 202.62,-169.57"/>
<polygon fill="black" stroke="black" points="204.35,-172.22 210.83,-165.22 201.4,-166.65 204.35,-172.22"/>

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB