Files
openfoodnetwork/app/services/customer_account_transactions/data_loader_service.rb
Gaetan Craig-Riou e21fadd124 Add CustomerAccountTransactions::DataLoaderService
It's used to load customer transactions related to a user and a specific
enterprise
2026-03-10 16:07:41 +11:00

29 lines
726 B
Ruby

# frozen_string_literal: false
module CustomerAccountTransactions
class DataLoaderService
attr_reader :user, :enterprise
def initialize(user:, enterprise:)
@user = user
@enterprise = enterprise
end
def customer_account_transactions
return [] if user.customers.empty?
enterprise_customer = user.customers.find_by(enterprise: )
return [] if enterprise_customer.nil?
enterprise_customer.customer_account_transactions.order(id: :desc)
end
def available_credit
return 0 if customer_account_transactions.empty?
# We are ordered by newest, so the lastest transaction is the first one
customer_account_transactions.first.balance
end
end
end