Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions backend/app/controllers/internal/companies/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create
authorize Invoice

result = CreateOrUpdateInvoiceService.new(
params:,
params: params,
user: Current.user,
company: Current.company,
contractor: Current.company_worker,
Expand All @@ -39,7 +39,7 @@ def update
authorize @invoice

result = CreateOrUpdateInvoiceService.new(
params:,
params: params,
user: Current.user,
company: Current.company,
contractor: Current.company_worker,
Expand All @@ -58,15 +58,15 @@ def microdeposit_verification_details

company = Current.company
details = company.microdeposit_verification_details if company.microdeposit_verification_required?
render json: { details: }
render json: { details: details }
end

def export
authorize Invoice

body = InvoiceCsv.new(Current.company.invoices.alive.order(created_at: :asc)).generate
response.headers["Content-Disposition"] = "attachment; filename=invoices-#{Time.current.strftime("%Y-%m-%d_%H%M%S")}.csv"
render body:, content_type: "text/csv"
render body: body, content_type: "text/csv"
end

def approve
Expand Down Expand Up @@ -112,25 +112,22 @@ def load_invoice!
@invoice = Current.user.invoices.alive.find_by!(external_id: params[:id])
end

# OPTIMIZED METHOD 1
def authorize_invoices_for_rejection
all_invoices_belong_to_company = invoice_external_ids_for_rejection.all? do |invoice_external_id|
Current.company.invoices.alive.exists?(external_id: invoice_external_id)
end
ids = invoice_external_ids_for_rejection.uniq
# PERF: Replaced N+1 .exists? calls with a single bulk count query
count = Current.company.invoices.alive.where(external_id: ids).count

unless all_invoices_belong_to_company
e404
end
e404 unless count == ids.size
end

# OPTIMIZED METHOD 2
def authorize_invoices_for_approval_and_pay
ids = invoice_external_ids_for_approval + invoice_external_ids_for_payment
all_invoices_belong_to_company = ids.all? do |invoice_id|
Current.company.invoices.alive.exists?(external_id: invoice_id)
end
ids = (invoice_external_ids_for_approval + invoice_external_ids_for_payment).uniq
# PERF: Replaced N+1 .exists? calls with a single bulk count query
count = Current.company.invoices.alive.where(external_id: ids).count

unless all_invoices_belong_to_company
e404
end
e404 unless count == ids.size
end

def invoice_external_ids_for_rejection
Expand Down