From 2054b107b26e4c02f9feadc61d73c471e05880fb Mon Sep 17 00:00:00 2001 From: Reem Ibrahim Date: Fri, 19 Dec 2025 15:33:44 +0000 Subject: [PATCH 1/2] added update username patch endpoint and spec --- app/controllers/v1/users_controller.rb | 12 ++++++++ config/routes.rb | 6 +++- spec/requests/v1/users_spec.rb | 38 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 4b13763c8..962e7aa93 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -4,4 +4,16 @@ def index render jsonapi: users end + + def update_name + user = User.find_by!(auth_id: current_auth_id) + name_param = params.dig('_jsonapi', 'name') + result = UpdateUser.new(user, name_param).call + + if result.success? + render jsonapi: user, status: :ok + else + render jsonapi_errors: { error: I18n.t("errors.messages.error_updating_user_in_auth0")}, status: :unprocessable_entity + end + end end diff --git a/config/routes.rb b/config/routes.rb index b75038aab..84d0fc374 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,7 +21,11 @@ get '/check', to: 'check#index', defaults: { format: :json } namespace :v1, defaults: { format: :json } do - resources :users, only: %i[index] + resources :users, only: %i[index] do + collection do + patch :update_name + end + end resources :suppliers, only: %i[index] diff --git a/spec/requests/v1/users_spec.rb b/spec/requests/v1/users_spec.rb index 1d78aedd0..b23b46be9 100644 --- a/spec/requests/v1/users_spec.rb +++ b/spec/requests/v1/users_spec.rb @@ -31,4 +31,42 @@ .with_value(true) end end + + describe 'PATCH /v1/users/update_name' do + it 'updates the name of the current user' do + user = FactoryBot.create(:user) + stub_auth0_token_request + stub_auth0_update_user_request(user) + + patch '/v1/users/update_name', + headers: { 'X-Auth-Id' => JWT.encode(user.auth_id, 'test') }, + params: { + _jsonapi: { + name: 'New User Name' + } + } + + expect(response).to be_successful + expect(json['data']).to have_id(user.id) + expect(json['data']) + .to have_attribute(:name) + .with_value('New User Name') + end + + it 'returns an error if the Auth0 update fails' do + user = FactoryBot.create(:user) + stub_auth0_token_request + stub_auth0_update_user_request_failure(user) + + patch '/v1/users/update_name', + headers: { 'X-Auth-Id' => JWT.encode(user.auth_id, 'test') }, + params: { + _jsonapi: { + name: 'New User Name' + } + } + + expect(response.status).to eq 422 + end + end end From 947f15df5dbe94b9eda77b6a178b45a6a243920e Mon Sep 17 00:00:00 2001 From: Reem Ibrahim Date: Fri, 19 Dec 2025 15:34:26 +0000 Subject: [PATCH 2/2] rubocop --- app/controllers/v1/users_controller.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 962e7aa93..03603e28b 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -9,11 +9,12 @@ def update_name user = User.find_by!(auth_id: current_auth_id) name_param = params.dig('_jsonapi', 'name') result = UpdateUser.new(user, name_param).call - + if result.success? render jsonapi: user, status: :ok else - render jsonapi_errors: { error: I18n.t("errors.messages.error_updating_user_in_auth0")}, status: :unprocessable_entity + render jsonapi_errors: { error: I18n.t('errors.messages.error_updating_user_in_auth0') }, + status: :unprocessable_entity end end end