Skip to content

Commit

Permalink
fix(payment-provider-code): Add some improvements: remove obsolete co…
Browse files Browse the repository at this point in the history
…mments, optimize migrations
  • Loading branch information
ivannovosad committed Oct 3, 2024
1 parent a729caa commit 54b0b8d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 55 deletions.
17 changes: 0 additions & 17 deletions app/services/payment_providers/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ def create_or_update(**args)
adyen_provider.success_redirect_url = args[:success_redirect_url] if args.key?(:success_redirect_url)
adyen_provider.save!

# if api_key != adyen_provider.api_key
# # NOTE: ensure existing payment_provider_customers are
# # attached to the provider
# reattach_provider_customers(
# organization_id: args[:organization_id],
# adyen_provider:
# )
# end

if payment_provider_code_changed?(adyen_provider, old_code, args)
adyen_provider.customers.update_all(payment_provider_code: args[:code]) # rubocop:disable Rails/SkipsModelValidations
end
Expand Down Expand Up @@ -132,14 +123,6 @@ def handle_event(organization:, event_json:)
end
end

# def reattach_provider_customers(organization_id:, adyen_provider:)
# PaymentProviderCustomers::AdyenCustomer
# .joins(:customer)
# .where(payment_provider_id: nil, customers: {organization_id:}).find_each do |c|
# c.update(payment_provider_id: adyen_provider.id)
# end
# end

private

def update_payment_status(event, payment_type)
Expand Down
8 changes: 5 additions & 3 deletions app/services/payment_providers/destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def destroy(id:)

customer_ids = payment_provider.customer_ids

payment_provider.payment_provider_customers.update_all(payment_provider_id: nil) # rubocop:disable Rails/SkipsModelValidations
payment_provider.discard!
ActiveRecord::Base.transaction do
payment_provider.payment_provider_customers.update_all(payment_provider_id: nil) # rubocop:disable Rails/SkipsModelValidations
payment_provider.discard!

Customer.where(id: customer_ids).update_all(payment_provider: nil, payment_provider_code: nil) # rubocop:disable Rails/SkipsModelValidations
Customer.where(id: customer_ids).update_all(payment_provider: nil, payment_provider_code: nil) # rubocop:disable Rails/SkipsModelValidations
end

result.payment_provider = payment_provider
result
Expand Down
14 changes: 0 additions & 14 deletions app/services/payment_providers/stripe_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ def create_or_update(**args)
unregister_webhook(stripe_provider, secret_key)

PaymentProviders::Stripe::RegisterWebhookJob.perform_later(stripe_provider)

# NOTE: ensure existing payment_provider_customers are
# attached to the provider
# reattach_provider_customers(
# organization_id: args[:organization_id],
# stripe_provider:
# )
end

if payment_provider_code_changed?(stripe_provider, old_code, args)
Expand Down Expand Up @@ -194,13 +187,6 @@ def unregister_webhook(stripe_provider, api_key)
Sentry.capture_exception(e)
end

# def reattach_provider_customers(organization_id:, stripe_provider:)
# PaymentProviderCustomers::StripeCustomer
# .joins(:customer)
# .where(payment_provider_id: nil, customers: {organization_id:})
# .update_all(payment_provider_id: stripe_provider.id) # rubocop:disable Rails/SkipsModelValidations
# end

def payment_service_klass(event)
payable_type = event.data.object.metadata.to_h[:lago_payable_type] || "Invoice"

Expand Down
17 changes: 6 additions & 11 deletions db/migrate/20241001105523_add_deleted_at_to_payment_providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ class AddDeletedAtToPaymentProviders < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
safety_assured do
add_column :payment_providers, :deleted_at, :datetime

remove_index :payment_providers, %i[code organization_id]
add_index :payment_providers, %i[code organization_id], unique: true, where: 'deleted_at IS NULL'
end
add_column :payment_providers, :deleted_at, :datetime
remove_index :payment_providers, %i[code organization_id]
add_index :payment_providers, %i[code organization_id], unique: true, where: 'deleted_at IS NULL', algorithm: :concurrently
end

def down
safety_assured do
remove_column :payment_providers, :deleted_at
remove_index :payment_providers, %i[code organization_id]
add_index :payment_providers, %i[code organization_id], unique: true
end
remove_column :payment_providers, :deleted_at
remove_index :payment_providers, %i[code organization_id]
add_index :payment_providers, %i[code organization_id], unique: true, algorithm: :concurrently
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ class AddDeletedAtToPaymentProviderCustomers < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
safety_assured do
add_column :payment_provider_customers, :deleted_at, :datetime

remove_index :payment_provider_customers, %i[customer_id type]
add_index :payment_provider_customers, %i[customer_id type], unique: true, where: 'deleted_at IS NULL'
end
add_column :payment_provider_customers, :deleted_at, :datetime
remove_index :payment_provider_customers, %i[customer_id type]
add_index :payment_provider_customers, %i[customer_id type], unique: true, where: 'deleted_at IS NULL', algorithm: :concurrently
end

def down
safety_assured do
remove_column :payment_provider_customers, :deleted_at
add_index :payment_provider_customers, %i[customer_id type], unique: true
end
remove_column :payment_provider_customers, :deleted_at
add_index :payment_provider_customers, %i[customer_id type], unique: true, algorithm: :concurrently
end
end

0 comments on commit 54b0b8d

Please sign in to comment.