ASSETS.cli.config do |cnf| # rubocop:disable Metrics/BlockLength cnf.people_filters do |filters| # rubocop:disable Metrics/BlockLength filters.add("-only-users", "only those with account") do |people, _sess, options| options.deep_merge!(people: {filter: {account: true}}) people.users end filters.add("-no-account", "only those with no account") do |people, _sess, options| options.deep_merge!(people: {filter: {account: false}}) people.account_present(false) end filters.add("-with-details", "only those with details") do |people, _sess, options| options.deep_merge!(people: {filter: {details: true}}) people.details_present end filters.add("-no-id", "only those with no external_id") do |people, _sess, options| options.deep_merge!(people: {filter: {external_id: nil}}) people.external_id_present(false) end # filters.add("-supervisors", "only those that have subordinates") do |people, session, options| # options.deep_merge!(people: {filter: {subordinates: true}}) # people.newFrom people.to_a.select {|person| person.subordinates > 0} # end filters.add("-supervisor-id", "only those with certain supervisor") do |people, session, options| id = SCR.get_arg("-supervisor-id", with_param: true) options.deep_merge!(people: {filter: {supervisor_id: id}}) unless (sup = session.micro.with_supervisor(id, people, strict: true)) status = session.batch.search([{"id" => id}]) sup = status.people.first end unless sup session.log(:error) { "Supervisor with id '#{id}' does not exist" } exit(1) end people.supervisor_id(sup.id) end filters.add("-no-default-tag", "only those users with no default tag") do |people, _sess, options| options.deep_merge!(people: {filter: {account: {default_tag: nil}}}) people.users.reject do |person| person.account.default_tag end.then do |filtered| people.newFrom filtered end end desc = "only those that have ALL the specified tags separated by '|'" filters.add("-filter-tags-all", desc) do |people, session, options| tags = SCR.get_arg("-filter-tags-all", with_param: true).upcase.split("|").compact options.deep_merge!(input: {filter: {filter_tags: {any: tags}}}) people.filter_tags_all(tags).tap do |filtered| msg = "Filtered #{filtered.count} people (out of #{people.count}) with 'all' filter_tags #{tags}" session.log(:info) { msg } end end desc = "only those that have ANY the specified tags separated by '|'" filters.add("-filter-tags-any", desc) do |people, session, options| tags = SCR.get_arg("-filter-tags-any", with_param: true).upcase.split("|").compact options.deep_merge!(input: {filter: {filter_tags_any: tags}}) people.filter_tags_any(tags).tap do |filtered| msg = "Filtered #{filtered.count} people (out of #{people.count}) with 'any' filter_tags #{tags}" session.log(:info) { msg } end end desc = "only those that have ANY tag in the specified subtrees separated by '|'" filters.add("-filter-tags-tree", desc) do |people, session, options| top_tags = SCR.get_arg("-filter-tags-tree", with_param: true).upcase.split("|").compact tags = top_tags.each_with_object([]) do |top, tgs| tgs.concat(session.tagtree.node(top).tags) end.uniq options.deep_merge!(input: {filter: {filter_tags: {tree: top_tags, any: tags}}}) people.filter_tags_any(tags).tap do |filtered| msg = "Filtered #{filtered.count} people (out of #{people.count})" msg << " with 'any' filter_tags in subtrees #{top_tags}" session.log(:info) { msg } end end filters.add("-schema-id", "only those with the specified schema id or name") do |people, session, options| sch_name = SCR.get_arg("-schema-id", with_param: true) sch_id = session.schemas.to_id(sch_name) unless sch_id msg = "You need to specify a correct schema id or name. " msg << "'#{sch_name}' does not exist. Correct options are: " msg << session.schemas.map(&:name).join(", ") session.log(:error) { msg } exit(1) end options.deep_merge!(people: {filter: {details: {schema_id: sch_id}}}) session.log(:info) { "Filtering people records with schema #{session.schemas.to_name(sch_id)}" } people.select do |person| person.details && (person.details.schema_id == sch_id) end.then do |filtered| session.log(:info) { "Filtered #{filtered.count} people out of #{people.count} total" } people.newFrom filtered end end end end