class Eco::API::UseCases::Default::People::Treat::SupersCyclicIdentify < Eco::API::Common::Loaders::UseCase name 'identify-cyclic-supers' type :export require_relative 'cli/supers_cyclic_identify_cli' OUT_FOLDER = "sftp".freeze OUT_TIME_FORMAT = '%Y%m%dT%H%M%S'.freeze OUT_FILENAME = "cyclic_supers".freeze attr_reader :people def main(_people, _session, options, _usecase) options[:end_get] = false save!(cyclic_sets) end private def cyclic_sets Eco::API::Common::People::SupervisorHelpers.identify_cyclic_chains(people) end def save!(data) if data.empty? log(:info) { "There were no cyclic supervisors identified!!" } return end ext = File.extname(file).downcase.delete(".") case ext when "txt" create_file(data, file: output_filename(ext: 'txt'), format: :txt) when "html" log(:warn) { "html is still not supported" } exit(1) # create_file(data, file: file, format: :html) when "json" log(:warn) { "json is still not supported" } exit(1) # create_file(data, file: file, format: :json) end end def create_file(sets, file:, format: :txt) File.open(file, "w") do |fd| fd << sets_to_str(sets, format: format) end log(:info) { "Generated file #{file}" } end def sets_to_str(sets, format: :txt) raise "Required Array. Given: #{sets.class}" unless sets.is_a?(Array) "".tap do |str| sets.each do |set| str << set_to_str(set, format: format) end end end def set_to_str(set, lev: 0, format: :txt) raise "Required Array. Given: #{set.class}" unless set.is_a?(Array) "".tap do |str| entry = set.shift str << (" " * lev) str << lev&.positive? ? "+-#{lev}- " : "" str << entry.name str << " (#{entry.external_id}|#{entry.email}|#{entry.id})\n" next unless set && !set.empty? str << set_to_str(set, lev: lev + 1, format: format) end end def output_filename(ext: nil) @output_filename ||= options.dig(:output, :file) || default_output_filename(ext: ext) end def default_output_filename(name = self.class::OUT_FILENAME, ext: nil) File.join(output_folder, "#{timestamp}_#{config.active_enviro}_#{name}.#{ext}") end def output_folder "#{config.active_enviro}/#{self.class::OUT_FOLDER}" end def timestamp(date = Time.now) date.strftime(self.class::OUT_TIME_FORMAT) end end