# frozen_string_literal: true
# Imports Atom custom_fields rows, collapsing duplicates, and attaches each
# CustomField to every MboProfile of the CompanyProfile referenced by parent_id.
#
# Grouping, CustomField upsert and validation-option/mapper reconciliation live in
# Maintenance::Atom::CustomFieldGroupImportable; this task only supplies the
# attachment strategy: parent_id is a CompanyProfile primary key.
#
# Expected CSV headers:
# name, validation_required, validation_regex,
# inclusion_list_validation_json, code_labels_json, auto_import_options,
# legacy_scope, legacy_agent_port, mapper_key,
# parent_id, created_at, updated_at
#
# Every parent_id references a single CompanyProfile; the field attaches to that
# profile's MboProfiles. A dedup group aggregates all parent_ids seen across its rows.
# Rows whose parent_id matches no CompanyProfile are logged and skipped — those companies exist locally
# under their own id and are picked up by
# Maintenance::Atom::ImportCustomFieldsByCompanyGuidTask instead.
# legacy_scope carries the cost-centre type (CCV/CCF); see
# CustomFieldImportable::CC_TYPE_TO_LEGACY_SCOPE. legacy_agent_port is required
# for CCF (PROFILE-scoped) fields. mapper_key is the concatenated
# "#{key}#{separator}" that becomes the field's AMADEUS Mapper.
class Maintenance::Atom::ImportCustomFieldsDedupedTask < MaintenanceTasks::Task
include DatadogTrace
include Maintenance::Atom::CustomFieldGroupImportable
csv_collection
report_on(StandardError)
private
def attach_parents(custom_field, group)
group['parent_ids'].each { |parent_id| attach_to_parent_id(custom_field, parent_id) }
end
def attach_to_parent_id(custom_field, parent_id)
return if parent_id.blank?
company_profile = CompanyProfile.find_by(id: parent_id)
return log_missing_company_profile(custom_field, parent_id) unless company_profile
attach_to_mbo_profiles(custom_field, company_profile)
end
def log_missing_company_profile(custom_field, parent_id)
Rails.logger.warn(
"CustomField #{custom_field.id}: parent CompanyProfile #{parent_id} not found, " \
'skipping mbo_profile attachment'
)
nil
end
end