Skip to content

Commit

Permalink
Misc: queries optimisation for refresh service (#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents authored Dec 19, 2024
1 parent 6b96df4 commit dd189dc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Invoice < ApplicationRecord
scope :visible, -> { where(status: VISIBLE_STATUS.keys) }
scope :invisible, -> { where(status: INVISIBLE_STATUS.keys) }
scope :with_generated_number, -> { where(status: %w[finalized voided]) }
scope :ready_to_be_refreshed, -> { where(ready_to_be_refreshed: true) }
scope :ready_to_be_refreshed, -> { draft.where(ready_to_be_refreshed: true) }
scope :ready_to_be_finalized, -> { draft.where('issuing_date <= ?', Time.current.to_date) }

scope :created_before,
Expand Down Expand Up @@ -495,9 +495,11 @@ def status_changed_to_finalized?
#
# index_invoices_on_customer_id (customer_id)
# index_invoices_on_customer_id_and_sequential_id (customer_id,sequential_id) UNIQUE
# index_invoices_on_issuing_date (issuing_date)
# index_invoices_on_number (number)
# index_invoices_on_organization_id (organization_id)
# index_invoices_on_payment_overdue (payment_overdue)
# index_invoices_on_ready_to_be_refreshed (ready_to_be_refreshed) WHERE (ready_to_be_refreshed = true)
# index_invoices_on_sequential_id (sequential_id)
# index_invoices_on_status (status)
#
Expand Down
1 change: 1 addition & 0 deletions app/services/invoices/refresh_draft_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def invoice_credit_note_items
CreditNoteItem
.joins(:credit_note)
.where(credit_note: {invoice_id: invoice.id})
.includes(:fee)
end

def flag_lifetime_usage_for_refresh
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241217120924_add_indexes_to_invoices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class AddIndexesToInvoices < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
add_index :invoices, :ready_to_be_refreshed, where: "ready_to_be_refreshed = true", algorithm: :concurrently
add_index :invoices, :issuing_date, algorithm: :concurrently
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@
end
end

describe 'ready_to_be_refreshed' do
let(:invoices) do
[
create(:invoice, status: :draft, ready_to_be_refreshed: true),
create(:invoice, status: :draft, ready_to_be_refreshed: false),
create(:invoice, status: :finalized, ready_to_be_refreshed: true)
]
end

before { invoices }

it 'returns only the invoices that are ready for refresh' do
expect(described_class.ready_to_be_finalized.pluck(:id)).to include(invoices[0].id)
end
end

describe 'when status is visible' do
it do
described_class::VISIBLE_STATUS.keys.each do |status|
Expand Down

0 comments on commit dd189dc

Please sign in to comment.