Skip to content

Commit b7f9eed

Browse files
committed
Update Field Names for Salesforce Production
This commit changes a number of field names to match those requested by the Salesforce team. Also, we now pass a flag into School#do_salesforce_sync to indicate whether this is a new school (create) or an existing school (update). This allows us to set the `status__c` field of the school to "New" if it's a new school, otherwise leave it alone.
1 parent d8d5c92 commit b7f9eed

8 files changed

Lines changed: 45 additions & 14 deletions

File tree

app/jobs/salesforce/contact_sync_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def perform(school_id:)
1010
sf_contact = Salesforce::Contact.find_by(pi_accounts_unique_id__c: school.creator_id)
1111
raise SalesforceRecordNotFound, "Contact not found for creator_id: #{school.creator_id}" unless sf_contact
1212

13-
sf_contact.editoragreetouxcontact__c = school.creator_agree_to_ux_contact
13+
sf_contact.editor_consent_to_ux_contact__c = school.creator_agree_to_ux_contact
1414
sf_contact.save!
1515
end
1616

app/jobs/salesforce/role_sync_job.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def perform(role_id:)
2020

2121
sf_role = Salesforce::Role.find_or_initialize_by(affiliation_id__c: role_id)
2222
sf_role.attributes = sf_role_attributes(role:)
23+
24+
# We also have a field on the Contact_Editor_Affiliation in Salesforce
25+
# called Editor_Type__c - this is mapped to the value of role.school.user_origin
26+
# If, for any reason, we can't get that, we fall back to the School model's default
27+
# value for user_origin. ::School.new never persists to the DB.
28+
sf_role.editor_type__c = role.school&.user_origin || ::School.new.user_origin
29+
2330
sf_role.save!
2431
end
2532

app/jobs/salesforce/school_sync_job.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,34 @@ class SchoolSyncJob < SalesforceSyncJob
1212
addressline2__c: :address_line_2,
1313
editormunicipality__c: :municipality,
1414
editoradministrativearea__c: :administrative_area,
15+
editortype__c: :user_origin,
1516
postcode__c: :postal_code,
1617
countrycode__c: :country_code,
1718
verifiedat__c: :verified_at,
1819
createdat__c: :created_at,
1920
updatedat__c: :updated_at,
2021
rejectedat__c: :rejected_at,
2122
website__c: :website,
22-
userorigin__c: :user_origin,
2323
districtnamesupplied__c: :district_name,
2424
ncesid__c: :district_nces_id,
2525
schoolrollnumber__c: :school_roll_number
2626
}.freeze
2727

28-
def perform(school_id:)
28+
def perform(school_id:, is_create: false)
2929
school = ::School.find(school_id)
3030

3131
sf_school = Salesforce::School.find_or_initialize_by(editoruuid__c: school_id)
3232
sf_school.attributes = sf_school_attributes(school:)
33+
sf_school.status__c = 'New' if is_create
34+
3335
sf_school.save!
3436
end
3537

3638
private
3739

3840
def sf_school_attributes(school:)
3941
mapped_attributes(school:).to_h do |sf_field, value|
40-
value = 'for_education' if sf_field == :userorigin__c && value.nil?
42+
value = ::School.new.user_origin if sf_field == :editortype__c && value.nil?
4143
value = truncate_value(sf_field:, value:) if value.is_a?(String)
4244

4345
[sf_field, value]

app/models/school.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class School < ApplicationRecord
5555

5656
after_create :generate_code!
5757

58-
after_commit :do_salesforce_sync, on: %i[create update], if: -> { FeatureFlags.salesforce_sync? }
58+
after_commit -> { do_salesforce_sync(is_create: true) }, on: :create, if: -> { FeatureFlags.salesforce_sync? }
59+
after_commit -> { do_salesforce_sync(is_create: false) }, on: :update, if: -> { FeatureFlags.salesforce_sync? }
5960

6061
def self.find_for_user!(user)
6162
school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id, rejected_at: nil)
@@ -169,8 +170,8 @@ def format_uk_postal_code
169170
self.postal_code = "#{cleaned_postal_code[0..-4]} #{cleaned_postal_code[-3..]}"
170171
end
171172

172-
def do_salesforce_sync
173-
Salesforce::SchoolSyncJob.perform_later(school_id: id)
173+
def do_salesforce_sync(is_create:)
174+
Salesforce::SchoolSyncJob.perform_later(school_id: id, is_create:)
174175
Salesforce::ContactSyncJob.perform_later(school_id: id)
175176
end
176177
end

spec/jobs/salesforce/contact_sync_job_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
ClimateControl.modify(SALESFORCE_ENABLED: 'true') { example.run }
1313
end
1414

