module Eco module API class UseCases class DefaultCases class CreateCase < DefaultCase def process @cases.define("create", type: :sync) do |entries, people, session, options, usecase| creation = session.job_group("main").new("create", usecase: usecase, type: :create, sets: [:core, :details, :account]) strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict)) pgs = session.policy_groups entries.each.with_index do |entry, i| if person = people.find(entry, strict: strict_search) session.logger.error("Entry(#{i}) - this person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}") else person = session.new_person core_excluded = ["supervisor_id"] ini_tags = person.filter_tags || [] entry.set_core(person, exclude: core_excluded) if session.tagtree person.filter_tags = session.tagtree.user_tags( initial: ini_tags, final: person.filter_tags, preserve_custom: true, add_custom: true ) end entry.set_details(person) unless options.dig(:exclude, :details) unless options.dig(:exclude, :account) ini_pg_ids = person.account&.policy_group_ids || [] entry.set_account(person) person.account&.send_invites = options[:send_invites] if options.key?(:send_invites) person.account.policy_group_ids = pgs.user_pg_ids( initial: ini_pg_ids, final: person.account.policy_group_ids ) person.account.permissions_custom = session.new_preset(person) if session.tagtree person.account.default_tag = session.tagtree.default_tag(*person.filter_tags) else tags = person.filter_tags person.account.default_tag = tags.first unless tags.length > 1 end end creation.add(person) end end end end end end end end end