Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/app/serializers/spree/v2/storefront/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Storefront
class UserSerializer < BaseSerializer
set_type :user

attributes :email
attributes :email, :public_metadata

attribute :store_credits do |user|
user.total_available_store_credit
Expand Down
17 changes: 17 additions & 0 deletions api/docs/v2/storefront/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
damianlegawiec marked this conversation as resolved.
Show resolved Hide resolved
description: ''
responses:
'200':
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -4889,6 +4904,8 @@ components:
email: spree@example.com
store_credits: 0
completed_orders: 0
public_metadata:
user_segment: 'supplier'
relationships:
default_billing_address:
data:
Expand Down
12 changes: 9 additions & 3 deletions api/spec/requests/spree/api/v2/storefront/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand Down Expand Up @@ -43,6 +43,7 @@
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
Expand Down Expand Up @@ -89,7 +90,9 @@
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 } }
Expand All @@ -100,8 +103,11 @@
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

Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module PermittedAttributes
]

# 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,
Expand Down
2 changes: 2 additions & 0 deletions core/lib/spree/testing_support/factories/user_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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')] }
Expand Down