15-
it 'sets editoragreetouxcontact__c from school.creator_agree_to_ux_contact' do
15+
it 'sets editor_consent_to_ux_contact__c from school.creator_agree_to_ux_contact' do
1616
perform_job
17-
expect(sf_contact.reload.editoragreetouxcontact__c).to be(true)
17+
expect(sf_contact.reload.editor_consent_to_ux_contact__c).to be(true)
1818
end
1919

2020
it 'saves the contact' do
@@ -36,7 +36,7 @@
3636
allow(Salesforce::Contact).to receive(:find_by)
3737
.with(pi_accounts_unique_id__c: school.creator_id)
3838
.and_return(sf_contact_double)
39-
allow(sf_contact_double).to receive(:editoragreetouxcontact__c=)
39+
allow(sf_contact_double).to receive(:editor_consent_to_ux_contact__c=)
4040
allow(sf_contact_double).to receive(:save!).and_raise(ActiveRecord::RecordInvalid)
4141
end
4242

@@ -53,9 +53,9 @@
5353
end
5454

5555
it 'discards the job without syncing' do
56-
sf_contact.update!(editoragreetouxcontact__c: false)
56+
sf_contact.update!(editor_consent_to_ux_contact__c: false)
5757
perform_job
58-
expect(sf_contact.reload.editoragreetouxcontact__c).to be(false)
58+
expect(sf_contact.reload.editor_consent_to_ux_contact__c).to be(false)
5959
end
6060
end
6161

spec/jobs/salesforce/role_sync_job_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
"Expected #{sf_field} to equal role.#{role_field}"
2323
end
2424
end
25+
26+
it 'sets editor_type__c from the school user_origin' do
27+
sf_role = Salesforce::Role.find_by(affiliation_id__c: role.id)
28+
expect(sf_role.editor_type__c).to eq(role.school.user_origin)
29+
end
2530
end
2631

2732
context 'when the Salesforce role fails to save' do
@@ -30,6 +35,7 @@
3035
before do
3136
allow(Salesforce::Role).to receive(:find_or_initialize_by).with(affiliation_id__c: role.id).and_return(sf_role)
3237
allow(sf_role).to receive(:attributes=)
38+
allow(sf_role).to receive(:editor_type__c=)
3339
allow(sf_role).to receive(:save!).and_raise(ActiveRecord::RecordInvalid)
3440
end
3541

spec/jobs/salesforce/school_sync_job_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
subject(:perform_job) { described_class.perform_now(school_id: school.id) }
77

88
let(:school) { create(:school) }
9+
let(:perform_job_as_create) { described_class.perform_now(school_id: school.id, is_create: true) }
910

1011
around do |example|
1112
ClimateControl.modify(SALESFORCE_ENABLED: 'true') { example.run }
@@ -23,6 +24,11 @@
2324
end
2425
end
2526

27+
it 'does not set status__c when is_create is false' do
28+
sf_school = Salesforce::School.find_by(editoruuid__c: school.id)
29+
expect(sf_school.status__c).to be_nil
30+
end
31+
2632
context 'when an address field is very long' do
2733
let(:school) { create(:school, address_line_1: '❌' * 300) }
2834

@@ -52,6 +58,15 @@
5258
end
5359
end
5460

61+
context 'when is_create is true' do
62+
before { perform_job_as_create }
63+
64+
it 'sets status__c to "New"' do
65+
sf_school = Salesforce::School.find_by(editoruuid__c: school.id)
66+
expect(sf_school.status__c).to eq('New')
67+
end
68+
end
69+
5570
context 'when the Salesforce school fails to save' do
5671
let(:sf_school) { instance_double(Salesforce::School) }
5772

spec/models/school_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@
620620
end
621621

622622
it 'enqueues Salesforce::SchoolSyncJob on create' do
623-
expect { create(:school) }.to have_enqueued_job(Salesforce::SchoolSyncJob)
623+
expect { create(:school) }.to have_enqueued_job(Salesforce::SchoolSyncJob).with(hash_including(is_create: true))
624624
end
625625

626626
it 'enqueues Salesforce::ContactSyncJob on create' do
@@ -629,7 +629,7 @@
629629

630630
it 'enqueues Salesforce::SchoolSyncJob on update' do
631631
school = create(:school)
632-
expect { school.update!(name: 'Updated Name') }.to have_enqueued_job(Salesforce::SchoolSyncJob)
632+
expect { school.update!(name: 'Updated Name') }.to have_enqueued_job(Salesforce::SchoolSyncJob).with(hash_including(is_create: false))
633633
end
634634

635635
it 'enqueues Salesforce::ContactSyncJob on update' do

0 commit comments

Comments
 (0)