Class: Eco::API::Organization::People

Inherits:
Language::Models::Collection show all
Defined in:
lib/eco/api/organization/people.rb

Constant Summary

Constants inherited from Language::Models::Collection

Language::Models::Collection::BASIC_METHODS, Language::Models::Collection::EXTENDED_METHODS

Main identifier helpers collapse

Special filters collapse

Searchers collapse

Basic Collection Methods collapse

Groupping methods collapse

Instance Method Summary collapse

Methods inherited from Language::Models::Collection

#<, #<<, #attr, #attr?, attr_collection, attr_presence, #attrs, attrs_create_method, #contains, #delete!, #each, #empty, #empty?, #group_by, #length, #new, #present, #present_all?, #present_some?, #remove, #to_c, #unique_attrs, #update

Constructor Details

#initialize(people = [], klass: Ecoportal::API::Internal::Person) ⇒ People

Returns a new instance of People.



11
12
13
14
15
# File 'lib/eco/api/organization/people.rb', line 11

def initialize(people = [], klass: Ecoportal::API::Internal::Person)
  @klass = Ecoportal::API::Internal::Person unless klass == Ecoportal::API::V1::Person
  super(people, klass: @klass)
  @caches_init = false
end

Instance Method Details

#[](id_or_ext) ⇒ Object



27
28
29
# File 'lib/eco/api/organization/people.rb', line 27

def [](id_or_ext)
  id(id_or_ext) || external_id(id_or_ext)
end

#contactsObject



38
39
40
# File 'lib/eco/api/organization/people.rb', line 38

def contacts
  details_present(true)
end

#email_id_mapsObject



166
167
168
# File 'lib/eco/api/organization/people.rb', line 166

def email_id_maps
  users.group_by(:email).transform_values { |person| person.id }
end

#exclude(object, strict: false) ⇒ Object



147
148
149
# File 'lib/eco/api/organization/people.rb', line 147

def exclude(object, strict: false)
  exclude_people(into_a(object), strict: strict)
end

#exclude!(object, strict: false) ⇒ Object



151
152
153
# File 'lib/eco/api/organization/people.rb', line 151

def exclude!(object, strict: false)
  self < exclude(object, strict: strict)
end

#exclude_people(list, strict: false) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/eco/api/organization/people.rb', line 155

def exclude_people(list, strict: false)
  list.map do |person|
    find(person, strict: strict)
  end.compact.yield_self do |discarded|
    newFrom to_a - discarded
  end
end

#external_id(*args) ⇒ Object



23
24
25
# File 'lib/eco/api/organization/people.rb', line 23

def external_id(*args)
  attr('external_id', *args).first
end

#filter_tags_all(tags) ⇒ Object



64
65
66
# File 'lib/eco/api/organization/people.rb', line 64

def filter_tags_all(tags)
  attr("filter_tags", tags, default_modifier.all.insensitive)
end

#filter_tags_any(tags) ⇒ Object



60
61
62
# File 'lib/eco/api/organization/people.rb', line 60

def filter_tags_any(tags)
  attr("filter_tags", tags, default_modifier.any.insensitive)
end

#find(object, strict: false) ⇒ Object



109
110
111
112
113
114
# File 'lib/eco/api/organization/people.rb', line 109

def find(object, strict: false)
  id          = attr_value(object, "id")
  external_id = attr_value(object, "external_id")
  email       = attr_value(object, "email")
  person(id: id, external_id: external_id, email: email, strict: strict)
end

#group_by_supervisorObject



170
171
172
# File 'lib/eco/api/organization/people.rb', line 170

def group_by_supervisor
  to_h(:supervisor_id)
end

#id(*args) ⇒ Object



19
20
21
# File 'lib/eco/api/organization/people.rb', line 19

def id(*args)
  attr('id', *args).first
end

#merge(data, strict: false, uniq: true) ⇒ Object



141
142
143
144
145
# File 'lib/eco/api/organization/people.rb', line 141

