module Eco module API class Session < Common::Session::BaseSession attr_reader :batch, :policy_groups attr_accessor :schema, :schemas_hash attr_reader :tagtree attr_reader :job_groups #attr_reader :organization #alias_method :org, :organization def initialize(init = {}) e = init msg = "Expected object Eco::API::Session::Config or Eco::API::Common::Session::Environment. Given: #{init}" raise msg unless e.is_a?(Session::Config) || e.is_a?(Eco::API::Common::Session::Environment) e = Eco::API::Common::Session::Environment.new(init, session: self) if !e.is_a?(Eco::API::Common::Session::Environment) super(e) logger.debug("LINE COMMAND: #{$0} #{ARGV.join(" ")}") @batch = Batch.new(enviro) @task = Task.new(enviro) @job_groups = JobGroups.new(enviro) @use_cases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases.use_group) @schemas = api&.person_schemas.to_a @schemas_hash = @schemas.map do |sch| [[sch.id, sch], [sch.name.downcase, sch]] end.flatten(1).to_h self.schema = config.people.default_schema || @schemas.first @policy_groups = nil @tagtree = nil if tree_file = config.dig('org', 'tagtree') tree = file_manager.load_json(tree_file) unless !tree_file @tagtree = Eco::API::Organization::TagTree.new(tree, enviro: enviro) end presets_custom_file = config.people.presets_custom presets_map_file = config.people.presets_map @presets_factory = Eco::API::Organization::PresetsFactory.new({ #policy_groups: @policy_groups, presets_custom: file_manager.dir.file(presets_custom_file, should_exist: true), presets_map: file_manager.dir.file(presets_map_file, should_exist: true), enviro: enviro }) end def self.configure # TODO: change to Session::Config.new.tap conf = Session::Config.new yield(conf) if block_given? conf end # TASKS & JOBS def do @task end def mail_to(**kargs) @mailer ||= Eco::API::Common::Session::Mailer.new(enviro: enviro) @mailer.mail(**kargs) end def s3upload(content: nil, file: nil, directory: nil) @s3uploader ||= Eco::API::Common::Session::S3Uploader.new(enviro: enviro) if content && file @s3uploader.upload(file, content) elsif file @s3uploader.upload_file(file) elsif directory @s3uploader.upload_directory(directory) else logger.error("To use Session.s3upload, you must specify either directory, file or content and file name") end end def job_group(name, order: :last) case when job_groups.exists?(name) job_groups[name] else job_groups.new(name, order: order) end end def jobs_launch(simulate: false) job_groups.launch(simulate: simulate) end def policy_groups @policy_groups ||= config.policy_groups end def schema=(value) case value when String sch = @schemas_hash[value.downcase] fatal "The schema with id or name '#{value}' does not exist" if !sch @schema = sch when Ecoportal::API::V1::PersonSchema @schema = value else logger.error("Session: Missuse of 'schema=' method. Given: #{value}") return end @person_factory = Eco::API::Common::People::PersonFactory.new(schema: @schema) fields_mapper_file = config.people.fields_mapper fields_mapper = Eco::Data::Mapper.new(file_manager.load_json(fields_mapper_file)) @entry_factory = Eco::API::Common::People::EntryFactory.new({ schema: @schema, person_parser: config.people.parser, fields_mapper: fields_mapper, logger: logger }) end def new_preset(input) case input when Ecoportal::API::Internal::Person @presets_factory.new(*input&.account&.policy_group_ids) when Array @presets_factory.new(*input) else @presets_factory.new(input) end end def new_person(**keyed_args) @person_factory.new(**keyed_args) end def new_entry(data, dependencies: {}) @entry_factory.new(data, dependencies: dependencies) end def csv_entries(file) rows = [] if Eco::API::Common::Session::FileManager.file_exists?(file) CSV.foreach(file, headers: true, encoding: Eco::API::Common::Session::FileManager.encoding(file)).with_index do |row, i| rows.push(row) end end @entry_factory.entries(rows) end def discarded_entries file = config.people.discarded_file file = file_manager.dir.file(file) csv_entries(file) end def process_case(name, type: nil, **params) args = { session: self }.merge(params) fatal("Undefined usecase '#{name}' of type '#{type.to_s}'") if !@use_cases.defined?(name, type: type) logger.debug("Session: going to process '#{name}' defined case") @use_cases.case(name, type: type).process(**args) end end end end require_relative 'session/config' require_relative 'session/batch' require_relative 'session/batch_status' require_relative 'session/task' require_relative 'session/batch_job' require_relative 'session/batch_jobs' require_relative 'session/job_groups'