Skip to content

Commit

Permalink
feat(manual-payments): Multiple fixes, change unique index in payments
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Jan 22, 2025
1 parent 1a77392 commit d32ba0d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 19 deletions.
12 changes: 6 additions & 6 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ def creditable_amount_cents
def refundable_amount_cents
return 0 if version_number < CREDIT_NOTES_MIN_VERSION || draft? || !payment_succeeded?

base_amount = if credit?
available_to_credit_amount_cents
else
total_paid_amount_cents - credit_notes.sum("refund_amount_cents + credit_amount_cents")
end
# base_amount = if credit?
# available_to_credit_amount_cents
# else
# total_paid_amount_cents - credit_notes.sum("refund_amount_cents + credit_amount_cents")
# end

amount = base_amount -
amount = available_to_credit_amount_cents -
credits.where(before_taxes: false).sum(:amount_cents) -
prepaid_credit_amount_cents
amount = amount.negative? ? 0 : amount
Expand Down
2 changes: 1 addition & 1 deletion app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def payment_request_succeeded
# Indexes
#
# index_payments_on_invoice_id (invoice_id)
# index_payments_on_payable_id_and_payable_type (payable_id,payable_type) UNIQUE WHERE (payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status]))
# index_payments_on_payable_id_and_payable_type (payable_id,payable_type) UNIQUE WHERE ((payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status])) AND (payment_type = 'provider'::payment_type))
# index_payments_on_payable_type_and_payable_id (payable_type,payable_id)
# index_payments_on_payment_provider_customer_id (payment_provider_customer_id)
# index_payments_on_payment_provider_id (payment_provider_id)
Expand Down
10 changes: 5 additions & 5 deletions config/locales/en/invoice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ en:
subscription: Subscription
subscription_interval: "%{plan_interval} subscription - %{plan_name}"
tax: Tax
tax_identification_number: "Tax ID: %{tax_identification_number}"
tax_identification_number: 'Tax ID: %{tax_identification_number}'
tax_name: "%{name} (%{rate}% on %{amount})"
tax_name_only:
customer_exempt: Customer is tax exempt
Expand All @@ -89,11 +89,11 @@ en:
tax_exempt: Customer is tax exempt.
total: Total
total_credits: Total credits
total_credits_with_value: "Total credits: %{credit_amount} credits"
total_credits_with_value: 'Total credits: %{credit_amount} credits'
total_due: Total due
total_events: "Total events: %{count}"
total_unit: "Total unit: %{units}"
total_unit_interval: "Total unit: %{events_count} events for %{units}"
total_events: 'Total events: %{count}'
total_unit: 'Total unit: %{units}'
total_unit_interval: 'Total unit: %{events_count} events for %{units}'
true_up_details: Minimum spend of %{min_amount} prorated on days of usage
true_up_metric: "%{metric} - True-up"
unit: Unit
Expand Down
25 changes: 25 additions & 0 deletions db/migrate/20250122130735_change_unique_index_in_payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class ChangeUniqueIndexInPayments < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
remove_index :payments, %i[payable_id payable_type]

add_index :payments,
%i[payable_id payable_type],
where: "payable_payment_status in ('pending', 'processing') and payment_type = 'provider'",
unique: true,
algorithm: :concurrently
end

def down
remove_index :payments, %i[payable_id payable_type]

add_index :payments,
%i[payable_id payable_type],
where: "payable_payment_status in ('pending', 'processing')",
unique: true,
algorithm: :concurrently
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@
let(:payment_status) { :succeeded }

it 'returns the correct refundable amount' do
expect(invoice.refundable_amount_cents).to eq(700)
expect(invoice.refundable_amount_cents).to eq(800)
end
end

Expand Down
7 changes: 4 additions & 3 deletions spec/queries/payments_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
let(:membership) { create(:membership) }
let(:organization) { membership.organization }
let(:invoice) { create(:invoice, organization:) }
let(:invoice2) { create(:invoice, organization:) }
let(:payment_request) { create(:payment_request, organization:) }
let(:payment_one) { create(:payment, payable: invoice) }
let(:payment_two) { create(:payment, payable: invoice) }
let(:payment_two) { create(:payment, payable: invoice2) }
let(:payment_three) { create(:payment, payable: payment_request) }

before do
Expand Down Expand Up @@ -51,9 +52,9 @@

it "returns only payments for the specified invoice" do
expect(result).to be_success
expect(returned_ids.count).to eq(2)
expect(returned_ids.count).to eq(1)
expect(returned_ids).to include(payment_one.id)
expect(returned_ids).to include(payment_two.id)
expect(returned_ids).not_to include(payment_two.id)
expect(returned_ids).not_to include(payment_three.id)
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/requests/api/v1/payments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@

it "returns organization's payments", :aggregate_failures do
invoice = create(:invoice, organization:)
invoice2 = create(:invoice, organization:)
payment_request = create(:payment_request, organization:)
first_payment = create(:payment, payable: invoice)
second_payment = create(:payment, payable: invoice)
second_payment = create(:payment, payable: invoice2)
third_payment = create(:payment, payable: payment_request)

subject
Expand Down

0 comments on commit d32ba0d

Please sign in to comment.