-
Notifications
You must be signed in to change notification settings - Fork 5
Sync Schools and Roles to Salesforce on Create and Update #677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fspeirs
wants to merge
11
commits into
main
Choose a base branch
from
fs-sync-schools-to-salesforce
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a47443
Set up Salesforce Connect database connection
fspeirs ce019c9
Add Salesforce sync base infrastructure
fspeirs 1959510
Add Heroku Connect to Docker Compose
fspeirs 79f0dc3
Implement School sync to Salesforce
fspeirs 608c3dd
Implement Role sync to Salesforce
fspeirs 29b45ca
Implement Contact sync to Salesforce
fspeirs a1df8ae
Set up CI for Salesforce Connect
fspeirs 3ae68ff
Use find_each in Salesforce backfill tasks and skip student roles
fspeirs f79901a
Fall back to main DB for salesforce_connect when SALESFORCE_CONNECT_D…
fspeirs b6bd57b
Skip Salesforce integration specs when Heroku Connect DB is unavailable
fspeirs 6e54d37
Change `ExperienceCSAgreeToUXContact` to `Editor`
fspeirs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class ContactSyncJob < SalesforceSyncJob | ||
| MODEL_CLASS = Salesforce::Contact | ||
|
|
||
| def perform(school_id:) | ||
| school = ::School.find(school_id) | ||
|
|
||
| sf_contact = Salesforce::Contact.find_by(pi_accounts_unique_id__c: school.creator_id) | ||
| raise SalesforceRecordNotFound, "Contact not found for creator_id: #{school.creator_id}" unless sf_contact | ||
|
|
||
| sf_contact.editoragreetouxcontact__c = school.creator_agree_to_ux_contact | ||
| sf_contact.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def concurrency_key_id = arguments.first.with_indifferent_access[:school_id] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class RoleSyncJob < SalesforceSyncJob | ||
| MODEL_CLASS = Salesforce::Role | ||
|
|
||
| FIELD_MAPPINGS = { | ||
| affiliation_id__c: :id, | ||
| contact__r__pi_accounts_unique_id__c: :user_id, | ||
| editor__r__editoruuid__c: :school_id, | ||
| roletype__c: :role, | ||
| createdat__c: :created_at, | ||
| updatedat__c: :updated_at | ||
| }.freeze | ||
|
|
||
| def perform(role_id:) | ||
| role = ::Role.find(role_id) | ||
|
|
||
| return if role.student? | ||
|
|
||
| sf_role = Salesforce::Role.find_or_initialize_by(affiliation_id__c: role_id) | ||
| sf_role.attributes = sf_role_attributes(role:) | ||
| sf_role.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sf_role_attributes(role:) | ||
| mapped_attributes(role:).to_h do |sf_field, value| | ||
| value = truncate_value(sf_field:, value:) if value.is_a?(String) | ||
|
|
||
| [sf_field, value] | ||
| end | ||
| end | ||
|
|
||
| def mapped_attributes(role:) | ||
| FIELD_MAPPINGS.transform_values do |role_field| | ||
| role.send(role_field) | ||
| end | ||
| end | ||
|
|
||
| def concurrency_key_id = arguments.first.with_indifferent_access[:role_id] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class SalesforceSyncJob < ApplicationJob | ||
| include GoodJob::ActiveJobExtensions::Concurrency | ||
|
|
||
| # Serialise concurrent performs for the same record (same class + record ID) | ||
| # to prevent TOCTOU races on find_or_initialize_by + save!, while allowing | ||
| # jobs for different records to run fully in parallel. | ||
| good_job_control_concurrency_with( | ||
| perform_limit: 1, | ||
| key: -> { "#{self.class.name}/#{concurrency_key_id}" } | ||
| ) | ||
fspeirs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| class SalesforceRecordNotFound < StandardError | ||
| end | ||
|
|
||
| class SkipBecauseSalesforceIsDisabled < StandardError | ||
| end | ||
|
|
||
| discard_on SkipBecauseSalesforceIsDisabled | ||
|
|
||
| retry_on SalesforceRecordNotFound, wait: :polynomially_longer, attempts: 10 | ||
|
|
||
| queue_as :salesforce_sync | ||
|
|
||
| before_perform do |_job| | ||
| salesforce_enabled = FeatureFlags.salesforce_sync? | ||
| raise SkipBecauseSalesforceIsDisabled, 'SALESFORCE_ENABLED is not true.' unless salesforce_enabled | ||
| end | ||
fspeirs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def perform(*) | ||
| raise NotImplementedError, 'Subclasses must implement perform' | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def concurrency_key_id | ||
| raise NotImplementedError, "#{self.class.name} must implement concurrency_key_id" | ||
| end | ||
|
|
||
| def truncate_value(sf_field:, value:) | ||
| column = self.class::MODEL_CLASS.column_for_attribute(sf_field) | ||
| return value if column.limit.nil? | ||
|
|
||
| value.truncate(column.limit, omission: '…') | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class SchoolSyncJob < SalesforceSyncJob | ||
| MODEL_CLASS = Salesforce::School | ||
|
|
||
| FIELD_MAPPINGS = { | ||
| editoruuid__c: :id, | ||
| name: :name, | ||
| editorreference__c: :reference, | ||
| addressline1__c: :address_line_1, | ||
| addressline2__c: :address_line_2, | ||
| editormunicipality__c: :municipality, | ||
| editoradministrativearea__c: :administrative_area, | ||
| postcode__c: :postal_code, | ||
| countrycode__c: :country_code, | ||
| verifiedat__c: :verified_at, | ||
| createdat__c: :created_at, | ||
| updatedat__c: :updated_at, | ||
| rejectedat__c: :rejected_at, | ||
| website__c: :website, | ||
| userorigin__c: :user_origin, | ||
| districtnamesupplied__c: :district_name, | ||
| ncesid__c: :district_nces_id, | ||
| schoolrollnumber__c: :school_roll_number | ||
| }.freeze | ||
|
|
||
| def perform(school_id:) | ||
| school = ::School.find(school_id) | ||
|
|
||
| sf_school = Salesforce::School.find_or_initialize_by(editoruuid__c: school_id) | ||
| sf_school.attributes = sf_school_attributes(school:) | ||
| sf_school.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sf_school_attributes(school:) | ||
| mapped_attributes(school:).to_h do |sf_field, value| | ||
| value = 'for_education' if sf_field == :userorigin__c && value.nil? | ||
| value = truncate_value(sf_field:, value:) if value.is_a?(String) | ||
|
|
||
| [sf_field, value] | ||
| end | ||
| end | ||
|
|
||
| def mapped_attributes(school:) | ||
| FIELD_MAPPINGS.transform_values do |school_field| | ||
| school.send(school_field) | ||
| end | ||
| end | ||
|
|
||
| def concurrency_key_id = arguments.first.with_indifferent_access[:school_id] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class Base < ApplicationRecord | ||
| self.abstract_class = true | ||
|
|
||
| connects_to database: { writing: :salesforce_connect } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class Contact < Salesforce::Base | ||
| self.table_name = 'salesforce.contact' | ||
| self.primary_key = :pi_accounts_unique_id__c | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class Role < Salesforce::Base | ||
| self.table_name = 'salesforce.contact_editor_affiliation__c' | ||
| self.primary_key = :affiliation_id__c | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class School < Salesforce::Base | ||
| self.table_name = 'salesforce.editor__c' | ||
| self.primary_key = :editoruuid__c | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.