require 'active_support/core_ext/object' require 'real_page/validator' require 'real_page/utils/snowflake_event_tracker' module RealPage module Validator # Sends parased response's roommate and prospect data to Snowflake via event tracker class ProspectsData def initialize(response, request_params, request_name) @response = response @request_params = request_params @guest_cards = guest_cards end def validate! return if guest_cards.blank? send_data_to_snowflake end private attr_reader :response, :request_params, :guest_cards def send_data_to_snowflake guest_cards.each do |guest_card| prospects_of_current_guest_card = prospects(guest_card) send_roommates_data_to_snowflake(prospects_of_current_guest_card) send_prospects_data_to_snowflake(prospects_of_current_guest_card, guest_card) end end def send_roommates_data_to_snowflake(prospects) prospects.each do |prospect| Utils::SnowflakeEventTracker.track_pms_resident_event( resident_type: 'ROOMMATE', request_params: request_params, first_name_present: !prospect['FirstName'].blank?, last_name_present: !prospect['LastName'].blank?, email_present: !prospect['Email'].blank?, phones_count: phones_count(prospect) ) end end def send_prospects_data_to_snowflake(prospects, guest_card) prospects.each do |prospect| Utils::SnowflakeEventTracker.track_pms_prospect_event( resident_type: 'ROOMMATE', request_params: request_params, contact_date: DateTime.parse(guest_card['DateContact']).to_time, contact_source: guest_card['PrimaryLeadSource'], remote_prospect_id: guest_card['GuestCardID'] ) end end def guest_cards guest_cards_hash = parsed_response.dig('GuestCards', 'GuestCard') return [] if guest_cards_hash.nil? return [guest_cards_hash] unless guest_cards_hash.is_a?(Array) guest_cards_hash end def prospects(guest_card) prospects_hash = guest_card.dig('Prospects', 'Prospect') return [] if prospects_hash.nil? return [prospects_hash] unless prospects_hash.is_a?(Array) prospects_hash end def parsed_response body = response['s:Envelope']['s:Body'] response_key = body.keys.detect { |key| key !~ /^xmlns/ } contents_response = body.fetch(response_key) result_key = contents_response.keys.detect { |key| key !~ /^xmlns/ } contents_result = contents_response.fetch(result_key) result = contents_result.values.first end def phones_count(prospect) phone_numbers_hash = prospect.dig('Numbers', 'PhoneNumber') return 0 if phone_numbers_hash.blank? if !phone_numbers_hash.is_a?(Array) return 0 if phone_numbers_hash['Number'].blank? return 1 end phone_numbers_hash.select { |phone_number| !phone_number['Number'].blank? }.length end end end end