Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
create account user update service
KacperMekarski3d26ee46728b
diff --git a/core/app/services/spree/account/users/update.rb b/core/app/services/spree/account/users/update.rbnew file mode 100644index 0000000000..a588872720--- /dev/null+++ b/core/app/services/spree/account/users/update.rb@@ -0,0 +1,17 @@+module Spree+ module Account+ module Users+ class Update+ prepend Spree::ServiceModule::Base++ def call(user:, user_params:)+ if user.update(user_params)+ success(user)+ else+ failure(user)+ end+ end+ end+ end+ end+enddiff --git a/core/spec/services/spree/account/users/update_spec.rb b/core/spec/services/spree/account/users/update_spec.rbnew file mode 100644index 0000000000..a06c07f0be--- /dev/null+++ b/core/spec/services/spree/account/users/update_spec.rb@@ -0,0 +1,127 @@+require 'spec_helper'++module Spree+ describe Account::Users::Update do+ subject { described_class }++ let!(:user) { create(:user_with_addresses) }+ let(:result) { subject.call(user: user, user_params: user_params) }+ let!(:new_default_ship_address) { create(:address, user: user) }+ let!(:new_default_bill_address) { create(:address, user: user) }+ let(:value) { result.value }++ shared_examples 'user not created' do+ it 'does not create new user' do+ expect { result }.not_to change(Spree.user_class, :count)+ end+ end++ shared_examples 'successful response' do+ it 'result is successful' do+ expect(result).to be_success+ end+ end++ shared_examples 'updated attributes' do+ it 'updates user with given params' do+ expect(value).to have_attributes(user_params)+ end+ end++ shared_examples 'both default addresses changed' do+ it 'changes both defauly bill address and ship address' do+ expect(user.bill_address).not_to eq(new_default_bill_address)+ expect(user.ship_address).not_to eq(new_default_ship_address)++ expect(value.bill_address_id).to eq(new_default_bill_address.id)+ expect(value.ship_address_id).to eq(new_default_ship_address.id)+ end+ end++ describe '#call' do+ context 'with valid params' do+ context 'when all params are given' do+ let(:user_params) do+ {+ email: '[email protected]',+ bill_address_id: new_default_bill_address.id,+ ship_address_id: new_default_ship_address.id+ }+ end++ it_should_behave_like 'user not created'+ it_should_behave_like 'successful response'+ it_should_behave_like 'updated attributes'+ it_should_behave_like 'both default addresses changed'+ end++ context 'when only address params are given' do+ let(:user_params) do+ {+ bill_address_id: new_default_bill_address.id,+ ship_address_id: new_default_ship_address.id+ }+ end++ it_should_behave_like 'user not created'+ it_should_behave_like 'successful response'+ it_should_behave_like 'updated attributes'+ it_should_behave_like 'both default addresses changed'++ context 'when only bill address is given' do+ let(:user_params) do+ {+ bill_address_id: new_default_bill_address.id+ }+ end++ it_should_behave_like 'user not created'+ it_should_behave_like 'successful response'+ it_should_behave_like 'updated attributes'++ it 'changes only user default bill address' do+ expect(user.bill_address).not_to eq(new_default_bill_address)+ expect(user.ship_address).not_to eq(new_default_ship_address)++ expect(value.bill_address_id).to eq(new_default_bill_address.id)+ expect { result }.not_to change(value, :ship_address_id)+ end+ end++ context 'when only ship address is given' do+ let(:user_params) do+ {+ ship_address_id: new_default_ship_address.id+ }+ end++ it_should_behave_like 'user not created'+ it_should_behave_like 'successful response'+ it_should_behave_like 'updated attributes'++ it 'changes only user default ship address' do+ expect(user.bill_address).not_to eq(new_default_bill_address)+ expect(user.ship_address).not_to eq(new_default_ship_address)++ expect(value.ship_address_id).to eq(new_default_ship_address.id)+ expect { result }.not_to change(value, :bill_address_id)+ end+ end+ end++ context 'when only address params are given' do+ let(:user_params) do+ {}+ end++ it_should_behave_like 'user not created'+ it_should_behave_like 'successful response'++ it 'does not change user data at all' do+ expect(value).to have_attributes(user.attributes)+ end+ end+ end+ end+ end+end