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::ATTR_COLLECTION_METHODS, Language::Models::Collection::ATTR_PRESENCE_METHODS

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, #newFrom, #present, #present_all?, #present_some?, #remove, #to_c, #unique_attrs, #update

Constructor Details

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



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

def initialize(people = [], klass: Ecoportal::API::Internal::Person, factory: nil)
  @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



21
22
23
# File 'lib/eco/api/organization/people.rb', line 21

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

#contactsObject



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

def contacts
  details_present(true)
end

#email_id_mapsObject



116
117
118
# File 'lib/eco/api/organization/people.rb', line 116

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

#exclude(object, strict: false) ⇒ Object



101
102
103
# File 'lib/eco/api/organization/people.rb', line 101

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

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



105
106
107
# File 'lib/eco/api/organization/people.rb', line 105

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

#exclude_people(list, strict: false) ⇒ Object



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

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

#external_id(*args) ⇒ Object



29
30
31
# File 'lib/eco/api/organization/people.rb', line 29

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

#find(object, strict: false) ⇒ Object



74
75
76
77
78
79
# File 'lib/eco/api/organization/people.rb', line 74

def find(object, strict: false)
  id          = object.respond_to?("id")?          object.send("id") : nil
  external_id = object.respond_to?("external_id")? object.send("external_id") : nil
  email       = object.respond_to?("email")?       object.send("email") : nil
  person(id: id, external_id: external_id, email: email, strict: strict)
end

#group_by_supervisorObject



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

def group_by_supervisor
  to_h(:supervisor_id)
end

#id(*args) ⇒ Object



25
26
27
# File 'lib/eco/api/organization/people.rb', line 25

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

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



95
96
97
98
99
# File 'lib/eco/api/organization/people.rb', line 95

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



133
134
135
136
# File 'lib/eco/api/organization/people.rb', line 133

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

#non_usersObject



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

def non_users
  (false)
end

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

It searches a person using the paramters given.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/eco/api/organization/people.rb', line 51

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

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

  pers
end

#supervisorsObject

only those that are present



125
126
127
128
129
130
131
# File 'lib/eco/api/organization/people.rb', line 125

def supervisors
  h_all = self.to_h
  sup_ids = h_all.keys & self.supervisor_ids
  newFrom h_all.select do |id, person|
    sup_ids.include?(id)
  end.values
end

#to_h(attr = "id") ⇒ Object



138
139
140
# File 'lib/eco/api/organization/people.rb', line 138

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

#to_jsonObject



17
18
19
# File 'lib/eco/api/organization/people.rb', line 17

def to_json
  to_a.to_json
end

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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/eco/api/organization/people.rb', line 81

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

#usersObject



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

def users
  (true)
end