def merge(data, strict: false, uniq: true)
  list = uniq ? exclude_people(data, strict: strict).to_a : to_a
  data = data.to_a unless data.is_a?(Array)
  newFrom list + data
end

#missing_supervisors_idsObject



55
56
57
58
# File 'lib/eco/api/organization/people.rb', line 55

def missing_supervisors_ids
  sup_ids = self.supervisor_ids
  sup_ids - (sup_ids & self.ids)
end

#newFrom(data) ⇒ Object



123
124
125
# File 'lib/eco/api/organization/people.rb', line 123

def newFrom(data)
  self.class.new(data, klass: @klass)
end

#non_usersObject



42
43
44
# File 'lib/eco/api/organization/people.rb', line 42

def non_users
  (false)
end

#person(id: nil, external_id: nil, email: nil, strict: false) ⇒ Person?

It searches a person using the parameters given.

Parameters:

  • id (String) (defaults to: nil)

    the internal id of the person

  • external_id (String) (defaults to: nil)

    the exernal_id of the person

  • email (String) (defaults to: nil)

    the email of the person

  • strict (Boolean) (defaults to: false)

    if should perform a soft or a strict search. strict will avoid repeated email addresses.

Returns:

  • (Person, nil)

    the person we were searching, or nil if not found.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/eco/api/organization/people.rb', line 86

def person(id: nil, external_id: nil, email: nil, strict: false)
  init_caches
  pers = @by_id[id]&.first if id
  pers = @by_external_id[external_id&.strip]&.first       if !pers && !external_id.to_s.strip.empty?

  # strict prevents taking existing user for searched person with same email
  # specially useful if the organisation ensures all have external id (no need for email search)
  if !pers && (!strict || external_id.to_s.strip.empty?)
    # person still not found and either not strict or no external_id provided
    pers = @by_users_email[email&.downcase.strip]&.first  if !email.to_s.strip.empty?

    if !pers && !strict && !email.to_s.strip.empty?
      candidates = @by_non_users_email[email&.downcase.strip] || []
      raise "Too many non-user candidates (#{candidates.length}) with email '#{email}'" if candidates.length > 1
      pers = candidates.first
    end

    pers = @by_external_id[email&.downcase.strip]&.first    if !pers && !email.to_s.strip.empty?
  end

  pers
end

#policy_group_ids_all(ids) ⇒ Object



72
73
74
# File 'lib/eco/api/organization/people.rb', line 72

def policy_group_ids_all(ids)
  attr("policy_group_ids", tags, default_modifier.all.insensitive)
end

#policy_group_ids_any(ids) ⇒ Object



68
69
70
# File 'lib/eco/api/organization/people.rb', line 68

def policy_group_ids_any(ids)
  attr("policy_group_ids", tags, default_modifier.any.insensitive)
end

#supervisorsObject



46
47
48
49
50
51
52
53
# File 'lib/eco/api/organization/people.rb', line 46

def supervisors
  sup_ids = self.ids & self.supervisor_ids
  sup_ids.map do |id|
    person(id: id, strict: true)
  end.yield_self do |supervisors|
    newFrom supervisors
  end
end

#to_h(attr = "id") ⇒ Object



174
175
176
# File 'lib/eco/api/organization/people.rb', line 174

def to_h(attr = "id")
  super(attr || "id")
end

#to_jsonObject



119
120
121
# File 'lib/eco/api/organization/people.rb', line 119

def to_json
  to_a.to_json
end

#uniq(strict: false, include_unsearchable: false) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/eco/api/organization/people.rb', line 127

def uniq(strict: false, include_unsearchable: false)
  unsearchable = []
  to_a.each_with_object([]) do |person, people|
    if found = find(person, strict: strict)
      people << found
    else
      unsearchable << person
    end
  end.yield_self do |found|
    found += unsearchable if include_unsearchable
    newFrom found
  end
end

#usersObject



34
35
36
# File 'lib/eco/api/organization/people.rb', line 34

def users
  (true)
end