Skip to content

Commit

Permalink
chore(services): Rename FinalizeService to RefreshDraftAndFinalizeSer…
Browse files Browse the repository at this point in the history
…vice (getlago#2302)

## Description

As we're considering adding more Invoice state, we should specify what
the current `FinalizeService` does. It not only finalize it, it takes a
draft invoice, refresh it and finalize it.

We're also changing a little bit how invoice is set in result and
reloaded, hoping to make it a little bit clearer.
  • Loading branch information
julienbourdeau authored and abdussamadbello committed Aug 8, 2024
1 parent aa66660 commit 0cf3aae
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def finalize
invoice = current_organization.invoices.draft.find_by(id: params[:id])
return not_found_error(resource: 'invoice') unless invoice

result = Invoices::FinalizeService.call(invoice:)
result = Invoices::RefreshDraftAndFinalizeService.call(invoice:)
if result.success?
render_invoice(result.invoice)
else
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/invoices/finalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Finalize < BaseMutation
type Types::Invoices::Object

def resolve(**args)
result = ::Invoices::FinalizeService.call(
result = ::Invoices::RefreshDraftAndFinalizeService.call(
invoice: current_organization.invoices.draft.find_by(id: args[:id])
)
result.success? ? result.invoice : result_error(result)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/invoices/finalize_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class FinalizeJob < ApplicationJob
queue_as 'invoices'

def perform(invoice)
Invoices::FinalizeService.call(invoice:)
Invoices::RefreshDraftAndFinalizeService.call(invoice:)
end
end
end
2 changes: 1 addition & 1 deletion app/services/customers/terminate_relations_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def call
customer.subscriptions.pending.find_each(&:mark_as_canceled!)

# NOTE: Finalize all draft invoices.
customer.invoices.draft.find_each { |invoice| Invoices::FinalizeService.call(invoice:) }
customer.invoices.draft.find_each { |invoice| Invoices::RefreshDraftAndFinalizeService.call(invoice:) }

# NOTE: Terminate applied coupons
customer.applied_coupons.active.find_each do |applied_coupon|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call

# NOTE: Finalize related draft invoices.
customer.invoices.ready_to_be_finalized.each do |invoice|
Invoices::FinalizeService.call(invoice:)
Invoices::RefreshDraftAndFinalizeService.call(invoice:)
end

# NOTE: Update issuing_date on draft invoices.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Invoices
class FinalizeService < BaseService
class RefreshDraftAndFinalizeService < BaseService
def initialize(invoice:)
@invoice = invoice
super
Expand All @@ -11,8 +11,7 @@ def call
return result.not_found_failure!(resource: 'invoice') if invoice.nil?

ActiveRecord::Base.transaction do
self.result = Invoices::RefreshDraftService.call(invoice:, context: :finalize)
result.raise_if_error!
Invoices::RefreshDraftService.call(invoice:, context: :finalize).raise_if_error!

invoice.status = :finalized
invoice.issuing_date = issuing_date
Expand All @@ -22,8 +21,10 @@ def call
invoice.credit_notes.each(&:finalized!)
end

SendWebhookJob.perform_later('invoice.created', result.invoice)
GeneratePdfAndNotifyJob.perform_later(invoice: invoice.reload, email: should_deliver_email?)
result.invoice = invoice.reload

SendWebhookJob.perform_later('invoice.created', invoice)
GeneratePdfAndNotifyJob.perform_later(invoice:, email: should_deliver_email?)
Integrations::Aggregator::Invoices::CreateJob.perform_later(invoice:) if invoice.should_sync_invoice?
Integrations::Aggregator::SalesOrders::CreateJob.perform_later(invoice:) if invoice.should_sync_sales_order?
Invoices::Payments::CreateService.new(invoice).call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call

# NOTE: Finalize related draft invoices.
organization.invoices.ready_to_be_finalized.each do |invoice|
Invoices::FinalizeService.call(invoice:)
Invoices::RefreshDraftAndFinalizeService.call(invoice:)
end

# NOTE: Update issuing_date on draft invoices.
Expand Down
2 changes: 1 addition & 1 deletion app/services/plans/destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call

# NOTE: Finalize all draft invoices.
invoices = Invoice.draft.joins(:plans).where(plans: {id: plan.id}).distinct
invoices.find_each { |invoice| Invoices::FinalizeService.call(invoice:) }
invoices.find_each { |invoice| Invoices::RefreshDraftAndFinalizeService.call(invoice:) }

plan.pending_deletion = false
plan.discard!
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/clock/finalize_invoices_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
before do
draft_invoice
finalized_invoice
allow(Invoices::FinalizeService).to receive(:call)
allow(Invoices::RefreshDraftAndFinalizeService).to receive(:call)
end

context 'when during the grace period' do
Expand Down
6 changes: 3 additions & 3 deletions spec/jobs/invoices/finalize_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
let(:result) { BaseService::Result.new }

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

it 'delegates to the Generate service' do
allow(Invoices::FinalizeService).to receive(:new)
allow(Invoices::RefreshDraftAndFinalizeService).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(Invoices::RefreshDraftAndFinalizeService).to have_received(:new)
expect(finalize_service).to have_received(:call)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
before do
invoice_to_be_finalized
invoice_to_not_be_finalized
allow(Invoices::FinalizeService).to receive(:call)
allow(Invoices::RefreshDraftAndFinalizeService).to receive(:call)
end

it 'updates invoice grace period on customer' do
Expand All @@ -36,8 +36,8 @@
result = update_service.call

expect(result.customer.invoice_grace_period).to eq(2)
expect(Invoices::FinalizeService).not_to have_received(:call).with(invoice: invoice_to_not_be_finalized)
expect(Invoices::FinalizeService).to have_received(:call).with(invoice: invoice_to_be_finalized)
expect(Invoices::RefreshDraftAndFinalizeService).not_to have_received(:call).with(invoice: invoice_to_not_be_finalized)
expect(Invoices::RefreshDraftAndFinalizeService).to have_received(:call).with(invoice: invoice_to_be_finalized)
end
end

Expand Down Expand Up @@ -76,7 +76,7 @@
travel_to(current_date) do
update_service.call

expect(Invoices::FinalizeService).not_to have_received(:call)
expect(Invoices::RefreshDraftAndFinalizeService).not_to have_received(:call)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rails_helper'

RSpec.describe Invoices::FinalizeService, type: :service do
RSpec.describe Invoices::RefreshDraftAndFinalizeService, type: :service do
subject(:finalize_service) { described_class.new(invoice:) }

describe '#call' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
before do
invoice_to_be_finalized
invoice_to_not_be_finalized
allow(Invoices::FinalizeService).to receive(:call)
allow(Invoices::RefreshDraftAndFinalizeService).to receive(:call)
end

it 'updates invoice grace period on organization' do
Expand All @@ -36,8 +36,8 @@
result = update_service.call

expect(result.organization.invoice_grace_period).to eq(2)
expect(Invoices::FinalizeService).not_to have_received(:call).with(invoice: invoice_to_not_be_finalized)
expect(Invoices::FinalizeService).to have_received(:call).with(invoice: invoice_to_be_finalized)
expect(Invoices::RefreshDraftAndFinalizeService).not_to have_received(:call).with(invoice: invoice_to_not_be_finalized)
expect(Invoices::RefreshDraftAndFinalizeService).to have_received(:call).with(invoice: invoice_to_be_finalized)
end
end

Expand Down Expand Up @@ -76,7 +76,7 @@
travel_to(current_date) do
update_service.call

expect(Invoices::FinalizeService).not_to have_received(:call)
expect(Invoices::RefreshDraftAndFinalizeService).not_to have_received(:call)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/services/organizations/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
before do
invoice_to_be_finalized
invoice_to_not_be_finalized
allow(Invoices::FinalizeService).to receive(:call)
allow(Invoices::RefreshDraftAndFinalizeService).to receive(:call)
end

it 'finalizes corresponding draft invoices' do
Expand All @@ -125,8 +125,8 @@

aggregate_failures do
expect(result.organization.invoice_grace_period).to eq(2)
expect(Invoices::FinalizeService).not_to have_received(:call).with(invoice: invoice_to_not_be_finalized)
expect(Invoices::FinalizeService).to have_received(:call).with(invoice: invoice_to_be_finalized)
expect(Invoices::RefreshDraftAndFinalizeService).not_to have_received(:call).with(invoice: invoice_to_not_be_finalized)
expect(Invoices::RefreshDraftAndFinalizeService).to have_received(:call).with(invoice: invoice_to_be_finalized)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/services/subscriptions/dates/monthly_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@

subscription.customer.update!(timezone: new_timezone)

finalized_result = Invoices::FinalizeService.call(invoice: result.invoice.reload)
finalized_result = Invoices::RefreshDraftAndFinalizeService.call(invoice: result.invoice.reload)
invoice_sub = finalized_result.invoice.reload.invoice_subscriptions.first

aggregate_failures do
Expand Down

0 comments on commit 0cf3aae

Please sign in to comment.