Skip to content

Commit

Permalink
feat(notifications): create for account access recipients (#90)
Browse files Browse the repository at this point in the history
* feat(notification): create for account access recipient

* feat(notification): fix linter warnings

* feat(notification): update according to the comments
  • Loading branch information
andreybakanovsky authored Sep 11, 2023
1 parent 17d268a commit 6186e25
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
5 changes: 1 addition & 4 deletions app/controllers/spends_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/topups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/mailers/transactions_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions app/services/send_notification.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/accept_account_shares/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion app/views/accounts/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 6 additions & 12 deletions app/views/transactions_mailer/transaction_notification.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<h1>Dear <%= @account.name%>, transaction added!</h1>
<h1>New transaction was added in <%= @account.name %>!</h1>
<h3>About transation:</h3>
<p>From:</p>
<%= @transaction.from_account.name %>
<p>To:</p>
<%= @transaction.to_account.name %>
<p>Amount:</p>
<%= @transaction.amount %>
<p>Description:</p>
<%= @transaction.description %>
<p>From: <%= @transaction.from_account.name %> </p>
<p>To: <%= @transaction.to_account.name %> </p>
<p>Amount: <%= @transaction.signed_amount(@account) %> </p>
<p>Description: <%= @transaction.description %> </p>
<p>Current balance: <%= @account.balance %> </p>
<br>
<p>Click to link for approve transaction </p><%= link_to 'Approve', '#' %>
<h3> Last 5 transactions: </h3>
<table>
<tr>
Expand All @@ -29,5 +25,3 @@
</tr>
<% end %>
</table>


0 comments on commit 6186e25

Please sign in to comment.