From 0214d0e63895024b1e87464b44e4861d4ff29688 Mon Sep 17 00:00:00 2001 From: Lovro Colic Date: Mon, 2 Dec 2024 15:41:06 +0100 Subject: [PATCH] update applied prepaid credits service exit case --- app/jobs/clock/finalize_invoices_job.rb | 2 ++ app/jobs/invoices/finalize_job.rb | 2 ++ .../credits/applied_prepaid_credit_service.rb | 4 +++- .../applied_prepaid_credit_service_spec.rb | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/jobs/clock/finalize_invoices_job.rb b/app/jobs/clock/finalize_invoices_job.rb index 58969eb0998..94c5abb2dda 100644 --- a/app/jobs/clock/finalize_invoices_job.rb +++ b/app/jobs/clock/finalize_invoices_job.rb @@ -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) diff --git a/app/jobs/invoices/finalize_job.rb b/app/jobs/invoices/finalize_job.rb index 85c4f1f9e64..e477c5169ac 100644 --- a/app/jobs/invoices/finalize_job.rb +++ b/app/jobs/invoices/finalize_job.rb @@ -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) diff --git a/app/services/credits/applied_prepaid_credit_service.rb b/app/services/credits/applied_prepaid_credit_service.rb index cc3fd7ab190..efdd59fa4dc 100644 --- a/app/services/credits/applied_prepaid_credit_service.rb +++ b/app/services/credits/applied_prepaid_credit_service.rb @@ -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) diff --git a/spec/services/credits/applied_prepaid_credit_service_spec.rb b/spec/services/credits/applied_prepaid_credit_service_spec.rb index 8094c3c144c..d09950b3048 100644 --- a/spec/services/credits/applied_prepaid_credit_service_spec.rb +++ b/spec/services/credits/applied_prepaid_credit_service_spec.rb @@ -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