From 711bcbe12dd87105890d6fe121f7cf96282d2a3a Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Thu, 25 Jul 2024 16:56:56 -0300 Subject: [PATCH] add user id verification to user by external id --- app/models/user.rb | 11 ++++++----- spec/models/user_spec.rb | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 5b0a841..a34e2f3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -66,11 +66,12 @@ def name end scope :by_external_identifier, lambda { |identifiers| - identifiers = [identifiers].flatten.compact.map(&:downcase) - joins('LEFT JOIN user_service_identifiers ON users.id = user_service_identifiers.user_id') - .where('lower(users.email) IN (:identifiers) OR lower(user_service_identifiers.identifier) IN (:identifiers)', - identifiers:) - } + identifiers = [identifiers].flatten.compact.map(&:to_s).map(&:downcase) + joins('LEFT JOIN user_service_identifiers ON users.id = user_service_identifiers.user_id') + .where('lower(users.email) IN (:identifiers) OR lower(user_service_identifiers.identifier) IN + (:identifiers) OR CAST(users.id AS TEXT) IN (:identifiers)', + identifiers:) + } def self.by_name(full_name) name_parts = full_name.split diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 17d8be3..a6d14a5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -102,6 +102,12 @@ user = create(:user, email: 'bruce.wayne@codelitt.com') expect(User.by_external_identifier(user.email).first).to eql(user) end + + it 'returns the user by the user id' do + user = create(:user, email: 'bruce.wayne@codelitt.com') + + expect(User.by_external_identifier(user.id).first).to eql(user) + end end context '#by_name' do