class Eco::API::UseCases::Default::People::Treat::OrgDataConvertCase < Eco::API::Common::Loaders::UseCase name "org-data-convert" type :import def org_data_convert(people) log(:info) { "Going to convert data from '#{source_enviro}' to '#{ASSETS.active_config}'" } puts "\n" people.each.with_index do |person, i| if (i % 10).zero? percent = i * 100 / people.length print "Processing: #{percent.round}% (#{i}/#{people.length} entries)\r" $stdout.flush end person.id = nil details_convert!(person) account_convert!(person) end people end def main(people, _session, _options, _usecase) org_data_convert(people) end private def account_convert!(person) return unless (account = person.account) account.policy_group_ids = policy_groups_convert(account.policy_group_ids) account.doc.tap do |doc| %w[login_provider_ids starred_ids landing_page_id].map do |attr| doc.delete(attr) end end end def details_convert!(person) return unless person.details person.details.as_json.tap do |details_doc| details_doc["schema_id"] = session.schema.id person.add_details(session.schema) person.details = details_doc end end def policy_groups_convert(ids) ids.map do |id| name = source_policy_groups.to_name(id) unless name || ignore_missing_policy_groups? msg = "Ops, do not know #{id} usergroup for source environment" msg << "\nUse the option -ignore-missing-policy-groups if you do not care" error(msg) end session.policy_groups.to_id(name).tap do |new_id| unless new_id || ignore_missing_policy_groups? msg = "Ops, do not know #{name} usergroup for destination environment" msg << "\nUse the option -ignore-missing-policy-groups if you do not care" error(msg) end end end.compact end def source_policy_groups source_config.policy_groups end def source_config @source_config ||= ASSETS.config(key: source_enviro, update_active: false).tap do |config| next if config error("The source environment '#{source_enviro}' does not exit or is not configured") end end def source_enviro @source_enviro ||= options[:source_enviro].tap do |enviro| next if enviro error("A source environment is required to migrate data from") end end def ignore_missing_policy_groups? options.dig(:ignore, :missing, :policy_groups) end def error(msg) logger.error(msg) exit(1) end end