Skip to content

Commit

Permalink
Merge pull request #12964 from chahmedejaz/task/12890-add-data-to-dfc…
Browse files Browse the repository at this point in the history
…-affiliate-sales-endpoint

Add cities and countries to DFC affiliate sales data endpoint
  • Loading branch information
RachL authored Nov 28, 2024
2 parents 3a7aed1 + c0887b1 commit 479eacc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
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
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

0 comments on commit 479eacc

Please sign in to comment.