Skip to content

Commit

Permalink
fix(invoice): Finalize each invoice in a single job (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet authored Dec 12, 2023
1 parent b452509 commit 08f4c94
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/jobs/clock/finalize_invoices_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FinalizeInvoicesJob < ApplicationJob

def perform
Invoice.ready_to_be_finalized.each do |invoice|
Invoices::FinalizeService.call(invoice:)
Invoices::FinalizeJob.perform_later(invoice)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions app/jobs/invoices/finalize_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Invoices
class FinalizeJob < ApplicationJob
queue_as 'invoices'

def perform(invoice)
Invoices::FinalizeService.call(invoice:)
end
end
end
6 changes: 3 additions & 3 deletions spec/jobs/clock/finalize_invoices_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

travel_to(current_date) do
described_class.perform_now
expect(Invoices::FinalizeService).not_to have_received(:call).with(invoice: draft_invoice)
expect(Invoices::FinalizeService).not_to have_received(:call).with(invoice: finalized_invoice)
expect(Invoices::FinalizeJob).not_to have_been_enqueued.with(draft_invoice)
expect(Invoices::FinalizeJob).not_to have_been_enqueued.with(finalized_invoice)
end
end
end
Expand All @@ -50,7 +50,7 @@

travel_to(current_date) do
described_class.perform_now
expect(Invoices::FinalizeService).to have_received(:call).with(invoice: draft_invoice)
expect(Invoices::FinalizeJob).to have_been_enqueued.with(draft_invoice)
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/jobs/invoices/finalize_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Invoices::FinalizeJob, type: :job do
let(:invoice) { create(:invoice) }

let(:result) { BaseService::Result.new }

let(:finalize_service) do
instance_double(Invoices::FinalizeService)
end

it 'delegates to the Generate service' do
allow(Invoices::FinalizeService).to receive(:new)
.with(invoice:)
.and_return(finalize_service)
allow(finalize_service).to receive(:call)
.and_return(result)

described_class.perform_now(invoice)

expect(Invoices::FinalizeService).to have_received(:new)
expect(finalize_service).to have_received(:call)
end
end

0 comments on commit 08f4c94

Please sign in to comment.