mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #12964 from chahmedejaz/task/12890-add-data-to-dfc-affiliate-sales-endpoint
Add cities and countries to DFC affiliate sales data endpoint
This commit is contained in:
@@ -12,7 +12,10 @@ class AffiliateSalesDataRowBuilder < DfcBuilder
|
||||
def build_supplier
|
||||
DataFoodConsortium::Connector::Enterprise.new(
|
||||
nil,
|
||||
localizations: [build_address(item[:supplier_postcode])],
|
||||
localizations: [build_address(
|
||||
item[:supplier_postcode],
|
||||
item[:supplier_country]
|
||||
)],
|
||||
suppliedProducts: [build_product],
|
||||
)
|
||||
end
|
||||
@@ -20,7 +23,10 @@ class AffiliateSalesDataRowBuilder < DfcBuilder
|
||||
def build_distributor
|
||||
DataFoodConsortium::Connector::Enterprise.new(
|
||||
nil,
|
||||
localizations: [build_address(item[:distributor_postcode])],
|
||||
localizations: [build_address(
|
||||
item[:distributor_postcode],
|
||||
item[:distributor_country]
|
||||
)],
|
||||
)
|
||||
end
|
||||
|
||||
@@ -89,9 +95,10 @@ class AffiliateSalesDataRowBuilder < DfcBuilder
|
||||
)
|
||||
end
|
||||
|
||||
def build_address(postcode)
|
||||
def build_address(postcode, country)
|
||||
DataFoodConsortium::Connector::Address.new(
|
||||
nil,
|
||||
country:,
|
||||
postalCode: postcode,
|
||||
)
|
||||
end
|
||||
|
||||
@@ -38,9 +38,11 @@ class AffiliateSalesQuery
|
||||
JOIN spree_products ON spree_products.id = spree_variants.product_id
|
||||
JOIN enterprises AS suppliers ON suppliers.id = spree_variants.supplier_id
|
||||
JOIN spree_addresses AS supplier_addresses ON supplier_addresses.id = suppliers.address_id
|
||||
JOIN spree_countries AS supplier_countries ON supplier_countries.id = supplier_addresses.country_id
|
||||
JOIN spree_orders ON spree_orders.id = spree_line_items.order_id
|
||||
JOIN enterprises AS distributors ON distributors.id = spree_orders.distributor_id
|
||||
JOIN spree_addresses AS distributor_addresses ON distributor_addresses.id = distributors.address_id
|
||||
JOIN spree_countries AS distributor_countries ON distributor_countries.id = distributor_addresses.country_id
|
||||
SQL
|
||||
end
|
||||
|
||||
@@ -53,7 +55,9 @@ class AffiliateSalesQuery
|
||||
spree_variants.unit_presentation,
|
||||
spree_line_items.price,
|
||||
distributor_addresses.zipcode AS distributor_postcode,
|
||||
distributor_countries.name AS distributor_country,
|
||||
supplier_addresses.zipcode AS supplier_postcode,
|
||||
supplier_countries.name AS supplier_country,
|
||||
|
||||
SUM(spree_line_items.quantity) AS quantity_sold
|
||||
SQL
|
||||
@@ -68,7 +72,9 @@ class AffiliateSalesQuery
|
||||
spree_variants.unit_presentation,
|
||||
spree_line_items.price,
|
||||
distributor_postcode,
|
||||
supplier_postcode
|
||||
supplier_postcode,
|
||||
distributor_country,
|
||||
supplier_country
|
||||
SQL
|
||||
end
|
||||
|
||||
@@ -82,7 +88,9 @@ class AffiliateSalesQuery
|
||||
unit_presentation
|
||||
price
|
||||
distributor_postcode
|
||||
distributor_country
|
||||
supplier_postcode
|
||||
supplier_country
|
||||
quantity_sold
|
||||
]
|
||||
end
|
||||
|
||||
@@ -53,9 +53,15 @@ RSpec.describe AffiliateSalesQuery do
|
||||
it "converts an array to a hash" do
|
||||
row = [
|
||||
"Apples",
|
||||
"item", "item", nil, nil,
|
||||
"item",
|
||||
"item",
|
||||
nil,
|
||||
nil,
|
||||
15.50,
|
||||
"3210", "3211",
|
||||
"3210",
|
||||
"country1",
|
||||
"3211",
|
||||
"country2",
|
||||
3,
|
||||
]
|
||||
expect(query.label_row(row)).to eq(
|
||||
@@ -67,7 +73,9 @@ RSpec.describe AffiliateSalesQuery do
|
||||
unit_presentation: nil,
|
||||
price: 15.50,
|
||||
distributor_postcode: "3210",
|
||||
distributor_country: "country1",
|
||||
supplier_postcode: "3211",
|
||||
supplier_country: "country2",
|
||||
quantity_sold: 3,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -34,6 +34,8 @@ RSpec.describe "Pay Your Suppliers Report" do
|
||||
before do
|
||||
login_as owner
|
||||
visit admin_reports_path
|
||||
|
||||
update_line_items_product_names
|
||||
end
|
||||
|
||||
context "on Reports page" do
|
||||
@@ -138,4 +140,18 @@ RSpec.describe "Pay Your Suppliers Report" do
|
||||
expect(lines.last).to have_content("TOTAL 50.0 50.0 0.0 0.0 0.0 50.0")
|
||||
end
|
||||
end
|
||||
|
||||
def update_line_items_product_names
|
||||
n = 1
|
||||
update_product_name_proc = proc do |order|
|
||||
order.line_items.each do |line_item|
|
||||
product = line_item.variant.product
|
||||
product.update!(name: "Product##{n}")
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
|
||||
update_product_name_proc.call(order1)
|
||||
update_product_name_proc.call(order2)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -97,12 +97,13 @@ paths:
|
||||
dfc-b:hasAddress:
|
||||
"@type": dfc-b:Address
|
||||
dfc-b:hasPostalCode: '20170'
|
||||
dfc-b:hasCountry: Australia
|
||||
dfc-b:supplies:
|
||||
"@type": dfc-b:SuppliedProduct
|
||||
dfc-b:name: Tomato
|
||||
dfc-b:hasQuantity:
|
||||
"@type": dfc-b:QuantitativeValue
|
||||
dfc-b:hasUnit: dfc-m:Gram
|
||||
dfc-b:hasUnit: dfc-m:Piece
|
||||
dfc-b:value: 1.0
|
||||
dfc-b:concernedBy:
|
||||
"@type": dfc-b:OrderLine
|
||||
@@ -124,6 +125,7 @@ paths:
|
||||
dfc-b:hasAddress:
|
||||
"@type": dfc-b:Address
|
||||
dfc-b:hasPostalCode: '20170'
|
||||
dfc-b:hasCountry: Australia
|
||||
'400':
|
||||
description: bad request
|
||||
"/api/dfc/enterprises/{enterprise_id}/catalog_items":
|
||||
|
||||
Reference in New Issue
Block a user