# frozen_string_literal: true module Usps module Imis class Mapper FIELD_MAPPING = { vessel_examiner: %w[ABC_ASC_Individual_Demog Vol_Vessel_Examiner], mm: %w[ABC_ASC_Individual_Demog TotMMS], mm_updated: %w[ABC_ASC_Individual_Demog MMS_Updated] }.freeze attr_reader :api def initialize(api = nil) @api = api || Api.new end # Update a member's data on multiple affected business objects by arbitrary field names # Does not require knowing which business object / iMIS-specific field name to use # # Only available for previously-mapped fields # # `data` is a hash of shape { field_key => value } # def update(data) updates = data.each_with_object({}) do |(field_key, value), hash| map_update(field_key) do |business_object_name, field| hash[business_object_name] ||= {} hash[business_object_name][field] = value end end updates.map do |business_object_name, field_updates| api.put_fields(business_object_name, field_updates) end end private def map_update(field_name) if FIELD_MAPPING.key?(field_name.to_sym) business_object_name, field = FIELD_MAPPING[field_name.to_sym] yield(business_object_name, field) else missing_mapping(field_name) end end def missing_mapping(field_name) unless ENV['TESTING'] warn( "Mapper does not know how to handle field \"#{field_name}\".\n\n" \ 'You can use api.put_fields(business_object_name, { field_name => value }) ' \ "if you know the business object and iMIS-specific field name.\n\n" ) end raise( Imis::Error::Mapper, "Unrecognized field: \"#{field_name}\". " \ 'Please report what data you are attempting to work with to ITCom leadership.' ) end end end end