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.

SD-1453 As a Storefront API user I can set User MetaData when creating an Account (#11497)

Aleksandar Petrushev56f131ac5295
diff --git a/api/app/serializers/spree/v2/storefront/user_serializer.rb b/api/app/serializers/spree/v2/storefront/user_serializer.rb
index c5e163e9fa..8a3a233843 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
+ attributes :email, :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 cbcd84dbed..cf57775aad 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -63,6 +63,16 @@ paths:
password_confirmation:
type: string
example: spree123
+ public_metadata:
+ type: object
+ example:
+ user_segment: 'supplier'
+ description: The public metadata for this User
+ private_metadata:
+ type: object
+ example:
+ has_abandoned_cart: false
+ description: The private metadata for this User
description: ''
responses:
'200':
@@ -3470,6 +3480,11 @@ components:
type: number
example: 3
description: Number of placed Orders by this User
+ public_metadata:
+ type: object
+ example:
+ user_segment: 'supplier'
+ description: The public metadata for this User
relationships:
type: object
properties:
@@ -4889,6 +4904,8 @@ components:
store_credits: 0
completed_orders: 0
+ public_metadata:
+ user_segment: 'supplier'
relationships:
default_billing_address:
data:
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 620ab0d3c3..51a4db22ed 100644
--- a/api/spec/requests/spree/api/v2/storefront/account_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/account_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe 'Storefront API v2 Account spec', type: :request do
include_context 'API v2 tokens'
- let!(:user) { create(:user_with_addresses) }
+ let!(:user) { create(:user_with_addresses) }
let(:headers) { headers_bearer }
shared_examples 'mock tests for failed user saving' do
@@ -43,6 +43,7 @@ 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(:completed_orders).with_value(user.orders.complete.count)
+ expect(json_response['data']).to have_attribute(:public_metadata).with_value(user.public_metadata)
end
context 'with params "include=default_billing_address"' do
@@ -89,7 +90,9 @@ describe 'Storefront API v2 Account spec', type: :request do
password: 'newpassword123',
password_confirmation: 'newpassword123',
bill_address_id: default_bill_address.id.to_s,
- ship_address_id: default_ship_address.id.to_s
+ ship_address_id: default_ship_address.id.to_s,
+ public_metadata: { 'has_other_account' => 'true' },
+ private_metadata: { 'shops_in_other_stores' => 'false' }
}
end
let(:params) { { user: new_attributes } }
@@ -100,8 +103,11 @@ describe 'Storefront API v2 Account spec', type: :request do
it_behaves_like 'returns 200 HTTP status'
it 'creates and returns user' do
- expect(json_response['data']['id'].to_i).to eq Spree.user_class.last.id
+ created_user = Spree.user_class.last
+ expect(json_response['data']['id'].to_i).to eq created_user.id
expect(json_response['data']).to have_attribute(:email).with_value(new_attributes[:email])
+ 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)
end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index f4ae0e74c6..a099ee519d 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -145,7 +145,7 @@ module Spree
]
# TODO: Should probably use something like Spree.user_class.attributes
- @@user_attributes = [:email, :bill_address_id, :ship_address_id, :password, :password_confirmation]
+ @@user_attributes = [:email, :bill_address_id, :ship_address_id, :password, :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 45d3ecbecf..935c76b925 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
+ public_metadata { {} }
+ private_metadata { {} }
factory :admin_user do
spree_roles { [Spree::Role.find_by(name: 'admin') || create(:role, name: 'admin')] }