diff --git a/app/controllers/transfers_controller.rb b/app/controllers/transfers_controller.rb index 8d4f4f432..7c8a958ee 100644 --- a/app/controllers/transfers_controller.rb +++ b/app/controllers/transfers_controller.rb @@ -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 diff --git a/app/mailers/payment_notifier.rb b/app/mailers/payment_notifier.rb new file mode 100644 index 000000000..8e121017e --- /dev/null +++ b/app/mailers/payment_notifier.rb @@ -0,0 +1,16 @@ +class PaymentNotifier < ActionMailer::Base + default from: "\"TimeOverflow\" " + + # @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 \ No newline at end of file diff --git a/app/services/transfer_creator.rb b/app/services/transfer_creator.rb new file mode 100644 index 000000000..aad5d1edb --- /dev/null +++ b/app/services/transfer_creator.rb @@ -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 \ No newline at end of file diff --git a/config/locales/email.ca.yml b/config/locales/email.ca.yml index 10cdc1b21..815469e07 100644 --- a/config/locales/email.ca.yml +++ b/config/locales/email.ca.yml @@ -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:" \ No newline at end of file + 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}" \ No newline at end of file diff --git a/config/locales/email.en.yml b/config/locales/email.en.yml index 76bd2a486..7d6aa3ee8 100644 --- a/config/locales/email.en.yml +++ b/config/locales/email.en.yml @@ -7,4 +7,10 @@ en: recent_posts: subject: Newsletter text1: "Latest offers published:" - text2: "Lastest inquiries published:" \ No newline at end of file + 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}" \ No newline at end of file diff --git a/config/locales/email.es.yml b/config/locales/email.es.yml index 5bcc85d4a..c6e5a8910 100644 --- a/config/locales/email.es.yml +++ b/config/locales/email.es.yml @@ -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}" \ No newline at end of file diff --git a/lib/time_formatter.rb b/lib/time_formatter.rb new file mode 100644 index 000000000..8944fe9b4 --- /dev/null +++ b/lib/time_formatter.rb @@ -0,0 +1,16 @@ +class TimeFormatter + def mdash + raw "—" + 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 + end + end +end diff --git a/spec/services/transfer_creator_spec.rb b/spec/services/transfer_creator_spec.rb new file mode 100644 index 000000000..97509ce22 --- /dev/null +++ b/spec/services/transfer_creator_spec.rb @@ -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 \ No newline at end of file