Skip to content
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

feat(salesforce): Added customers webhooks #2783

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/jobs/send_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SendWebhookJob < ApplicationJob
'events.errors' => Webhooks::Events::ValidationErrorsService,
'fee.created' => Webhooks::Fees::PayInAdvanceCreatedService,
'fee.tax_provider_error' => Webhooks::Integrations::Taxes::FeeErrorService,
'customer.created' => Webhooks::Customers::CreatedService,
'customer.updated' => Webhooks::Customers::UpdatedService,
'customer.accounting_provider_created' => Webhooks::Integrations::AccountingCustomerCreatedService,
'customer.accounting_provider_error' => Webhooks::Integrations::AccountingCustomerErrorService,
'customer.crm_provider_created' => Webhooks::Integrations::CrmCustomerCreatedService,
Expand Down
1 change: 1 addition & 0 deletions app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def track_customer_created(customer)
organization_id: customer.organization_id
}
)
SendWebhookJob.perform_later('customer.created', customer)
end

def should_create_billing_configuration?(billing, customer)
Expand Down
2 changes: 1 addition & 1 deletion app/services/customers/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def call
customer: result.customer,
new_customer: false
)

SendWebhookJob.perform_later('customer.updated', customer)
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
Expand Down
29 changes: 29 additions & 0 deletions app/services/webhooks/customers/created_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Webhooks
module Customers
class CreatedService < Webhooks::BaseService
private

def current_organization
@current_organization ||= object.organization
end

def object_serializer
::V1::CustomerSerializer.new(
object,
root_name: 'customer',
includes: %i[integration_customers]
)
end

def webhook_type
'customer.created'
end

def object_type
'customer'
end
end
end
end
29 changes: 29 additions & 0 deletions app/services/webhooks/customers/updated_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Webhooks
module Customers
class UpdatedService < Webhooks::BaseService
private

def current_organization
@current_organization ||= object.organization
end

def object_serializer
::V1::CustomerSerializer.new(
object,
root_name: 'customer',
includes: %i[integration_customers]
)
end

def webhook_type
'customer.updated'
end

def object_type
'customer'
end
end
end
end
38 changes: 38 additions & 0 deletions spec/jobs/send_webhook_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,44 @@
end
end

context 'when webhook type is customer.created' do
let(:webhook_service) { instance_double(Webhooks::Customers::CreatedService) }
let(:customer) { create(:customer) }

before do
allow(Webhooks::Customers::CreatedService).to receive(:new)
.with(object: customer, options: {})
.and_return(webhook_service)
allow(webhook_service).to receive(:call)
end

it 'calls the created webhook service' do
send_webhook_job.perform_now('customer.created', customer)

expect(Webhooks::Customers::CreatedService).to have_received(:new)
expect(webhook_service).to have_received(:call)
end
end

context 'when webhook type is customer.updated' do
let(:webhook_service) { instance_double(Webhooks::Customers::UpdatedService) }
let(:customer) { create(:customer) }

before do
allow(Webhooks::Customers::UpdatedService).to receive(:new)
.with(object: customer, options: {})
.and_return(webhook_service)
allow(webhook_service).to receive(:call)
end

it 'calls the updated webhook service' do
send_webhook_job.perform_now('customer.updated', customer)

expect(Webhooks::Customers::UpdatedService).to have_received(:new)
expect(webhook_service).to have_received(:call)
end
end

context 'when webhook_type is payment_request.created' do
let(:webhook_service) { instance_double(Webhooks::PaymentRequests::CreatedService) }
let(:payment_request) { create(:payment_request) }
Expand Down
16 changes: 16 additions & 0 deletions spec/services/customers/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

before do
allow(SegmentTrackJob).to receive(:perform_later)
allow(SendWebhookJob).to receive(:perform_later)
allow(CurrentContext).to receive(:source).and_return('api')
end

Expand Down Expand Up @@ -91,6 +92,15 @@
)
end

it 'calls SendWebhookJob with customer.created' do
customer = customers_service.create_from_api(
organization:,
params: create_args
).customer

expect(SendWebhookJob).to have_received(:perform_later).with('customer.created', customer)
end

it 'calls SegmentTrackJob' do
customer = customers_service.create_from_api(
organization:,
Expand Down Expand Up @@ -994,6 +1004,7 @@

before do
allow(SegmentTrackJob).to receive(:perform_later)
allow(SendWebhookJob).to receive(:perform_later)
allow(CurrentContext).to receive(:source).and_return('graphql')
end

Expand Down Expand Up @@ -1022,6 +1033,11 @@
end
end

it 'calls SendWebhookJob with customer.created' do
customer = customers_service.create(**create_args).customer
expect(SendWebhookJob).to have_received(:perform_later).with('customer.created', customer)
end

it 'calls SegmentTrackJob' do
customer = customers_service.create(**create_args).customer

Expand Down
6 changes: 4 additions & 2 deletions spec/services/customers/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
}
end

it 'updates a customer' do
result = customers_service.call
it 'updates a customer and calls SendWebhookJob' do
allow(SendWebhookJob).to receive(:perform_later)

result = customers_service.call
updated_customer = result.customer
aggregate_failures do
expect(updated_customer.name).to eq(update_args[:name])
Expand All @@ -42,6 +43,7 @@

shipping_address = update_args[:shipping_address]
expect(updated_customer.shipping_city).to eq(shipping_address[:city])
expect(SendWebhookJob).to have_received(:perform_later).with('customer.updated', updated_customer)
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/services/webhooks/customers/created_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Webhooks::Customers::CreatedService do
subject(:webhook_service) { described_class.new(object: customer) }

let(:organization) { create(:organization) }
let(:customer) { create(:customer, organization:) }

describe '.call' do
it_behaves_like 'creates webhook', 'customer.created', 'customer'
end
end
14 changes: 14 additions & 0 deletions spec/services/webhooks/customers/updated_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Webhooks::Customers::UpdatedService do
subject(:webhook_service) { described_class.new(object: customer) }

let(:organization) { create(:organization) }
let(:customer) { create(:customer, organization:) }

describe '.call' do
it_behaves_like 'creates webhook', 'customer.updated', 'customer'
end
end