diff --git a/.env b/.env index 091dc95..e787d58 100644 --- a/.env +++ b/.env @@ -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= diff --git a/Gemfile b/Gemfile index 81dd888..bf70e13 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index 27d14eb..89105c8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/app/models/buy_crypto_order.rb b/app/models/buy_crypto_order.rb index 58aa0d2..9ecdb52 100644 --- a/app/models/buy_crypto_order.rb +++ b/app/models/buy_crypto_order.rb @@ -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 diff --git a/app/models/concerns/notifiable.rb b/app/models/concerns/notifiable.rb new file mode 100644 index 0000000..18ed3f8 --- /dev/null +++ b/app/models/concerns/notifiable.rb @@ -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 diff --git a/app/models/sell_crypto_order.rb b/app/models/sell_crypto_order.rb index 7a9233e..2b64346 100644 --- a/app/models/sell_crypto_order.rb +++ b/app/models/sell_crypto_order.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index fadf31e..5b82b24 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/services/slack_notifier.rb b/app/services/slack_notifier.rb new file mode 100644 index 0000000..1a2e0c7 --- /dev/null +++ b/app/services/slack_notifier.rb @@ -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 diff --git a/erd.svg b/erd.svg index e9b3d71..1428561 100644 --- a/erd.svg +++ b/erd.svg @@ -67,13 +67,13 @@ stringβ€Šβˆ— - + m_Currency->m_Balance - + m_Currency->m_BuyCryptoOrder @@ -96,7 +96,7 @@ integer (8)β€Šβˆ— FK - + m_Currency->m_SellCryptoOrder @@ -151,30 +151,30 @@ string - + m_User->m_Balance - + m_User->m_BuyCryptoOrder - + m_User->m_FiatBalance - + m_User->m_SellCryptoOrder - + m_User->m_StakeOrder