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

Add cities and countries to DFC affiliate sales data endpoint #12964

Open
wants to merge 5 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ def initialize(row)
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

def build_distributor
DataFoodConsortium::Connector::Enterprise.new(
nil,
localizations: [build_address(item[:distributor_postcode])],
localizations: [build_address(
item[:distributor_postcode],
item[:distributor_country]
)],
)
end

Expand Down Expand Up @@ -89,9 +95,10 @@ def build_price
)
end

def build_address(postcode)
def build_address(postcode, country)
DataFoodConsortium::Connector::Address.new(
nil,
country:,
postalCode: postcode,
)
end
Expand Down
10 changes: 9 additions & 1 deletion engines/dfc_provider/app/services/affiliate_sales_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ def tables
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

Expand All @@ -53,7 +55,9 @@ def fields
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
Expand All @@ -68,7 +72,9 @@ def key_fields
spree_variants.unit_presentation,
spree_line_items.price,
distributor_postcode,
supplier_postcode
supplier_postcode,
distributor_country,
supplier_country
SQL
end

Expand All @@ -82,7 +88,9 @@ def labels
unit_presentation
price
distributor_postcode
distributor_country
supplier_postcode
supplier_country
quantity_sold
]
end
Expand Down
12 changes: 10 additions & 2 deletions engines/dfc_provider/spec/services/affiliate_sales_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@
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(
Expand All @@ -67,7 +73,9 @@
unit_presentation: nil,
price: 15.50,
distributor_postcode: "3210",
distributor_country: "country1",
supplier_postcode: "3211",
supplier_country: "country2",
quantity_sold: 3,
}
)
Expand Down
16 changes: 16 additions & 0 deletions spec/system/admin/reports/pay_your_suppliers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
before do
login_as owner
visit admin_reports_path

update_line_items_product_names
end

context "on Reports page" do
Expand Down Expand Up @@ -138,4 +140,18 @@
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though these specs are unrelated to this PR's changes, rebasing with the latest master does cause these spec failures.

The reason was that somehow, product names were different in the report and the actual order line item variant product.
In the line_item factory, these products are created with random names. So, in this PR, I've updated those product names to have constant values.

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
4 changes: 3 additions & 1 deletion swagger/dfc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
Expand Down
Loading