-
Notifications
You must be signed in to change notification settings - Fork 105
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
Feat(invoice_custom_sections): add create_invoice_applied_custom_sections service #3008
Merged
annvelents
merged 19 commits into
feat-invoice-custom-sections-api-controller
from
feat-invoice-custom-sections-create-applied-service
Jan 14, 2025
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9d6598c
WIP update customer with invoice_custom sections service
annvelents f40bce5
refactoring of the select and deselect services
annvelents 14a7c9f
fix and add tests
annvelents c62ed53
fix linter
annvelents 3989d8e
refactor managing invoice_custom_sections
annvelents 0a97f37
fix index on code uniqueness to apply only on not deleted ics
annvelents 153be68
split index removing and addition
annvelents 9022998
working on tests enhansing
annvelents a8fc104
add create_invoice_applied_custom_sections service
annvelents 90594ec
call apply_invoice_custom_section service when creating invoices
annvelents 6809e3a
fix service call
annvelents 02fb8c5
add call to create applied_invoice_custom_sections from all invoice c…
annvelents 924d1a8
wip adding tests for applying invoice custom sections
annvelents 877df57
add tests on creating applied_invoice_custom_section for different in…
annvelents 0ada1ba
fix linter
annvelents 4ea553a
fix tests
annvelents 91b9079
remove dyplicated migration
annvelents 901fa19
remove not needed call
annvelents 2c5755f
Feat(invoice_custom_sections): use custom sections in pdf (#3013)
annvelents File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/services/invoices/apply_invoice_custom_sections_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Invoices | ||
class ApplyInvoiceCustomSectionsService < BaseService | ||
def initialize(invoice:) | ||
@invoice = invoice | ||
@customer = invoice.customer | ||
|
||
super() | ||
end | ||
|
||
def call | ||
result.applied_sections = [] | ||
return result if customer.skip_invoice_custom_sections | ||
|
||
customer.applicable_invoice_custom_sections.each do |custom_section| | ||
invoice.applied_invoice_custom_sections.create!( | ||
code: custom_section.code, | ||
details: custom_section.details, | ||
display_name: custom_section.display_name, | ||
name: custom_section.name | ||
) | ||
end | ||
result.applied_sections = invoice.applied_invoice_custom_sections | ||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
end | ||
|
||
private | ||
|
||
attr_reader :invoice, :customer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
spec/services/invoices/apply_invoice_custom_sections_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Invoices::ApplyInvoiceCustomSectionsService, type: :service do | ||
subject(:invoice_service) { described_class.new(invoice:) } | ||
|
||
let(:organization) { create(:organization) } | ||
let(:customer) { create(:customer, organization:) } | ||
let(:invoice) { create(:invoice, customer:) } | ||
let(:custom_sections) { create_list(:invoice_custom_section, 3, organization:) } | ||
|
||
before do | ||
organization.selected_invoice_custom_sections << custom_sections[1..2] | ||
end | ||
|
||
describe '#call' do | ||
context 'when the customer has skip_invoice_custom_sections flag' do | ||
let(:customer) { create(:customer, organization:, skip_invoice_custom_sections: true) } | ||
|
||
it 'does not apply any custom sections' do | ||
result = invoice_service.call | ||
expect(result).to be_success | ||
expect(result.applied_sections).to be_empty | ||
expect(invoice.reload.applied_invoice_custom_sections).to be_empty | ||
end | ||
end | ||
|
||
context 'when the customer has custom sections' do | ||
before do | ||
customer.selected_invoice_custom_sections << custom_sections[0..1] | ||
end | ||
|
||
it 'applies the custom sections to the invoice' do | ||
result = invoice_service.call | ||
expect(result).to be_success | ||
sections = invoice.reload.applied_invoice_custom_sections | ||
expect(sections.map(&:code)).to match_array(custom_sections[0..1].map(&:code)) | ||
expect(sections.map(&:details)).to match_array(custom_sections[0..1].map(&:details)) | ||
expect(sections.map(&:display_name)).to match_array(custom_sections[0..1].map(&:display_name)) | ||
expect(sections.map(&:name)).to match_array(custom_sections[0..1].map(&:name)) | ||
end | ||
end | ||
|
||
context 'when the customer inherits custom sections from the organization' do | ||
it 'applies the organization\'s sections to the invoice' do | ||
result = invoice_service.call | ||
expect(result).to be_success | ||
sections = invoice.reload.applied_invoice_custom_sections | ||
expect(sections.map(&:code)).to match_array(custom_sections[1..2].map(&:code)) | ||
expect(sections.map(&:details)).to match_array(custom_sections[1..2].map(&:details)) | ||
expect(sections.map(&:display_name)).to match_array(custom_sections[1..2].map(&:display_name)) | ||
expect(sections.map(&:name)).to match_array(custom_sections[1..2].map(&:name)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.shared_examples 'applies invoice_custom_sections' do | ||
let(:invoice_custom_sections) { create_list(:invoice_custom_section, 4, organization:) } | ||
|
||
before do | ||
organization.selected_invoice_custom_sections = invoice_custom_sections[2..3] | ||
end | ||
|
||
context 'when the customer has :skip_invoice_custom_sections flag' do | ||
before { customer.update(skip_invoice_custom_sections: true) } | ||
|
||
it 'doesn\'t create applied_invoice_custom_section' do | ||
expect { service_call }.not_to change(AppliedInvoiceCustomSection, :count) | ||
end | ||
end | ||
|
||
context 'when customer follows organizations invoice_custom_sections' do | ||
it 'creates applied_invoice_custom_sections' do | ||
result = service_call | ||
invoice = result.invoice | ||
expect(invoice.applied_invoice_custom_sections.pluck(:code)).to match_array(organization.selected_invoice_custom_sections.pluck(:code)) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