From 6186e25a6353aa65920ee00eecc3d63bb8d2eed4 Mon Sep 17 00:00:00 2001 From: Andrey Bakanovsky Date: Tue, 12 Sep 2023 00:46:58 +0300 Subject: [PATCH] feat(notifications): create for account access recipients (#90) * feat(notification): create for account access recipient * feat(notification): fix linter warnings * feat(notification): update according to the comments --- app/controllers/spends_controller.rb | 5 +---- app/controllers/topups_controller.rb | 5 +---- app/mailers/transactions_mailer.rb | 6 +++--- app/services/send_notification.rb | 20 +++++++++++++++++++ .../accept_account_shares/show.html.slim | 2 +- app/views/accounts/edit.html.slim | 2 +- .../transaction_notification.html.erb | 18 ++++++----------- 7 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 app/services/send_notification.rb diff --git a/app/controllers/spends_controller.rb b/app/controllers/spends_controller.rb index d7ec76ac..dee0b08b 100644 --- a/app/controllers/spends_controller.rb +++ b/app/controllers/spends_controller.rb @@ -11,10 +11,7 @@ def create originator: current_user, access_token: Devise.friendly_token ) - - if account.notification? - TransactionsMailer.transaction_notification(account).deliver - end + SendNotification.call(account) redirect_to account_path(account) end diff --git a/app/controllers/topups_controller.rb b/app/controllers/topups_controller.rb index e06c8d6e..9f16a469 100644 --- a/app/controllers/topups_controller.rb +++ b/app/controllers/topups_controller.rb @@ -11,10 +11,7 @@ def create originator: current_user, access_token: Devise.friendly_token ) - - if account.notification? - TransactionsMailer.transaction_notification(account).deliver - end + SendNotification.call(account) redirect_to account_path(account) end diff --git a/app/mailers/transactions_mailer.rb b/app/mailers/transactions_mailer.rb index e22b3de0..03675e76 100644 --- a/app/mailers/transactions_mailer.rb +++ b/app/mailers/transactions_mailer.rb @@ -8,11 +8,11 @@ def automatic_top_up_done(transaction) mail to: @user.email, title: "Automatic top up account - #{@to_account.name}" end - def transaction_notification(account) + def transaction_notification(account, account_share = nil) @account = account @transaction = Transaction.last @transactions = @account.transactions.order(created_at: :desc).limit(5) - - mail to: @account.email, subject: "Transaction added." + + mail to: account_share&.email || @account.email, subject: "Transaction added." end end diff --git a/app/services/send_notification.rb b/app/services/send_notification.rb new file mode 100644 index 00000000..88a296ec --- /dev/null +++ b/app/services/send_notification.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class SendNotification + def initialize(account) + @account = account + end + + def self.call(...) + new(...).execute + end + + def execute + return unless @account.notification? + + TransactionsMailer.transaction_notification(@account).deliver + @account.account_shares.accepted.each do |account_share| + TransactionsMailer.transaction_notification(@account, account_share).deliver + end + end +end diff --git a/app/views/accept_account_shares/show.html.slim b/app/views/accept_account_shares/show.html.slim index 30bfc854..129d3b27 100644 --- a/app/views/accept_account_shares/show.html.slim +++ b/app/views/accept_account_shares/show.html.slim @@ -10,4 +10,4 @@ div.columns.is-centered.is-vcentered = link_to 'Accept', accept_account_share_path, method: :patch, class: 'button is-success is-medium' span.has-text-grey-light - = link_to 'Cancel', root_path, class: 'button is-light is-medium' + = link_to 'Cancel', request.referer.present? ? request.referer : root_path, class: 'button is-light is-medium' diff --git a/app/views/accounts/edit.html.slim b/app/views/accounts/edit.html.slim index a0b4b8e8..0ad334db 100644 --- a/app/views/accounts/edit.html.slim +++ b/app/views/accounts/edit.html.slim @@ -9,7 +9,7 @@ .field = f.check_box :notification, class: "checkbox" |   - = f.label :notification, "Approve transactions" + = f.label :notification, "Report transactions" br div.buttons.is-flex.is-justify-content-flex-end = link_to 'Back', account_path(account), class: 'button is-light' diff --git a/app/views/transactions_mailer/transaction_notification.html.erb b/app/views/transactions_mailer/transaction_notification.html.erb index 07c533d4..f1912ff6 100644 --- a/app/views/transactions_mailer/transaction_notification.html.erb +++ b/app/views/transactions_mailer/transaction_notification.html.erb @@ -1,15 +1,11 @@ -

Dear <%= @account.name%>, transaction added!

+

New transaction was added in <%= @account.name %>!

About transation:

-

From:

-<%= @transaction.from_account.name %> -

To:

-<%= @transaction.to_account.name %> -

Amount:

-<%= @transaction.amount %> -

Description:

-<%= @transaction.description %> +

From: <%= @transaction.from_account.name %>

+

To: <%= @transaction.to_account.name %>

+

Amount: <%= @transaction.signed_amount(@account) %>

+

Description: <%= @transaction.description %>

+

Current balance: <%= @account.balance %>


-

Click to link for approve transaction

<%= link_to 'Approve', '#' %>

Last 5 transactions:

@@ -29,5 +25,3 @@ <% end %>
- -