Skip to content

Commit

Permalink
fix(pdf): Fix credit note generating pdf and webhook (#2659)
Browse files Browse the repository at this point in the history
## Context

For credit notes, Lago does not automatically generate PDF files. The
webhook `credit_note.generated` is only sent when an API request is
made.

Expected behaviour: the credit note logic should be the same as the
invoice logic.

## Description

This PR changes logic for generating credit notes and their webhook so
that it's the same as invoices.
  • Loading branch information
ivannovosad authored Oct 7, 2024
1 parent 2176404 commit c88720c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/services/credit_notes/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def call
if credit_note.finalized?
track_credit_note_created
deliver_webhook
CreditNotes::GeneratePdfJob.perform_later(credit_note)
deliver_email
handle_refund if should_handle_refund?
report_to_tax_provider
Expand Down
11 changes: 5 additions & 6 deletions app/services/credit_notes/generate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ def initialize(credit_note:, context: nil)
end

def call
return result.not_found_failure!(resource: 'credit_note') if credit_note.blank?
return result.not_found_failure!(resource: 'credit_note') unless credit_note.finalized?
return result.not_found_failure!(resource: 'credit_note') if credit_note.blank? || !credit_note.finalized?

generate_pdf(credit_note) if credit_note.file.blank?
generate_pdf(credit_note) if should_generate_pdf?

SendWebhookJob.perform_later('credit_note.generated', credit_note) if should_send_webhook?
SendWebhookJob.perform_later('credit_note.generated', credit_note)

result.credit_note = credit_note
result
Expand All @@ -40,8 +39,8 @@ def generate_pdf(credit_note)
credit_note.save!
end

def should_send_webhook?
context == 'api'
def should_generate_pdf?
context == 'admin' || credit_note.file.blank?
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def call
invoice.credit_notes.each do |credit_note|
track_credit_note_created(credit_note)
SendWebhookJob.perform_later('credit_note.created', credit_note)
CreditNotes::GeneratePdfJob.perform_later(credit_note)
end

result
Expand Down
7 changes: 5 additions & 2 deletions spec/services/credit_notes/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@
it 'delivers a webhook' do
create_service.call

expect(SendWebhookJob).to have_been_enqueued
.with('credit_note.created', CreditNote)
aggregate_failures do
expect(SendWebhookJob).to have_been_enqueued.with('credit_note.created', CreditNote)

expect(CreditNotes::GeneratePdfJob).to have_been_enqueued
end
end

it 'delivers an email' do
Expand Down
10 changes: 10 additions & 0 deletions spec/services/credit_notes/generate_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,15 @@
end.to have_enqueued_job(SendWebhookJob)
end
end

context 'when context is admin' do
let(:context) { 'admin' }

it 'calls the SendWebhook job' do
expect do
credit_note_generate_service.call
end.to have_enqueued_job(SendWebhookJob)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@
finalize_service.call
end.to have_enqueued_job(SendWebhookJob).with('credit_note.created', CreditNote)
end

it 'enqueues CreditNotes::GeneratePdfJob' do
expect do
finalize_service.call
end.to have_enqueued_job(CreditNotes::GeneratePdfJob)
end
end

context 'when tax integration is set up' do
Expand Down

0 comments on commit c88720c

Please sign in to comment.