Skip to content

armgit / spree

Public Imported from GitHub
0

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.

VEN-109 Add Names to Users and in APIs (#11633)

Aleksandar Petrushevd7f6477afeb7
diff --git a/api/app/serializers/spree/api/v2/platform/user_serializer.rb b/api/app/serializers/spree/api/v2/platform/user_serializer.rb
index 966f80130a..ec3f116c50 100644
--- a/api/app/serializers/spree/api/v2/platform/user_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/user_serializer.rb
@@ -5,7 +5,7 @@ module Spree
class UserSerializer < BaseSerializer
set_type :user
- attributes :email, :created_at, :updated_at, :public_metadata, :private_metadata
+ attributes :email, :first_name, :last_name, :created_at, :updated_at, :public_metadata, :private_metadata
attribute :average_order_value do |user, params|
price_stats(user.report_values_for(:average_order_value, params[:store]))
diff --git a/api/app/serializers/spree/v2/storefront/user_serializer.rb b/api/app/serializers/spree/v2/storefront/user_serializer.rb
index 8a3a233843..67cdd3d6a3 100644
--- a/api/app/serializers/spree/v2/storefront/user_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/user_serializer.rb
@@ -4,7 +4,7 @@ module Spree
class UserSerializer < BaseSerializer
set_type :user
- attributes :email, :public_metadata
+ attributes :email, :first_name, :last_name, :public_metadata
attribute :store_credits do |user|
user.total_available_store_credit
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index c2ff55ba24..5428dde6b8 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -57,6 +57,12 @@ paths:
email:
type: string
+ first_name:
+ type: string
+ example: John
+ last_name:
+ type: string
+ example: Snow
password:
type: string
example: spree123
@@ -98,6 +104,12 @@ paths:
email:
type: string
+ first_name:
+ type: string
+ example: John
+ last_name:
+ type: string
+ example: Snow
bill_address_id:
type: string
example: '1'
@@ -3631,6 +3643,12 @@ components:
email:
type: string
+ first_name:
+ type: string
+ example: John
+ last_name:
+ type: string
+ example: Doe
store_credits:
type: number
example: 150.75
@@ -5081,6 +5099,8 @@ components:
type: user
attributes:
+ first_name: John
+ last_name: Snow
store_credits: 0
completed_orders: 0
public_metadata:
diff --git a/api/spec/requests/spree/api/v2/storefront/account_spec.rb b/api/spec/requests/spree/api/v2/storefront/account_spec.rb
index 68c60c8b49..def473ae9c 100644
--- a/api/spec/requests/spree/api/v2/storefront/account_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/account_spec.rb
@@ -31,7 +31,10 @@ describe 'Storefront API v2 Account spec', type: :request do
end
describe 'account#show' do
- before { get '/api/v2/storefront/account', headers: headers }
+ before do
+ user.reload
+ get '/api/v2/storefront/account', headers: headers
+ end
it_behaves_like 'returns 200 HTTP status'
@@ -42,6 +45,8 @@ describe 'Storefront API v2 Account spec', type: :request do
expect(json_response['data']).to have_attribute(:email).with_value(user.email)
expect(json_response['data']).to have_attribute(:store_credits).with_value(user.total_available_store_credit)
+ expect(json_response['data']).to have_attribute(:first_name).with_value(user.first_name)
+ expect(json_response['data']).to have_attribute(:last_name).with_value(user.last_name)
expect(json_response['data']).to have_attribute(:completed_orders).with_value(user.orders.complete.count)
expect(json_response['data']).to have_attribute(:public_metadata).with_value(user.public_metadata)
end
@@ -91,6 +96,8 @@ describe 'Storefront API v2 Account spec', type: :request do
password_confirmation: 'newpassword123',
bill_address_id: default_bill_address.id.to_s,
ship_address_id: default_ship_address.id.to_s,
+ first_name: 'Peter',
+ last_name: 'Parker',
public_metadata: { 'has_other_account' => 'true' },
private_metadata: { 'shops_in_other_stores' => 'false' }
}
@@ -106,6 +113,8 @@ describe 'Storefront API v2 Account spec', type: :request do
created_user = Spree.user_class.last
expect(json_response['data']['id']).to eq created_user.id.to_s
expect(json_response['data']).to have_attribute(:email).with_value(new_attributes[:email])
+ expect(json_response['data']).to have_attribute(:first_name).with_value(new_attributes[:first_name])
+ expect(json_response['data']).to have_attribute(:last_name).with_value(new_attributes[:last_name])
expect(json_response['data']).to have_attribute(:public_metadata).with_value(new_attributes[:public_metadata])
expect(created_user.private_metadata).to eq(new_attributes[:private_metadata])
expect(json_response.size).to eq(1)
@@ -156,6 +165,8 @@ describe 'Storefront API v2 Account spec', type: :request do
let(:new_attributes) do
{
+ first_name: 'Michael',
+ last_name: 'Scott',
password: 'newpassword123',
password_confirmation: 'newpassword123',
bill_address_id: new_default_bill_address.id.to_s,
@@ -176,6 +187,8 @@ describe 'Storefront API v2 Account spec', type: :request do
it 'updates and returns user' do
expect(json_response['data']).to have_id(user.id.to_s)
expect(json_response['data']).to have_attribute(:email).with_value(new_attributes[:email])
+ expect(json_response['data']).to have_attribute(:first_name).with_value(new_attributes[:first_name])
+ expect(json_response['data']).to have_attribute(:last_name).with_value(new_attributes[:last_name])
expect(json_response['data']).to have_relationship(:default_billing_address)
.with_data({ 'id' => new_default_bill_address.id.to_s, 'type' => 'address' })
expect(json_response['data']).to have_relationship(:default_shipping_address)
diff --git a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
index fa4a3fdf83..3e6cfec75a 100644
--- a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
@@ -17,6 +17,8 @@ describe Spree::Api::V2::Platform::UserSerializer do
type: :user,
attributes: {
email: user.email,
+ first_name: user.first_name,
+ last_name: user.last_name,
average_order_value: [],
lifetime_value: [],
store_credits: [],
@@ -55,6 +57,8 @@ describe Spree::Api::V2::Platform::UserSerializer do
it do
expect(subject.serializable_hash[:data][:attributes]).to eq({
email: user.email,
+ first_name: user.first_name,
+ last_name: user.last_name,
average_order_value: [{ amount: '110.00', currency: 'USD' }, { amount: '110.00', currency: 'EUR' }],
lifetime_value: [{ amount: '110.00', currency: 'USD' }, { amount: '110.00', currency: 'EUR' }],
store_credits: [{ amount: '100.00', currency: 'USD' }, { amount: '90.00', currency: 'EUR' }],
diff --git a/api/spec/swagger_helper.rb b/api/spec/swagger_helper.rb
index 8b0b360327..307e6a8ef4 100644
--- a/api/spec/swagger_helper.rb
+++ b/api/spec/swagger_helper.rb
@@ -2064,6 +2064,8 @@ RSpec.configure do |config|
required: %w[email password password_confirmation],
properties: {
email: { type: :string },
+ first_name: { type: :string },
+ last_name: { type: :string },
password: { type: :string },
password_confirmation: { type: :string },
ship_address_id: { type: :string },
@@ -2083,6 +2085,8 @@ RSpec.configure do |config|
type: :object,
properties: {
email: { type: :string },
+ first_name: { type: :string },
+ last_name: { type: :string },
password: { type: :string },
password_confirmation: { type: :string },
ship_address_id: { type: :string },
diff --git a/core/app/models/spree/legacy_user.rb b/core/app/models/spree/legacy_user.rb
index 95c7c56764..d21df8559d 100644
--- a/core/app/models/spree/legacy_user.rb
+++ b/core/app/models/spree/legacy_user.rb
@@ -8,7 +8,6 @@ module Spree
self.table_name = 'spree_users'
- attr_accessor :password
- attr_accessor :password_confirmation
+ attr_accessor :password, :password_confirmation
end
end
diff --git a/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb b/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb
new file mode 100644
index 0000000000..93372e1252
--- /dev/null
+++ b/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb
@@ -0,0 +1,9 @@
+class AddFirstNameAndLastNameToSpreeUsers < ActiveRecord::Migration[5.2]
+ def change
+ if Spree.user_class.present?
+ users_table_name = Spree.user_class.table_name
+ add_column users_table_name, :first_name, :string unless column_exists?(users_table_name, :first_name)
+ add_column users_table_name, :last_name, :string unless column_exists?(users_table_name, :last_name)
+ end
+ end
+end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index bb66eaf2b2..129dcdceb2 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -146,7 +146,8 @@ module Spree
]
# TODO: Should probably use something like Spree.user_class.attributes
- @@user_attributes = [:email, :bill_address_id, :ship_address_id, :password, :password_confirmation, { public_metadata: {}, private_metadata: {} }]
+ @@user_attributes = [:email, :bill_address_id, :ship_address_id, :password, :first_name, :last_name,
+ :password_confirmation, { public_metadata: {}, private_metadata: {} }]
@@variant_attributes = [
:name, :presentation, :cost_price, :discontinue_on, :lock_version,
diff --git a/core/lib/spree/testing_support/factories/user_factory.rb b/core/lib/spree/testing_support/factories/user_factory.rb
index 935c76b925..97d8c199ca 100644
--- a/core/lib/spree/testing_support/factories/user_factory.rb
+++ b/core/lib/spree/testing_support/factories/user_factory.rb
@@ -9,6 +9,8 @@ FactoryBot.define do
password { 'secret' }
password_confirmation { password }
authentication_token { generate(:user_authentication_token) } if Spree.user_class.attribute_method? :authentication_token
+ first_name { 'John' }
+ last_name { 'Snow' }
public_metadata { {} }
private_metadata { {} }