Skip to content
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

Time exchanges can now be made between organisations #639

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
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
41 changes: 32 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,38 @@ def find_source
end
end

def create_persisters
source_organization = @source.organization.account
source_type = @source.accountable_type
destination_organization = @account.organization.account
@persisters = Array.new
if source_organization == destination_organization
transfer_persister_between(@source, @account)
else
transfer_persister_between(@source, source_organization) if source_type == "Member"
transfer_persister_between(source_organization, destination_organization)
transfer_persister_between(destination_organization, @account)
end
end

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

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

def persisters_saved?
@persisters.each { |persister| return false if !persister.save }
end

def redirect_target
case accountable = @account.accountable
when Organization
accountable
when Member
accountable.user
accountable.organization == current_organization ? accountable.user : accountable.organization
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)
markets marked this conversation as resolved.
Show resolved Hide resolved
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
Loading