From fbe0e2cc29cc4a5ead43daf1fc607941c7ce85e4 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 22 Nov 2013 16:22:44 +1100 Subject: [PATCH] Basic plumbing for the Customers in place --- app/models/spree/ability_decorator.rb | 2 +- lib/open_food_network/customers_report.rb | 51 ++++++++++ .../customers_report_spec.rb | 96 +++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 lib/open_food_network/customers_report.rb create mode 100644 spec/lib/open_food_network/customers_report_spec.rb diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 44242b5909..ed018c4fee 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -66,7 +66,7 @@ class AbilityDecorator end # Enterprise User can access reports page - can [:admin, :index, :orders_and_distributors, :group_buys, :bulk_coop, :payments, :orders_and_fulfillment, :products_and_inventory], :report + can [:admin, :index, :customers, :orders_and_distributors, :group_buys, :bulk_coop, :payments, :orders_and_fulfillment, :products_and_inventory], :report end end end diff --git a/lib/open_food_network/customers_report.rb b/lib/open_food_network/customers_report.rb new file mode 100644 index 0000000000..61c2d1dcc3 --- /dev/null +++ b/lib/open_food_network/customers_report.rb @@ -0,0 +1,51 @@ +module OpenFoodNetwork + class CustomersReport + attr_reader :params + def initialize(user, params = {}) + @params = params + @user = user + end + + def header + if is_mailing_list? + ["Email", "First Name", "Last Name", "Suburb"] + else + ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", "Shipping Method"] + end + end + + def table + orders.map do |order| + if is_mailing_list? + [order.email, + order.billing_address.firstname, + order.billing_address.lastname, + order.billing_address.city] + else + ba = order.billing_address + da = order.distributor.address + [ba.firstname, + ba.lastname, + [ba.address1, ba.address2, ba.city].join(" "), + order.email, + ba.phone, + order.distributor.name, + [da.address1, da.address2, da.city].join(" "), + order.shipping_method.name + ] + end + end + end + + def orders + Spree::Order.managed_by(@user).complete.not_state(:canceled) + end + + private + + def is_mailing_list? + params[:report_type] == "mailing_list" + end + end +end + diff --git a/spec/lib/open_food_network/customers_report_spec.rb b/spec/lib/open_food_network/customers_report_spec.rb new file mode 100644 index 0000000000..161835c806 --- /dev/null +++ b/spec/lib/open_food_network/customers_report_spec.rb @@ -0,0 +1,96 @@ +require 'spec_helper' + +module OpenFoodNetwork + describe CustomersReport do + context "As a site admin" do + let(:user) do + user = create(:user) + user.spree_roles << Spree::Role.find_or_create_by_name!("admin") + user + end + subject { CustomersReport.new user } + + context "a mailing list" do + before do + subject.stub(:params).and_return(report_type: "mailing_list") + end + + it "returns headers for mailing_list" do + subject.header.should == ["Email", "First Name", "Last Name", "Suburb"] + end + + it "should build a table from a list of variants" do + order = double(:order, email: "test@test.com") + address = double(:billing_address, firstname: "Firsty", + lastname: "Lasty", city: "Suburbia") + order.stub(:billing_address).and_return address + subject.stub(:orders).and_return [order] + + subject.table.should == [[ + "test@test.com", "Firsty", "Lasty", "Suburbia" + ]] + end + end + + context "an addresses report" do + before do + subject.stub(:params).and_return(report_type: "addresses") + end + it "returns headers for addresses" do + subject.header.should == ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", "Shipping Method"] + end + + it "should build a table from a list of variants" do + a = create(:address) + d = create(:distributor_enterprise) + o = create(:order, distributor: d, bill_address: a) + o.shipping_method = create(:shipping_method) + + subject.stub(:orders).and_return [o] + subject.table.should == [[ + a.firstname, a.lastname, + [a.address1, a.address2, a.city].join(" "), + o.email, a.phone, d.name, + [d.address.address1, d.address.address2, d.address.city].join(" "), + o.shipping_method.name + ]] + end + end + + describe "Fetching orders" do + it "should fetch complete orders" do + o1 = create(:order) + o2 = create(:order, completed_at: 1.day.ago) + subject.orders.should == [o2] + end + it "not show cancelled orders" do + o1 = create(:order, state: "canceled", completed_at: 1.day.ago) + o2 = create(:order, completed_at: 1.day.ago) + subject.orders.should == [o2] + end + end + end + context "As an enterprise user" do + let(:user) do + user = create(:user) + user.spree_roles = [] + user.save! + user + end + subject { CustomersReport.new user } + describe "Fetching orders" do + it "should only show orders managed by the current user" do + d1 = create(:distributor_enterprise) + d1.enterprise_roles.build(user: user).save + d2 = create(:distributor_enterprise) + d2.enterprise_roles.build(user: create(:user)).save + + o1 = create(:order, distributor: d1, completed_at: 1.day.ago) + o2 = create(:order, distributor: d2, completed_at: 1.day.ago) + + subject.orders.should == [o1] + end + end + end + end +end