From 5201e54f826f1a7e584256810f216182a2fbe5e0 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 6 Apr 2023 05:52:15 +0100 Subject: [PATCH] group orders before rendering the report --- .../reports/customers/mailing_list.rb | 11 ++++++++ spec/lib/reports/customers_report_spec.rb | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/reporting/reports/customers/mailing_list.rb b/lib/reporting/reports/customers/mailing_list.rb index 492a0abb4d..b334ce93e0 100644 --- a/lib/reporting/reports/customers/mailing_list.rb +++ b/lib/reporting/reports/customers/mailing_list.rb @@ -4,6 +4,17 @@ module Reporting module Reports module Customers class MailingList < Base + def query_result + super.group_by do |order| + { + email: order.email, + first_name: order.billing_address.firstname, + last_name: order.billing_address.lastname, + suburb: order.billing_address.city, + } + end.values.map(&:first) + end + def columns { email: proc { |order| order.email }, diff --git a/spec/lib/reports/customers_report_spec.rb b/spec/lib/reports/customers_report_spec.rb index f9a1c1a77e..a90c41e80a 100644 --- a/spec/lib/reports/customers_report_spec.rb +++ b/spec/lib/reports/customers_report_spec.rb @@ -32,6 +32,31 @@ module Reporting "test@test.com", "Firsty", "Lasty", "Suburbia" ]]) end + + context "when there are multiple orders for the same customer" do + let!(:address) { + create(:bill_address, firstname: "Firsty", + lastname: "Lasty", city: "Suburbia") + } + let!(:order1) { + create(:order_with_totals_and_distribution, :completed, bill_address: address) + } + let!(:order2) { + create(:order_with_totals_and_distribution, :completed, bill_address: address) + } + before do + [order1, order2].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 [order1] + expect(subject.table_rows.size).to eq(1) + expect(subject.table_rows).to eq([[ + "test@test.com", "Firsty", "Lasty", "Suburbia" + ]]) + end + end end describe "addresses report" do