Skip to content

mail notification to origin and destination when transfer is done #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/controllers/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ class TransfersController < ApplicationController
def create
@source = find_source
@account = Account.find(transfer_params[:destination])
transfer = Transfer.new(
transfer_params.merge(source: @source, destination: @account)
)

transfer_creator = TransferCreator.new(
source: @source,
destination: @account,
amount: transfer_params[:amount],
reason: transfer_params[:reason],
post_id: transfer_params[:post_id]
)
begin
transfer.save!
transfer_creator.create!
rescue ActiveRecord::RecordInvalid
flash[:error] = transfer.errors.full_messages.to_sentence
flash[:error] = transfer_creator.transfer.errors.full_messages.to_sentence
end
redirect_to redirect_target
end
Expand Down
16 changes: 16 additions & 0 deletions app/mailers/payment_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class PaymentNotifier < ActionMailer::Base
default from: "\"TimeOverflow\" <info@timeoverflow.org>"

# @param username [String] username of the destination account
# @param time [String]
def transfer_source(user, username_destination, time)
mail(to: "#{user.email}", subject: default_i18n_subject(time: time, username_destination: username_destination), body: 'SI')
end

# @param username [String] username of the source account
# @param time [String]
def transfer_destination(user, username_source, time)
# Todo: Send with destination locale
mail(to: "#{user.email}", subject: default_i18n_subject(time: time, username_source: username_source), body: 'SI')
end
end
30 changes: 30 additions & 0 deletions app/services/transfer_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class TransferCreator

def initialize(source:, destination:, amount:, reason:, post_id:)
@source = source
@destination = destination
@amount = amount
@reason = reason
@post_id = post_id
end

attr_reader :transfer

def create!
@transfer = Transfer.new(
source: @source,
destination: @destination,
amount: @amount,
reason: @reason,
post_id: @post_id
)
@transfer.save! && notify_by_email
end

def notify_by_email
# mail notificación pago
#Todo: check accountable as organization
PaymentNotifier.transfer_source(@source.accountable.user, @destination.accountable.display_name_with_uid, @transfer.amount.to_f/3600).deliver_now
PaymentNotifier.transfer_destination(@destination.accountable.user, @source.accountable.display_name_with_uid, @transfer.amount.to_f/3600).deliver_now
end
end
14 changes: 10 additions & 4 deletions config/locales/email.ca.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
ca:
mailers_globals:
footer:
text: "%{organization_name} en"
text: "%{organization_name} a"

organization_notifier:
recent_posts:
subject: Boletín semanal
text1: "Últimas ofertas publicadas:"
text2: "Últimas demandas publicadas:"
subject: "Butlletí setmanal"
text1: "Darreres ofertes publicades:"
text2: "Darreres demandes publicades:"

payment_notifier:
transfer_source:
subject: "Has pagat %{time} hores a %{username_destination}"
transfer_destination:
subject: "Has rebut %{time} hores de %{username_source}"
8 changes: 7 additions & 1 deletion config/locales/email.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ en:
recent_posts:
subject: Newsletter
text1: "Latest offers published:"
text2: "Lastest inquiries published:"
text2: "Lastest inquiries published:"

payment_notifier:
transfer_source:
subject: "You paid %{time} hours to %{username_destination}"
transfer_destination:
subject: "You received %{time} hours from %{username_source}"
6 changes: 6 additions & 0 deletions config/locales/email.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ es:
subject: Boletín semanal
text1: "Últimas ofertas publicadas:"
text2: "Últimas demandas publicadas:"

payment_notifier:
transfer_source:
subject: "Has pagado %{time} horas a %{username_destination}"
transfer_destination:
subject: "Has recibido %{time} horas de %{username_source}"
16 changes: 16 additions & 0 deletions lib/time_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class TimeFormatter
def mdash
raw "&mdash;"
end

def seconds_to_h_m(seconds)
sign = seconds <=> 0
if sign.try :nonzero?
minutes, _seconds = seconds.abs.divmod(60)
hours, minutes = minutes.divmod(60)
raw format("%s%d:%02d", ("-" if sign < 0), hours, minutes)
else
mdash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Que es mdash? No lo veo definido en ningun sitio.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esto... copy paste de /helpers/application_helper.rb función seconds_to_hm(seconds) 😬 me falto tampien copy paste de

def mdash
    raw "&mdash;"
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vas a necesitar raw tambien me temo.... y creo que es cosa de vistas...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ademas, no dupliques codigo. Muevelo y usa en todos sitios la clase esa.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me temo que estás haciendo de Ruby aquí. Si no me equivoco #strftime hace esto ya http://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/DateTime.html#method-i-strftime

end
end
end
27 changes: 27 additions & 0 deletions spec/services/transfer_creator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe TransferCreator do
describe '#create!' do
let(:jordi_member) { Fabricate(:member) }
let(:organization) { member.organization }
let(:paco_member) { Fabricate(:member, organization: organization) }
let(:jordi) { jordi_member.user }
let(:paco) { paco_member.user }
let(:amount) { 3 }
let(:post) { double(Post, id: 666) }

subject do
described_class.new(
source: jordi,
destination: paco,
amount: amount,
reason: reason,
post_id: post.id
).create!
end

it 'Creates a new Transfer' do
expect(subject).to be true
end
end
end