Class: Eco::API::UseCases::DefaultCases::CreateCase

Inherits:
Eco::API::UseCases::DefaultCase show all
Defined in:
lib/eco/api/usecases/default_cases/create_case.rb

Instance Method Summary collapse

Methods inherited from Eco::API::UseCases::DefaultCase

#initialize

Constructor Details

This class inherits a constructor from Eco::API::UseCases::DefaultCase

Instance Method Details

#processObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/eco/api/usecases/default_cases/create_case.rb', line 7

def process
  @cases.define("create", type: :sync) do |entries, people, session, options, usecase|
    creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
    supers   = session.job_group("post").new("supers", usecase: usecase, type: :update, sets: :core)

    strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
    pgs = session.policy_groups

    entries.each.with_index do |entry, i|
      if person = people.find(entry, strict: strict_search)
        session.logger.error("Entry(#{i}) - this person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}")
      else
        person = session.new_person
        core_excluded = ["supervisor_id"]

        ini_tags   = person.filter_tags || []
        entry.set_core(person, exclude: core_excluded)

        if session.tagtree
          person.filter_tags = session.tagtree.user_tags(
            initial:          ini_tags,
            final:            person.filter_tags,
            preserve_custom:  true,
            add_custom:       true
          )
        end

        entry.set_details(person) unless options.dig(:exclude, :details)

        unless options.dig(:exclude, :account)
          ini_pg_ids = person.&.policy_group_ids || []
          entry.(person)

          person.&.send_invites = options[:send_invites] if options.key?(:send_invites)

          person..policy_group_ids   = pgs.user_pg_ids(
            initial:         ini_pg_ids,
            final:           person..policy_group_ids
          )

          person..permissions_custom = session.new_preset(person)

          if session.tagtree
            person..default_tag = session.tagtree.default_tag(*person.filter_tags)
          else
            tags = person.filter_tags
            person..default_tag = tags.first unless tags.length > 1
          end
        end

        creation.add(person)

        # set supervisor

        unless options.dig(:exclude, :core) || options.dig(:exclude, :supervisor)
          if !(sup_id = entry.supervisor_id)
            person.supervisor_id = nil
          else
            if supervisor = people.person(id: sup_id, external_id: sup_id, email: sup_id)
              person.supervisor_id = supervisor.id
            else
              # delay setting supervisor if does not exit

              supers.add(person) do |person|
                person.supervisor_id = sup_id
              end
            end
          end
        end

      end
    end
  end
end