Skip to content

Commit

Permalink
feat: time can now be spent on an offer from another organization
Browse files Browse the repository at this point in the history
To do this, 3 transfers are made: member1 --> organization1; organization1 --> organization2; organization2 --> member2. In order to reuse the implemented code and not make it unnecessarily long, it was necessary to make some modifications to the implemented code. Issue #638.
  • Loading branch information
franpb14 committed May 15, 2021
1 parent 059bbcb commit 11048ec
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/offers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def model
def show
super

member = @offer.user.members.find_by(organization: current_organization)
member = @offer.user.members.find_by(organization: @offer.organization)
@destination_account = member.account if member
end
end
40 changes: 31 additions & 9 deletions app/controllers/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ def create
@source = find_source
@account = Account.find(transfer_params[:destination])

transfer = Transfer.new(
transfer_params.merge(source: @source, destination: @account)
)

persister = ::Persister::TransferPersister.new(transfer)
create_persisters

if persister.save
if persisters_saved?
redirect_to redirect_target
else
redirect_back fallback_location: redirect_target, alert: transfer.errors.full_messages.to_sentence
redirect_back fallback_location: redirect_target, alert: @transfer.errors.full_messages.to_sentence
end
end

Expand All @@ -23,7 +19,8 @@ def new
current_organization,
current_user,
params[:offer],
params[:destination_account_id]
params[:destination_account_id],
params[:organization_id] || current_organization.id
)

render(
Expand Down Expand Up @@ -57,12 +54,37 @@ def find_source
end
end

def create_persisters
source_organization = @source.organization.account
destination_organization = @account.organization.account

if source_organization == destination_organization
@persister = transfer_persister_between(@source, @account)
else
@pers_src_org = transfer_persister_between(@source, source_organization)
@pers_between_orgs = transfer_persister_between(source_organization, destination_organization)
@pers_org_acc = transfer_persister_between(destination_organization, @account)
end
end

def transfer_persister_between(source, destination)
@transfer = Transfer.new(
transfer_params.merge(source: source, destination: destination)
)

::Persister::TransferPersister.new(@transfer)
end

def persisters_saved?
@persister&.save || @pers_src_org&.save && @pers_between_orgs&.save && @pers_org_acc&.save
end

def redirect_target
case accountable = @account.accountable
when Organization
accountable
when Member
accountable.user
accountable.organization == current_organization ? accountable.user : @source.accountable.user
else
raise ArgumentError
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def next_reg_number_seq
reg_number_seq
end

def global_balance
Account.where(organization_id: id).sum(:balance)
end

def ensure_url
return if web.blank? || URI.parse(web).is_a?(URI::HTTP)
rescue
Expand Down
12 changes: 9 additions & 3 deletions app/models/transfer_factory.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
class TransferFactory
def initialize(current_organization, current_user, offer_id, destination_account_id)
def initialize(current_organization, current_user, offer_id, destination_account_id,
destination_organization_id)
@current_organization = current_organization
@current_user = current_user
@offer_id = offer_id
@destination_account_id = destination_account_id
@destination_organization_id = destination_organization_id.to_i
end

def destination_organization
Organization.find(@destination_organization_id)
end

# Returns the offer that is the subject of the transfer
#
# @return [Maybe<Offer>]
def offer
current_organization.offers.find_by_id(offer_id)
destination_organization.offers.find_by_id(offer_id)
end

# Returns a new instance of Transfer with the data provided
Expand Down Expand Up @@ -73,7 +79,7 @@ def admin?
#
# @return [Account]
def destination_account
@destination_account ||= current_organization
@destination_account ||= destination_organization
.all_accounts
.find(destination_account_id)
end
Expand Down
11 changes: 11 additions & 0 deletions app/views/offers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,16 @@
<% end %>
<% end %>
</p>
<% else %>
<p class="actions text-right">
<% if current_user and @offer.user != current_user %>
<%= link_to new_transfer_path(id: @offer.user.id, offer: @offer.id, destination_account_id: @destination_account.id,
organization_id: @offer.organization.id),
class: "btn btn-success" do %>
<%= glyph :time %>
<%= t ".give_time_for" %>
<% end %>
<% end %>
</p>
<% end %>
<%= render "shared/post", post: @offer %>
5 changes: 5 additions & 0 deletions app/views/organizations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
<%= t 'global.balance' %>
</strong>
<%= seconds_to_hm(@organization.account.try(:balance)) %>
<br/>
<strong>
<%= t 'global.global_balance' %>
</strong>
<%= seconds_to_hm(@organization.try(:global_balance)) %>
</p>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ en:
filter: Filter
from: From
give_time: Time transfer
global_balance: Global balance
home: Home
information: Information
locales_header: change language
Expand Down

0 comments on commit 11048ec

Please sign in to comment.