mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-15 23:57:48 +00:00
Associate new users with existing customer records
This commit is contained in:
@@ -15,6 +15,7 @@ Spree.user_class.class_eval do
|
||||
accepts_nested_attributes_for :enterprise_roles, :allow_destroy => true
|
||||
|
||||
attr_accessible :enterprise_ids, :enterprise_roles_attributes, :enterprise_limit
|
||||
after_create :associate_customers
|
||||
after_create :send_signup_confirmation
|
||||
|
||||
validate :limit_owned_enterprises
|
||||
@@ -41,6 +42,10 @@ Spree.user_class.class_eval do
|
||||
customers.of(enterprise).first
|
||||
end
|
||||
|
||||
def associate_customers
|
||||
Customer.update_all({ user_id: id }, { user_id: nil, email: email })
|
||||
end
|
||||
|
||||
def send_signup_confirmation
|
||||
Delayed::Job.enqueue ConfirmSignupJob.new(id)
|
||||
end
|
||||
|
||||
@@ -17,11 +17,11 @@ describe Spree.user_class do
|
||||
it "enforces the limit on the number of enterprise owned" do
|
||||
expect(u2.owned_enterprises(:reload)).to eq []
|
||||
u2.owned_enterprises << e1
|
||||
expect(u2.save!).to_not raise_error
|
||||
expect {
|
||||
expect { u2.save! }.to_not raise_error
|
||||
expect do
|
||||
u2.owned_enterprises << e2
|
||||
u2.save!
|
||||
}.to raise_error ActiveRecord::RecordInvalid, "Validation failed: #{u2.email} is not permitted to own any more enterprises (limit is 1)."
|
||||
end.to raise_error ActiveRecord::RecordInvalid, "Validation failed: #{u2.email} is not permitted to own any more enterprises (limit is 1)."
|
||||
end
|
||||
end
|
||||
|
||||
@@ -53,6 +53,23 @@ describe Spree.user_class do
|
||||
create(:user)
|
||||
end.to enqueue_job ConfirmSignupJob
|
||||
end
|
||||
|
||||
it "should not create a customer" do
|
||||
expect do
|
||||
create(:user)
|
||||
end.to change(Customer, :count).by(0)
|
||||
end
|
||||
|
||||
describe "when a customer record exists" do
|
||||
let!(:customer) { create(:customer, user: nil) }
|
||||
|
||||
it "should not create a customer" do
|
||||
expect(customer.user).to be nil
|
||||
user = create(:user, email: customer.email)
|
||||
customer.reload
|
||||
expect(customer.user).to eq user
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "known_users" do
|
||||
@@ -65,9 +82,9 @@ describe Spree.user_class do
|
||||
it "returns a list of users which manage shared enterprises" do
|
||||
expect(u1.known_users).to include u1, u2
|
||||
expect(u1.known_users).to_not include u3
|
||||
expect(u2.known_users).to include u1,u2
|
||||
expect(u2.known_users).to include u1, u2
|
||||
expect(u2.known_users).to_not include u3
|
||||
expect(u3.known_users).to_not include u1,u2,u3
|
||||
expect(u3.known_users).to_not include u1, u2, u3
|
||||
end
|
||||
end
|
||||
|
||||
@@ -85,14 +102,14 @@ describe Spree.user_class do
|
||||
let!(:u2) { create(:user) }
|
||||
let!(:distributor1) { create(:distributor_enterprise) }
|
||||
let!(:distributor2) { create(:distributor_enterprise) }
|
||||
let!(:d1o1) { create(:completed_order_with_totals, distributor: distributor1, user_id: u1.id)}
|
||||
let!(:d1o2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u1.id)}
|
||||
let!(:d1_order_for_u2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u2.id)}
|
||||
let!(:d1o3) { create(:order, state: 'cart', distributor: distributor1, user_id: u1.id)}
|
||||
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: u2.id)}
|
||||
let!(:d1o1) { create(:completed_order_with_totals, distributor: distributor1, user_id: u1.id) }
|
||||
let!(:d1o2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u1.id) }
|
||||
let!(:d1_order_for_u2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u2.id) }
|
||||
let!(:d1o3) { create(:order, state: 'cart', distributor: distributor1, user_id: u1.id) }
|
||||
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: u2.id) }
|
||||
|
||||
let!(:completed_payment) { create(:payment, order: d1o1, state: 'completed')}
|
||||
let!(:payment) { create(:payment, order: d1o2, state: 'invalid')}
|
||||
let!(:completed_payment) { create(:payment, order: d1o1, state: 'completed') }
|
||||
let!(:payment) { create(:payment, order: d1o2, state: 'invalid') }
|
||||
|
||||
it "returns enterprises that the user has ordered from" do
|
||||
expect(u1.enterprises_ordered_from).to eq [distributor1.id]
|
||||
@@ -115,7 +132,7 @@ describe Spree.user_class do
|
||||
end
|
||||
|
||||
it "doesn't return uncompleted payments" do
|
||||
expect(u1.orders_by_distributor.first.distributed_orders.map{|o| o.payments}.flatten).not_to include payment
|
||||
expect(u1.orders_by_distributor.first.distributed_orders.map(&:payments).flatten).not_to include payment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user