Skip to content

Commit 79fca5b

Browse files
committed
Update UserInfoApiClient to allow user lookup by email.
This change adds a search_by_email method to allow lookup of users by email address to acquire the ID of proposed school owners.
1 parent 75ecaa4 commit 79fca5b

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

lib/user_info_api_client.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,34 @@ def fetch_by_ids(user_ids)
1818
transform_result(response.body.fetch('users', []))
1919
end
2020

21+
def search_by_email(email)
22+
return [] if email.blank?
23+
return stubbed_users_by_email(email) if bypass_oauth?
24+
25+
response = conn.get do |r|
26+
r.url "/users/#{email}"
27+
end
28+
return [] if response.body.blank?
29+
30+
# Single user response has 'user' key, not 'users'
31+
user = response.body.fetch('user', nil)
32+
user.present? ? [transform_user(user)] : []
33+
rescue Faraday::ResourceNotFound
34+
[]
35+
end
36+
2137
private
2238

2339
def bypass_oauth?
2440
ENV.fetch('BYPASS_OAUTH', nil) == 'true'
2541
end
2642

43+
def transform_user(user)
44+
user.transform_keys { |k| k.to_s.underscore.to_sym }
45+
end
46+
2747
def transform_result(result)
28-
{ result: }.transform_keys { |k| k.to_s.underscore.to_sym }.fetch(:result)
48+
result.map { |user| transform_user(user) }
2949
end
3050

3151
def conn
@@ -62,5 +82,26 @@ def stubbed_users(user_ids)
6282
}
6383
end
6484
end
85+
86+
def stubbed_users_by_email(email)
87+
[{
88+
id: SecureRandom.uuid,
89+
email: email,
90+
username: nil,
91+
parentalEmail: nil,
92+
name: 'School Owner',
93+
nickname: 'Owner',
94+
country: 'United Kingdom',
95+
country_code: 'GB',
96+
postcode: nil,
97+
dateOfBirth: nil,
98+
verifiedAt: '2024-01-01T12:00:00.000Z',
99+
createdAt: '2024-01-01T12:00:00.000Z',
100+
updatedAt: '2024-01-01T12:00:00.000Z',
101+
discardedAt: nil,
102+
lastLoggedInAt: '2024-01-01T12:00:00.000Z',
103+
roles: ''
104+
}]
105+
end
65106
end
66107
end

0 commit comments

Comments
 (0)