-
Couldn't load subscription status.
- Fork 5
Salesforce integration #577
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SalesforceSyncJob < ApplicationJob | ||
| def perform(school_id) | ||
| school = School.find(school_id) | ||
|
|
||
| account_data = { | ||
| Name: school.name, | ||
| Website: school.website, | ||
| BillingStreet: [school.address_line_1, school.address_line_2].compact.join("\n"), | ||
| BillingCity: school.municipality, | ||
| BillingState: school.administrative_area, | ||
| BillingPostalCode: school.postal_code, | ||
| BillingCountryCode: school.country_code, | ||
| Industry: 'Education' | ||
| } | ||
|
|
||
| client.create('Account', account_data) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def client | ||
| Restforce.new( | ||
| username: ENV.fetch('SALESFORCE_USERNAME'), | ||
| password: ENV.fetch('SALESFORCE_PASSWORD'), | ||
| client_id: ENV.fetch('SALESFORCE_CLIENT_ID'), | ||
| client_secret: ENV.fetch('SALESFORCE_CLIENT_SECRET'), | ||
| host: ENV.fetch('SALESFORCE_HOST'), | ||
| api_version: '57.0' | ||
| ) | ||
| end | ||
|
Comment on lines
+23
to
+32
|
||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -64,6 +64,10 @@ def verify! | |||||
| attempts += 1 | ||||||
| retry | ||||||
| end | ||||||
|
|
||||||
| SalesforceSyncJob.perform_later(id) | ||||||
|
|
||||||
| true | ||||||
|
Comment on lines
+69
to
+70
|
||||||
| true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe SalesforceSyncJob do | ||
| describe '#perform' do | ||
| let(:school) do | ||
| create(:school, | ||
| name: 'West Beverly Hills High School', | ||
| website: 'https://example.com', | ||
| address_line_1: '16711 Mulholland Drive', | ||
| address_line_2: nil, | ||
| municipality: 'Beverly Hills', | ||
| administrative_area: 'California', | ||
| postal_code: '90210', | ||
| country_code: 'US') | ||
| end | ||
|
|
||
| let(:mock_client) { instance_double(Restforce::Client) } | ||
|
|
||
| let(:expected_account_data) do | ||
| { | ||
| Name: 'West Beverly Hills High School', | ||
| Website: 'https://example.com', | ||
| BillingStreet: '16711 Mulholland Drive', | ||
| BillingCity: 'Beverly Hills', | ||
| BillingState: 'California', | ||
| BillingPostalCode: '90210', | ||
| BillingCountryCode: 'US', | ||
| Industry: 'Education' | ||
| } | ||
| end | ||
|
|
||
| before do | ||
| stub_const('ENV', { | ||
| 'SALESFORCE_USERNAME' => 'salesforce-username', | ||
| 'SALESFORCE_PASSWORD' => 'salesforce-password', | ||
| 'SALESFORCE_CLIENT_ID' => 'salesforce-client-id', | ||
| 'SALESFORCE_CLIENT_SECRET' => 'salesforce-client-secret', | ||
| 'SALESFORCE_HOST' => 'example.com' | ||
| }) | ||
|
|
||
| allow(Restforce).to receive(:new).and_return(mock_client) | ||
| allow(mock_client).to receive(:create) | ||
| end | ||
|
|
||
| it 'creates a Salesforce account with correct data' do | ||
| described_class.perform_now(school.id) | ||
|
|
||
| expect(mock_client).to have_received(:create).with('Account', expected_account_data) | ||
| end | ||
|
|
||
| it 'concatenates the address fields' do | ||
| school.update(address_line_2: 'Address line 2') | ||
| expected_data = expected_account_data.merge(BillingStreet: "16711 Mulholland Drive\nAddress line 2") | ||
|
|
||
| described_class.perform_now(school.id) | ||
|
|
||
| expect(mock_client).to have_received(:create).with('Account', expected_data) | ||
| end | ||
|
|
||
| it 'configures Restforce client with correct credentials' do | ||
| described_class.perform_now(school.id) | ||
|
|
||
| expect(Restforce).to have_received(:new).with( | ||
| username: 'salesforce-username', | ||
| password: 'salesforce-password', | ||
| client_id: 'salesforce-client-id', | ||
| client_secret: 'salesforce-client-secret', | ||
| host: 'example.com', | ||
| api_version: '57.0' | ||
| ) | ||
| end | ||
|
|
||
| it 'raises error when school is not found' do | ||
| expect { described_class.perform_now('not-an-id') }.to raise_error(ActiveRecord::RecordNotFound) | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Salesforce API call has no error handling. If the API call fails, the job will raise an exception with no context about which school failed to sync. Consider wrapping this in a rescue block that logs the school_id and re-raises, or let the job framework handle retries with better error context.