Skip to content

Commit 2e54ffa

Browse files
committed
Implement Contact sync to Salesforce
1 parent 6e68af6 commit 2e54ffa

5 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module Salesforce
4+
class ContactSyncJob < SalesforceSyncJob
5+
MODEL_CLASS = Salesforce::Contact
6+
7+
def perform(school_id:)
8+
school = ::School.find(school_id)
9+
10+
sf_contact = Salesforce::Contact.find_by(pi_accounts_unique_id__c: school.creator_id)
11+
raise SalesforceRecordNotFound, "Contact not found for creator_id: #{school.creator_id}" unless sf_contact
12+
13+
sf_contact.experiencecsagreetouxcontact__c = school.creator_agree_to_ux_contact
14+
sf_contact.save!
15+
end
16+
end
17+
end

app/models/salesforce/contact.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module Salesforce
4+
class Contact < Salesforce::Base
5+
self.table_name = 'salesforce.contact'
6+
self.primary_key = :pi_accounts_unique_id__c
7+
end
8+
end

lib/tasks/salesforce_sync.rake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ namespace :salesforce_sync do
1414
Salesforce::RoleSyncJob.perform_later(role_id:)
1515
end
1616
end
17+
18+
desc 'Sync creator_agree_to_ux_contact for all Schools to Salesforce Contact'
19+
task contact: :environment do
20+
School.pluck(:id).each do |school_id|
21+
Salesforce::ContactSyncJob.perform_later(school_id:)
22+
end
23+
end
1724
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
FactoryBot.define do
4+
factory(:salesforce_contact, class: 'Salesforce::Contact') do
5+
pi_accounts_unique_id__c { SecureRandom.uuid }
6+
end
7+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe Salesforce::ContactSyncJob do
6+
subject(:perform_job) { described_class.perform_now(school_id: school.id) }
7+
8+
let(:school) { create(:school, creator_agree_to_ux_contact: true) }
9+
let!(:sf_contact) { create(:salesforce_contact, pi_accounts_unique_id__c: school.creator_id) }
10+
11+
it 'sets experiencecsagreetouxcontact__c from school.creator_agree_to_ux_contact' do
12+
perform_job
13+
expect(sf_contact.reload.experiencecsagreetouxcontact__c).to be(true)
14+
end
15+
16+
it 'saves the contact' do
17+
expect { perform_job }.not_to raise_error
18+
end
19+
20+
context 'when the Contact is not found in Salesforce' do
21+
before { sf_contact.destroy }
22+
23+
it 'retries the job' do
24+
expect { perform_job }.to have_enqueued_job(described_class).with(school_id: school.id)
25+
end
26+
end
27+
28+
context 'when the Salesforce contact fails to save' do
29+
let(:sf_contact_double) { instance_double(Salesforce::Contact) }
30+
31+
before do
32+
allow(Salesforce::Contact).to receive(:find_by)
33+
.with(pi_accounts_unique_id__c: school.creator_id)
34+
.and_return(sf_contact_double)
35+
allow(sf_contact_double).to receive(:experiencecsagreetouxcontact__c=)
36+
allow(sf_contact_double).to receive(:save!).and_raise(ActiveRecord::RecordInvalid)
37+
end
38+
39+
it 'raises an error' do
40+
expect { perform_job }.to raise_error(ActiveRecord::RecordInvalid)
41+
end
42+
end
43+
44+
context 'when SALESFORCE_ENABLED is false' do
45+
around do |example|
46+
ClimateControl.modify(SALESFORCE_ENABLED: 'false') do
47+
example.run
48+
end
49+
end
50+
51+
it 'discards the job without syncing' do
52+
sf_contact.update!(experiencecsagreetouxcontact__c: false)
53+
perform_job
54+
expect(sf_contact.reload.experiencecsagreetouxcontact__c).to be(false)
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)