Skip to content

Commit

Permalink
Merge pull request #35534 from code-dot-org/ha/cr-process-country
Browse files Browse the repository at this point in the history
CRv2: Process country attribute
  • Loading branch information
hacodeorg committed Jun 30, 2020
2 parents dbbf101 + 20609fb commit 3871938
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dashboard/app/models/contact_rollups_processed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def self.import_from_raw_table(batch_size = DEFAULT_BATCH_SIZE)
processed_contact_data.merge! extract_roles(contact_data)
processed_contact_data.merge! extract_state(contact_data)
processed_contact_data.merge! extract_city(contact_data)
processed_contact_data.merge! extract_country(contact_data)
processed_contact_data.merge! extract_updated_at(contact_data)
valid_contacts += 1
rescue StandardError
Expand Down Expand Up @@ -331,6 +332,15 @@ def self.extract_city(contact_data)
form_geo_city.nil? ? {} : {city: form_geo_city}
end

def self.extract_country(contact_data)
# Priority: user country > form geo country
user_country = extract_field_latest_value contact_data, 'dashboard.users', 'country'
return {country: user_country} if user_country

form_geo_country = extract_field_latest_value contact_data, 'pegasus.form_geos', 'country'
form_geo_country.nil? ? {} : {country: form_geo_country}
end

# Extract the latest value of a field in a source table from contact data.
# @param contact_data [Hash] output of the +parse_contact_data+ method
# @param table [String]
Expand Down
2 changes: 2 additions & 0 deletions dashboard/test/models/contact_rollups_pardot_memory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class ContactRollupsPardotMemoryTest < ActiveSupport::TestCase
'roles' => 'Form Submitter',
'state' => 'Washington',
'city' => 'Seattle',
'country' => 'United States',
}
refute ContactRollupsPardotMemory.find_by_email(contact.email)
PardotV2.expects(:submit_batch_request).once.returns([])
Expand All @@ -146,6 +147,7 @@ class ContactRollupsPardotMemoryTest < ActiveSupport::TestCase
'db_Roles_0' => 'Form Submitter',
'db_State' => 'Washington',
'db_City' => 'Seattle',
'db_Country' => 'United States',
}
assert_equal expected_data_synced, record[:data_synced]
end
Expand Down
47 changes: 47 additions & 0 deletions dashboard/test/models/contact_rollups_processed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class ContactRollupsProcessedTest < ActiveSupport::TestCase
once.returns({})
ContactRollupsProcessed.expects(:extract_city).
once.returns({})
ContactRollupsProcessed.expects(:extract_country).
once.returns({})
ContactRollupsProcessed.expects(:extract_updated_at).
once.returns({})

Expand Down Expand Up @@ -480,6 +482,51 @@ class ContactRollupsProcessedTest < ActiveSupport::TestCase
end
end

test 'extract_country' do
base_time = Time.now.utc
form_geos_input = {
'pegasus.form_geos' => {
'country' => [
{'value' => 'Canada', 'data_updated_at' => base_time - 1.day},
{'value' => 'United States', 'data_updated_at' => base_time},
]
}
}
users_input = {
'dashboard.users' => {
'country' => [
{'value' => 'United Kingdom', 'data_updated_at' => base_time - 1.day},
{'value' => 'Italy', 'data_updated_at' => base_time},
]
}
}

tests = [
{
input: {}, expected_output: {}
},
# data come from the same table, the most recent value wins
{
input: form_geos_input,
expected_output: {country: 'United States'}
},
{
input: users_input,
expected_output: {country: 'Italy'}
},
# users data has higher priority than form_geos data
{
input: form_geos_input.merge(users_input),
expected_output: {country: 'Italy'}
}
]

tests.each_with_index do |test, index|
output = ContactRollupsProcessed.extract_country test[:input]
assert_equal test[:expected_output], output, "Test index #{index} failed"
end
end

test 'extract_hoc_organizer_years' do
contact_data = {
'pegasus.forms' => {
Expand Down
2 changes: 2 additions & 0 deletions lib/test/cdo/contact_rollups/test_pardot_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def test_convert_to_pardot_prospect_single_value_attributes
form_roles: 'engineer,teacher',
state: 'Washington',
city: 'Seattle',
country: 'United States',
},
expected_output: {
email: 'test0@domain.com',
Expand All @@ -215,6 +216,7 @@ def test_convert_to_pardot_prospect_single_value_attributes
db_Form_Roles: 'engineer,teacher',
db_State: 'Washington',
db_City: 'Seattle',
db_Country: 'United States',
}
},
{
Expand Down

0 comments on commit 3871938

Please sign in to comment.