Sha256: 2fdbf08fd80b6548beb54fcc5a6a8adfe95525979e995b3ccbd41fba08f855ae

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

module Eco
  module API
    class UseCases
      class DefaultCases
        class ToCsvDetailedCase < DefaultCase

          def process
            @cases.define("to-csv-detailed", type: :export) do |people, session, options, usecase|
              unless people && !people.empty?
                session.logger.warn("No source people to create the file... aborting!")
                next false
              end

              unless file = options[:file] || options.dig(:export, :file, :name)
                session.logger.error("Destination file not specified")
                next false
              end

              login_providers = session.login_providers

              abilities = session.new_preset([]).keys

              session.logger.info("going to create file: #{file}")
              CSV.open(file, "w") do |csv|
                deps    = {"supervisor_id" => {people: people}}
                header  = session.new_entry(people.first, dependencies: deps).to_hash.keys
                header += abilities
                header += ["Login Methods"] if login_providers.any_optional?
                header += ["Subordinates"]

                csv << header
                people.each do |person|
                  data = session.new_entry(person, dependencies: deps).to_hash.values
                  person_abilities = (person.account && person.account.permissions_custom) || {}
                  data += abilities.map {|key| person_abilities[key] || "no access"}
                  if login_providers.any_optional?
                    logins = (person.account && person.account.login_provider_ids) || []
                    data.push(login_providers.to_name(logins).join("|"))
                  end
                  data.push(person.subordinates)
                  csv << data
                end
              end
              true
            end
          end

        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eco-helpers-1.0.9 lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb
eco-helpers-1.0.8 lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb