Class: Slk::Services::ProfileResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/profile_resolver.rb

Overview

Orchestrates the API calls needed to assemble a Models::Profile. Memoizes within the instance — one resolver per command run.

Constant Summary collapse

SCHEMA_TTL =

24h

86_400
PROFILE_TTL =

1h

3_600
EMPTY_SCHEMA =
{ 'ok' => false, 'profile' => { 'fields' => [], 'sections' => [] } }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(users_api:, team_api:, cache_store: nil, workspace_name: nil, on_debug: nil) ⇒ ProfileResolver

Returns a new instance of ProfileResolver.



14
15
16
17
18
19
20
21
22
23
# File 'lib/slk/services/profile_resolver.rb', line 14

def initialize(users_api:, team_api:, cache_store: nil, workspace_name: nil, on_debug: nil)
  @users_api = users_api
  @team_api = team_api
  @cache_store = cache_store
  @workspace_name = workspace_name
  @on_debug = on_debug
  @refresh = false
  @profile_cache = {}
  @home_team_names = {}
end

Instance Attribute Details

#refreshObject

Returns the value of attribute refresh.



12
13
14
# File 'lib/slk/services/profile_resolver.rb', line 12

def refresh
  @refresh
end

Instance Method Details

#resolve(user_id) ⇒ Object

Resolve a user ID to a Profile. Memoized per resolver instance.



26
27
28
29
30
# File 'lib/slk/services/profile_resolver.rb', line 26

def resolve(user_id)
  return @profile_cache[user_id] if @profile_cache.key?(user_id)

  @profile_cache[user_id] = build_profile(user_id)
end

#resolve_chain_up(user_id, depth: 5) ⇒ Object

Walks Supervisor (or first non-inverse type:user field) upward. Returns Array<Profile> from immediate supervisor to top, capped by depth.



46
47
48
49
50
51
52
# File 'lib/slk/services/profile_resolver.rb', line 46

def resolve_chain_up(user_id, depth: 5)
  chain = []
  seen = Set.new([user_id])
  current = resolve(user_id)
  depth.times { current = step_up(current, seen, chain) or break }
  chain
end

#resolve_with_people(user_id) ⇒ Object

Resolve a profile and one level of type:user custom fields, populating ‘resolved_users` so the formatter can render the People section.



34
35
36
37
38
39
40
41
42
# File 'lib/slk/services/profile_resolver.rb', line 34

def resolve_with_people(user_id)
  profile = resolve(user_id)
  return profile if profile.external?

  profile.people_fields.flat_map(&:user_ids).uniq.each do |ref_id|
    profile.resolved_users[ref_id] ||= resolve(ref_id)
  end
  profile
end