From d55098f94fcb28ebb0851d82267c9e48bb16ea5b Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 10 Jul 2023 11:48:59 +0200 Subject: [PATCH] Add the number of orders for the customer --- lib/reporting/reports/customers/addresses.rb | 1 + spec/lib/reports/customers_report_spec.rb | 18 +++++++++--------- spec/system/admin/reports_spec.rb | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/reporting/reports/customers/addresses.rb b/lib/reporting/reports/customers/addresses.rb index 964de8ce5c..1790613e4c 100644 --- a/lib/reporting/reports/customers/addresses.rb +++ b/lib/reporting/reports/customers/addresses.rb @@ -29,6 +29,7 @@ module Reporting hub: proc { |orders| orders.first.distributor&.name }, hub_address: proc { |orders| orders.first.distributor&.address&.address_and_city }, shipping_method: proc { |orders| orders.first.shipping_method&.name }, + total_orders: proc { |orders| orders.count }, } end # rubocop:enable Metrics/AbcSize diff --git a/spec/lib/reports/customers_report_spec.rb b/spec/lib/reports/customers_report_spec.rb index fdd7926328..e49b10c267 100644 --- a/spec/lib/reports/customers_report_spec.rb +++ b/spec/lib/reports/customers_report_spec.rb @@ -65,7 +65,7 @@ module Reporting it "returns headers for addresses" do expect(subject.table_headers).to eq(["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", - "Shipping Method"]) + "Shipping Method", "Total Number of Orders"]) end it "builds a table from a list of variants" do @@ -81,7 +81,7 @@ module Reporting o.email, a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - o.shipping_method.name + o.shipping_method.name, 1 ]]) end @@ -105,7 +105,7 @@ module Reporting end end - it "returns only one row per customer" do + it "returns only one row per customer and count the number of orders" do expect(subject.query_result).to match_array [[o1, o2]] expect(subject.table_rows.size).to eq(1) expect(subject.table_rows) @@ -114,7 +114,7 @@ module Reporting [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 + o1.shipping_method.name, 2 ]]) end @@ -136,13 +136,13 @@ module Reporting [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 + o1.shipping_method.name, 1 ], [ 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 + o2.shipping_method.name, 1 ]]) end end @@ -161,7 +161,7 @@ module Reporting context "when the shipping method column is being included" do let(:fields_to_show) do [:first_name, :last_name, :billing_address, :email, :phone, :hub, :hub_address, - :shipping_method] + :shipping_method, :total_orders] end subject { Addresses.new(user, { fields_to_show: }) } @@ -178,7 +178,7 @@ module Reporting a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - o1.shipping_method.name + o1.shipping_method.name, 1 ], [ a.firstname, @@ -188,7 +188,7 @@ module Reporting a.phone, d.name, [d.address.address1, d.address.address2, d.address.city].join(" "), - sm2.name + sm2.name, 1 ] ] ) diff --git a/spec/system/admin/reports_spec.rb b/spec/system/admin/reports_spec.rb index e5c3b6863c..174974d885 100644 --- a/spec/system/admin/reports_spec.rb +++ b/spec/system/admin/reports_spec.rb @@ -173,7 +173,7 @@ describe ' table = rows.map { |r| r.all("th").map { |c| c.text.strip } } expect(table.sort).to eq([ ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", - "Shipping Method"].map(&:upcase) + "Shipping Method", "Total Number of Orders"].map(&:upcase) ].sort) end end