diff --git a/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb b/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb index bb24b5504..b648b348c 100644 --- a/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb +++ b/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb @@ -3,7 +3,10 @@ class Enterprise::Api::V1::AccountsController < Api::BaseController before_action :check_authorization def subscription - Enterprise::CreateStripeCustomerJob.perform_later(@account) if stripe_customer_id.blank? + if stripe_customer_id.blank? && @account.custom_attributes['is_creating_customer'].blank? + @account.update(custom_attributes: { is_creating_customer: true }) + Enterprise::CreateStripeCustomerJob.perform_later(@account) + end head :no_content end diff --git a/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb b/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb index 57ef52dbb..354aa5a50 100644 --- a/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb +++ b/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb @@ -32,6 +32,17 @@ RSpec.describe 'Enterprise Billing APIs', type: :request do headers: admin.create_new_auth_token, as: :json end.to have_enqueued_job(Enterprise::CreateStripeCustomerJob).with(account) + expect(account.reload.custom_attributes).to eq({ 'is_creating_customer': true }.with_indifferent_access) + end + + it 'does not enqueue a job if a job is already enqueued' do + account.update!(custom_attributes: { is_creating_customer: true }) + + expect do + post "/enterprise/api/v1/accounts/#{account.id}/subscription", + headers: admin.create_new_auth_token, + as: :json + end.not_to have_enqueued_job(Enterprise::CreateStripeCustomerJob).with(account) end it 'does not enqueues a job if customer id is present' do