mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Move data processing to model, add payment method preload
This commit is contained in:
@@ -52,14 +52,7 @@ module InjectionHelper
|
||||
end
|
||||
|
||||
def inject_orders_by_distributor
|
||||
# Convert ActiveRecord::Relation to array for serialization
|
||||
data_array = spree_current_user.orders_by_distributor.to_a
|
||||
data_array.each do |enterprise|
|
||||
enterprise.distributed_orders.each do |order|
|
||||
order.payments.keep_if{ |payment| payment.state == "completed" }
|
||||
end
|
||||
end
|
||||
data_array.sort!{ |a, b| b.distributed_orders.length <=> a.distributed_orders.length }
|
||||
data_array = spree_current_user.orders_by_distributor
|
||||
inject_json_ams "orders_by_distributor", data_array, Api::OrdersByDistributorSerializer
|
||||
end
|
||||
|
||||
|
||||
@@ -56,7 +56,18 @@ Spree.user_class.class_eval do
|
||||
|
||||
# Returns orders and their associated payments for all distributors that have been ordered from
|
||||
def orders_by_distributor
|
||||
Enterprise.includes(distributed_orders: :payments).where(enterprises: { id: self.enterprises_ordered_from }, spree_orders: { state: 'complete', user_id: self.id }).order('spree_orders.completed_at DESC')
|
||||
data_array = Enterprise.includes(distributed_orders: {payments: :payment_method})
|
||||
.where(enterprises: { id: self.enterprises_ordered_from },
|
||||
spree_orders: { state: 'complete', user_id: self.id}
|
||||
).order('spree_orders.completed_at DESC')
|
||||
.to_a
|
||||
# Remove uncompleted payments as these will not be reflected in order balance
|
||||
data_array.each do |enterprise|
|
||||
enterprise.distributed_orders.each do |order|
|
||||
order.payments.keep_if{ |payment| payment.state == "completed" }
|
||||
end
|
||||
end
|
||||
data_array.sort!{ |a, b| b.distributed_orders.length <=> a.distributed_orders.length }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user