Class: Eco::API::UseCases::DefaultCases::RecoverDBCase

Inherits:
Eco::API::UseCases::DefaultCase show all
Defined in:
lib/eco/api/usecases/default_cases/recover_db_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
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eco/api/usecases/default_cases/recover_db_case.rb', line 7

def process
  @cases.define("recover-db", type: :sync) do |entries, people, session, options, usecase|
    unless entries.is_a?(Eco::API::Organization::People)
      raise "Your input should be an 'Eco::API::Organization::People' object. Got: #{entries.class}"
    end

    if options.dig(:include, :create)
      creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account])
    end

    update   = session.job_group("main").new("update", usecase: usecase, type: :update, sets: [:core, :details, :account])

    if options.dig(:include, :delete)
      remove   = session.job_group("main").new("remove", usecase: usecase, type: :delete, sets: [:core, :details, :account])
      people.exclude(entries).map {|person| remove.add(person)}
    end

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

    not_exist = []

    entries.each.with_index do |entry, i|
      create = ! (person = people.find(entry, strict: strict_search))

      if create && !options.dig(:include, :create)
        not_exist.push(entry);
      else
        person = session.new_person if create

        unless options.dig(:exclude, :core)
          person.external_id   = entry.external_id   unless options.dig(:exclude, :external_id)
          person.name          = entry.name          unless options.dig(:exclude, :name)
          person.email         = entry.email         unless options.dig(:exclude, :email)
          person.supervisor_id = entry.supervisor_id unless options.dig(:exclude, :supervisor)
          person.filter_tags   = entry.filter_tags || [] unless options.dig(:exclude, :filter_tags)
        end

        unless options.dig(:exclude, :account)
          #person.account = entry.account
          if eaccount = entry.
            person. ||= {}
            person..default_tag          = eaccount.default_tag  unless options.dig(:exclude, :filter_tags)

            unless options.dig(:exclude, :policy_groups) && person..policy_group_ids
              person..policy_group_ids   = eaccount.policy_group_ids
            end

            unless options.dig(:exclude, :abilities) && (person..permissions_preset || person..permissions_custom)
              person..permissions_preset = eaccount.permissions_preset if eaccount.permissions_preset
              person..permissions_custom = eaccount.permissions_custom
            end
            person.. = eaccount. if eaccount.
            person..accept_eula        = eaccount.accept_eula if eaccount.accept_eula

            if eaccount.preferences
              person..doc["preferences"] = JSON.parse((eaccount.doc["preferences"] || {}).to_json)
            end

            person..starred_ids        = eaccount.starred_ids     if eaccount.landing_page_id
            person..landing_page_id    = eaccount.landing_page_id if eaccount.landing_page_id

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

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

        creation.add(person) if create
        update.add(person) unless create
      end
    end

    unless not_exist.empty?
      session.logger.error("There were #{not_exist.length} entries of the back up that do not exist in the (filtered?) people manager")
      session.logger.error("Some examples:")
      not_exist.slice(0, 4).each_with_index do |entry, i|
        session.logger.error("Entry(#{i}) - this person does not exist: #{entry.name} (#{entry.email})")
      end
    end

  end
end