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

fix: update applied prepaid credits service exit case #2901

Merged
merged 1 commit into from
Dec 3, 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/clock/finalize_invoices_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class FinalizeInvoicesJob < ApplicationJob

queue_as 'clock'

unique :until_executed, on_conflict: :log

def perform
Invoice.ready_to_be_finalized.each do |invoice|
Invoices::FinalizeJob.perform_later(invoice)
Expand Down
2 changes: 2 additions & 0 deletions app/jobs/invoices/finalize_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Invoices
class FinalizeJob < ApplicationJob
queue_as 'invoices'

unique :until_executed, on_conflict: :log

retry_on Sequenced::SequenceError, wait: :polynomially_longer

def perform(invoice)
Expand Down
4 changes: 3 additions & 1 deletion app/services/credits/applied_prepaid_credit_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def initialize(invoice:, wallet:)
end

def call
return result if already_applied?
if already_applied?
return result.service_failure!(code: 'already_applied', message: 'Prepaid credits already applied')
end

amount_cents = compute_amount
amount = compute_amount_from_cents(amount_cents)
Expand Down
16 changes: 16 additions & 0 deletions spec/services/credits/applied_prepaid_credit_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,21 @@
expect(wallet.credits_balance).to eq(0.0)
end
end

context 'when already applied' do
let(:wallet_transaction) { create(:wallet_transaction, wallet:, invoice:, transaction_type: 'outbound') }

before { wallet_transaction }

it 'returns error' do
result = credit_service.call

aggregate_failures do
expect(result).not_to be_success
expect(result.error.code).to eq('already_applied')
expect(result.error.error_message).to eq('Prepaid credits already applied')
end
end
end
end
end