class ZuoraConnect::ZuoraUser

def self.update_id_response(zuora_user_id, zuora_entity_id, zuora_current_identity, app_instance, permissions)

app_instance is only needed to try to migrate :/
zuora_current_identity comes from session
zuora_user_id/zuora_entity_id both come from cookie or headers
def self.update_id_response(zuora_user_id, zuora_entity_id, zuora_current_identity, app_instance, permissions)
  zuora_user = find_or_create_by!(zuora_user_id: zuora_user_id) do |user|
    user.zuora_identity_response = { zuora_entity_id => zuora_current_identity }
    user.app_permissions = permissions
  end
  zuora_user.zuora_identity_response[zuora_entity_id] = zuora_current_identity
  zuora_user.app_permissions = permissions
  if zuora_user.changed?
    zuora_user.save!
  end
  zuora_user
# NOTE(hartley): this rescue is deprecated. We should not be migrating in production
rescue ActiveRecord::StatementInvalid => e
  raise unless e.message.include?('PG::UndefinedTable') && e.message.include?('zuora_users')
  ZuoraConnect.logger.fatal('Error querying zuora_users table: attempting migration to recover')
  app_instance.apartment_switch(nil, true)
  retry
end