mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Merge pull request #10665 from abdellani/fix-remove-duplicated-cusomter-from-addresses-list-report
Fix Repeated customer entries on Addresses List Report
This commit is contained in:
@@ -4,6 +4,20 @@ module Reporting
|
||||
module Reports
|
||||
module Customers
|
||||
class Addresses < Base
|
||||
def query_result
|
||||
super.group_by do |order|
|
||||
{
|
||||
first_name: order.billing_address.firstname,
|
||||
last_name: order.billing_address.lastname,
|
||||
billing_address: order.billing_address.address_and_city,
|
||||
email: order.email,
|
||||
phone: order.billing_address.phone,
|
||||
hub_id: order.distributor_id,
|
||||
shipping_method_id: order.shipping_method&.id,
|
||||
}
|
||||
end.values.map(&:first)
|
||||
end
|
||||
|
||||
def columns
|
||||
{
|
||||
first_name: proc { |order| order.billing_address.firstname },
|
||||
|
||||
@@ -57,6 +57,89 @@ module Reporting
|
||||
o.shipping_method.name
|
||||
]])
|
||||
end
|
||||
|
||||
context "when there are multiple orders for the same customer" do
|
||||
let!(:a) { create(:bill_address) }
|
||||
let!(:d){ create(:distributor_enterprise) }
|
||||
let!(:sm) { create(:shipping_method, distributors: [d]) }
|
||||
let!(:o1) {
|
||||
create(:order_with_totals_and_distribution, :completed, distributor: d, bill_address: a, shipping_method: sm)
|
||||
}
|
||||
let!(:o2) {
|
||||
create(:order_with_totals_and_distribution, :completed, distributor: d, bill_address: a, shipping_method: sm)
|
||||
}
|
||||
before do
|
||||
[o1, o2].each do |order|
|
||||
order.update!(email: "test@test.com")
|
||||
end
|
||||
end
|
||||
|
||||
it "returns only one row per customer" do
|
||||
expect(subject.query_result).to match_array [o1]
|
||||
expect(subject.table_rows.size).to eq(1)
|
||||
expect(subject.table_rows).to eq([[
|
||||
a.firstname, a.lastname,
|
||||
[a.address1, a.address2, a.city].join(" "),
|
||||
o1.email, a.phone, d.name,
|
||||
[d.address.address1, d.address.address2, d.address.city].join(" "),
|
||||
o1.shipping_method.name
|
||||
]])
|
||||
end
|
||||
|
||||
context "orders from different hubs" do
|
||||
let!(:d2) { create(:distributor_enterprise) }
|
||||
let!(:sm2) { create(:shipping_method, distributors: [d2]) }
|
||||
let!(:o2) {
|
||||
create(:order_with_totals_and_distribution, :completed, distributor: d2, bill_address: a, shipping_method: sm2)
|
||||
}
|
||||
|
||||
it "returns one row per customer per hub" do
|
||||
expect(subject.query_result.size).to eq(2)
|
||||
expect(subject.table_rows.size).to eq(2)
|
||||
expect(subject.table_rows).to eq([[
|
||||
a.firstname, a.lastname,
|
||||
[a.address1, a.address2, a.city].join(" "),
|
||||
o1.email, a.phone, d.name,
|
||||
[d.address.address1, d.address.address2, d.address.city].join(" "),
|
||||
o1.shipping_method.name
|
||||
], [
|
||||
a.firstname, a.lastname,
|
||||
[a.address1, a.address2, a.city].join(" "),
|
||||
o2.email, a.phone, d2.name,
|
||||
[d2.address.address1, d2.address.address2, d2.address.city].join(" "),
|
||||
o2.shipping_method.name
|
||||
]])
|
||||
end
|
||||
end
|
||||
|
||||
context "orders with different shipping methods" do
|
||||
let!(:sm2) { create(:shipping_method, distributors: [d]) }
|
||||
let!(:o2) {
|
||||
create(:order_with_totals_and_distribution, :completed, distributor: d, bill_address: a, shipping_method: sm2)
|
||||
}
|
||||
before do
|
||||
o2.select_shipping_method(sm2.id)
|
||||
end
|
||||
|
||||
it "returns one row per customer per shipping method" do
|
||||
expect(subject.query_result.size).to eq(2)
|
||||
expect(subject.table_rows.size).to eq(2)
|
||||
expect(subject.table_rows).to eq([[
|
||||
a.firstname, a.lastname,
|
||||
[a.address1, a.address2, a.city].join(" "),
|
||||
o1.email, a.phone, d.name,
|
||||
[d.address.address1, d.address.address2, d.address.city].join(" "),
|
||||
o1.shipping_method.name
|
||||
], [
|
||||
a.firstname, a.lastname,
|
||||
[a.address1, a.address2, a.city].join(" "),
|
||||
o2.email, a.phone, d.name,
|
||||
[d.address.address1, d.address.address2, d.address.city].join(" "),
|
||||
sm2.name
|
||||
]])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "fetching orders" do
|
||||
|
||||
Reference in New Issue
Block a user