mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Data manipulation and balance calc in rails instead
This commit is contained in:
@@ -56,6 +56,13 @@ module InjectionHelper
|
||||
inject_json_ams "orders", spree_current_user.orders.where(state: :complete).to_a, Api::OrderSerializer
|
||||
end
|
||||
|
||||
def inject_orders_by_distributor
|
||||
#render partial: "json/injection_ams", locals: {name:"orders_by_distributor", json: spree_current_user.orders_by_distributor}
|
||||
inject_json_ams "orders_by_distributor",
|
||||
Enterprise.includes(:distributed_orders).where(id: spree_current_user.enterprises_ordered_from).to_a,
|
||||
Api::OrdersByDistributorSerializer
|
||||
end
|
||||
|
||||
def inject_json(name, partial, opts = {})
|
||||
render partial: "json/injection", locals: {name: name, partial: partial}.merge(opts)
|
||||
end
|
||||
|
||||
@@ -49,6 +49,12 @@ Spree.user_class.class_eval do
|
||||
owned_enterprises(:reload).size < enterprise_limit
|
||||
end
|
||||
|
||||
# Returns Enterprise IDs for distributors that the user has shopped at
|
||||
def enterprises_ordered_from
|
||||
self.orders.where(state: :complete).map(&:distributor_id).uniq
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def limit_owned_enterprises
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
class Api::OrderSerializer < ActiveModel::Serializer
|
||||
attributes :id, :completed_at, :total, :state, :shipment_state, :payment_state, :outstanding_balance, :total_money, :balance_money
|
||||
|
||||
has_one :distributor, serializer: Api::IdNameSerializer
|
||||
|
||||
|
||||
def completed_at
|
||||
object.completed_at.blank? ? "" : object.completed_at.strftime("%F %T")
|
||||
end
|
||||
|
||||
8
app/serializers/api/orders_by_distributor_serializer.rb
Normal file
8
app/serializers/api/orders_by_distributor_serializer.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Api::OrdersByDistributorSerializer < ActiveModel::Serializer
|
||||
attributes :name, :balance, :distributed_orders
|
||||
has_many :distributed_orders, serializer: Api::OrderSerializer
|
||||
|
||||
def balance
|
||||
object.distributed_orders.map(&:outstanding_balance).reduce(:+).to_money.to_s
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,5 @@
|
||||
= inject_orders_for_user
|
||||
= inject_orders_by_distributor
|
||||
|
||||
.row.pad-top
|
||||
.small-12.columns.pad-top
|
||||
|
||||
Reference in New Issue
Block a user