Skip to content

Commit

Permalink
Fix(rev-share): fix bugs found during QA (#3098)
Browse files Browse the repository at this point in the history
## Context

There were small issues found during the QA

## Description

Fixed invoice numbering switching from customer-based to
per-organization
Do not allow edit customers via API if they are not editable
  • Loading branch information
annvelents authored Jan 24, 2025
1 parent 4bbb63b commit ec8ef1f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def generate_organization_sequential_id
) do
# If previous invoice had different numbering, base sequential id is the total number of invoices
organization_sequential_id = if switched_from_customer_numbering?
organization.invoices.with_generated_number.count
organization.invoices.non_self_billed.with_generated_number.count
else
organization
.invoices
Expand Down
2 changes: 1 addition & 1 deletion app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create_from_api(organization:, params:)
customer.lastname = params[:lastname] if params.key?(:lastname)
customer.customer_type = params[:customer_type] if params.key?(:customer_type)

if customer.organization.revenue_share_enabled?
if customer.organization.revenue_share_enabled? && customer.editable?
customer.account_type = params[:account_type] if params.key?(:account_type)
customer.exclude_from_dunning_campaign = customer.partner_account?
end
Expand Down
131 changes: 131 additions & 0 deletions spec/scenarios/invoices/invoice_numbering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -746,4 +746,135 @@
end
end
end

context 'with partner customer' do
let(:customer_third) { create(:customer, organization:, account_type: 'partner') }

around { |test| lago_premium!(&test) }

before { organization.update!(premium_integrations: ['revenue_share']) }

it 'creates invoice numbers correctly' do
# NOTE: Jul 19th: create the subscription
travel_to(subscription_at) do
create_subscription(
{
external_customer_id: customer_first.external_id,
external_id: customer_first.external_id,
plan_code: monthly_plan.code,
billing_time: 'anniversary',
subscription_at: subscription_at.iso8601
}
)
create_subscription(
{
external_customer_id: customer_second.external_id,
external_id: customer_second.external_id,
plan_code: monthly_plan.code,
billing_time: 'anniversary',
subscription_at: subscription_at.iso8601
}
)
create_subscription(
{
external_customer_id: customer_third.external_id,
external_id: customer_third.external_id,
plan_code: monthly_plan.code,
billing_time: 'anniversary',
subscription_at: subscription_at.iso8601
}
)

invoices = organization.invoices.order(created_at: :desc).limit(3)
sequential_ids = invoices.pluck(:sequential_id)
organization_sequential_ids = invoices.pluck(:organization_sequential_id)
numbers = invoices.pluck(:number)

expect(sequential_ids).to match_array([1, 1, 1])
expect(organization_sequential_ids).to match_array([0, 0, 0])
expect(numbers).to match_array(%w[ORG-1-001-001 ORG-1-002-001 ORG-1-003-001])
end

# NOTE: August 19th: Bill subscription
travel_to(DateTime.new(2023, 8, 19, 12, 12)) do
Subscriptions::BillingService.call
perform_all_enqueued_jobs

invoices = organization.invoices.order(created_at: :desc).limit(3)
sequential_ids = invoices.pluck(:sequential_id)
organization_sequential_ids = invoices.pluck(:organization_sequential_id)
numbers = invoices.pluck(:number)

expect(sequential_ids).to match_array([2, 2, 2])
expect(organization_sequential_ids).to match_array([0, 0, 0])
expect(numbers).to match_array(%w[ORG-1-001-002 ORG-1-002-002 ORG-1-003-002])
end

# NOTE: September 19th: Bill subscription
travel_to(DateTime.new(2023, 9, 19, 12, 12)) do
Subscriptions::BillingService.call
perform_all_enqueued_jobs

invoices = organization.invoices.order(created_at: :desc).limit(3)
sequential_ids = invoices.pluck(:sequential_id)
organization_sequential_ids = invoices.pluck(:organization_sequential_id)
numbers = invoices.pluck(:number)

expect(sequential_ids).to match_array([3, 3, 3])
expect(organization_sequential_ids).to match_array([0, 0, 0])
expect(numbers).to match_array(%w[ORG-1-001-003 ORG-1-002-003 ORG-1-003-003])
end

# NOTE: October 19th: Switching to per_organization numbering and Bill subscription
travel_to(DateTime.new(2023, 10, 19, 12, 12)) do
organization.update!(document_numbering: 'per_organization', document_number_prefix: 'ORG-11')

Subscriptions::BillingService.call
perform_all_enqueued_jobs

invoices = organization.invoices.order(created_at: :desc).limit(3)
sequential_ids = invoices.pluck(:sequential_id)
organization_sequential_ids = invoices.pluck(:organization_sequential_id)
numbers = invoices.pluck(:number)

expect(sequential_ids).to match_array([4, 4, 4])
expect(organization_sequential_ids).to match_array([7, 8, 0])
expect(numbers).to match_array(%w[ORG-11-202310-007 ORG-11-202310-008 ORG-11-003-004])
end

# NOTE: November 19th: Switching to per_customer numbering and Bill subscription
travel_to(DateTime.new(2023, 11, 19, 12, 12)) do
organization.update!(document_numbering: 'per_customer')

Subscriptions::BillingService.call
perform_all_enqueued_jobs

invoices = organization.invoices.order(created_at: :desc).limit(3)
sequential_ids = invoices.pluck(:sequential_id)
organization_sequential_ids = invoices.pluck(:organization_sequential_id)
numbers = invoices.pluck(:number)

expect(sequential_ids).to match_array([5, 5, 5])
expect(organization_sequential_ids).to match_array([0, 0, 0])
expect(numbers).to match_array(%w[ORG-11-001-005 ORG-11-002-005 ORG-11-003-005])
end

# NOTE: December 19th: Switching to per_organization numbering and Bill subscription
travel_to(DateTime.new(2023, 12, 19, 12, 12)) do
organization.update!(document_numbering: 'per_organization')

Subscriptions::BillingService.call
perform_all_enqueued_jobs

invoices = organization.invoices.order(created_at: :desc).limit(3)
sequential_ids = invoices.pluck(:sequential_id)
organization_sequential_ids = invoices.pluck(:organization_sequential_id)
numbers = invoices.pluck(:number)

expect(sequential_ids).to match_array([6, 6, 6])
expect(organization_sequential_ids).to match_array([11, 12, 0])
expect(numbers).to match_array(%w[ORG-11-202312-011 ORG-11-202312-012 ORG-11-003-006])
end
end
end
end
19 changes: 19 additions & 0 deletions spec/services/customers/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,25 @@
expect(customer).to be_partner_account
expect(customer).to be_exclude_from_dunning_campaign
end

context "when updating a customer that already have an invoice" do
let(:customer) { create(:customer, organization:, account_type: "customer") }
let(:invoice) { create(:invoice, customer: customer) }

before { invoice }

it "doesn't update customer to partner" do
result = customers_service.create_from_api(
organization:,
params: create_args.merge(external_id: customer.external_id)
)

expect(result).to be_success

customer = result.customer
expect(customer).to be_customer_account
end
end
end
end

Expand Down

0 comments on commit ec8ef1f

Please sign in to comment.