require_relative 'base' require 'active_support' require 'active_support/core_ext' require 'yardi/model/prospect' require 'yardi/utils/phone_parser' require 'yardi/validator/prospect_eventing' module Yardi module DocumentParser # Build Prospect objects from prospect search response body. class Prospects < Base private attr_reader :body # @param body [Hash] the body of the XML response parsed # into a Hash # @return [Array] # @raise [Yardi::Error::Base] if the response is invalid def parse_body(body) @body = body prospects end def prospects prospects = result_node['LeadManagement']['Prospects']['Prospect'] prospects = [prospects] unless prospects.is_a?(Array) prospects.map { |prospect| build_prospect(prospect) } end def build_prospect(prospect) customer = [prospect['Customers']['Customer']].flatten.first events = prospect.dig('Events', 'Event') || [] Model::Prospect.new( first_name: customer['Name']&.[]('FirstName'), last_name: customer['Name']&.[]('LastName'), email: customer['Email'], phones: Utils::PhoneParser.parse(customer['Phone']), events: build_events(events), prospect_id: remote_id(customer, 'ProspectID'), tenant_id: remote_id(customer, 'TenantID') ) end def build_events(event_nodes) events_array = event_nodes.is_a?(Array) ? event_nodes : [event_nodes] source = transaction_source(events_array) events_array.map do |e| Model::Event.new( remote_id: e.fetch('EventID', {})['IDValue'], type: e['EventType'], timestamp: e['EventDate'], first_contact: e['FirstContact'] == 'true', transaction_source: source ) end end def transaction_source(event_nodes) transaction_node = event_nodes.detect { |e| e['TransactionSource'].present? } if transaction_node.nil? 'None Given From Yardi' else transaction_node['TransactionSource'] end end def remote_id(customer, id_type) desired_id_node = customer['Identification'].detect do |id_node| id_node['IDType'] == id_type end desired_id_node['IDValue'] end def validator_classes # since leadsender also goes through this flow we want to make this generic/extendable by checking send_prospect_events if @params[:send_prospect_events] [Validator::ProspectEventing] else [] end end def action if @params[:with_or] 'GetYardiGuestActivity_SearchWithOR'.freeze else 'GetYardiGuestActivity_Search'.freeze end end end end end