class Eco::API::UseCases::Default::People::OrgDataConvertCase < Eco::API::Common::Loaders::UseCase name "org-data-convert" type :import def org_data_convert(people) pp "Going to convert data from '#{source_enviro}' to '#{ASSETS.active_config}'" puts "\n" people.each.with_index do |person, i| if (i % 10 == 0) 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) if account = person.account account.policy_group_ids = policy_groups_convert(account.policy_group_ids) account.doc.tap do |doc| ["login_provider_ids", "starred_ids", "landing_page_id"].map do |attr| doc.delete(attr) end end end end def details_convert!(person) if 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 end def policy_groups_convert(ids) ids.map do |id| unless name = source_policy_groups.to_name(id) || 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| unless config error("The source environment '#{source_enviro}' does not exit or is not configured") end end end def source_enviro @source_enviro ||= options.dig(:source_enviro).tap do |enviro| error("A source environment is required to migrate data from") unless enviro end end def ignore_missing_policy_groups? options.dig(:ignore, :missing, :policy_groups) end def logger @session.logger end def error(msg) logger.error(msg) exit(1) end end