Skip to content
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

APIs for invoice list page #208

Merged
merged 23 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions app/controllers/internal_api/v1/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def index
invoices:,
pagy: pagy_metadata(pagy),
summary: {
overdue_amount: current_company.invoices.where(status: :overdue).sum(:amount),
overdue_amount: current_company.invoices.overdue.sum(:amount),
outstanding_amount: current_company.invoices.sum(:outstanding_amount),
draft_amount: current_company.invoices.where(status: :draft).sum(:amount)
draft_amount: current_company.invoices.draft.sum(:amount)
}
}
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Invoice < ApplicationRecord
scope :to_date, -> (to) { where("issue_date <= ?", to) if to.present? }
scope :for_clients, -> (client_ids) { where(client_id: client_ids) if client_ids.present? }

delegate :name, to: :client, prefix: :client
apoorv-mishra marked this conversation as resolved.
Show resolved Hide resolved

def sub_total
@_sub_total ||= invoice_line_items.sum { |line_item| line_item[:rate] * line_item[:quantity] }
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/internal_api/v1/invoices/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ json.invoices do
json.due_date invoice.due_date
json.amount invoice.amount
json.client do
json.name invoice.client.name
json.name invoice.client_name
end
json.company do
json.name current_company.name
Expand Down
4 changes: 4 additions & 0 deletions spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@
end
end
end

describe ".delegate" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe ".delegate" do
describe "Delegates" do

it { is_expected.to delegate_method(:name).to(:client).with_prefix(:client) }
end
end
25 changes: 12 additions & 13 deletions spec/requests/internal_api/v1/invoices/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,43 @@
describe "invoices_per_page param" do
it "returns the number the invoices specified by invoices_per_page" do
invoices_per_page = 10
send_request :get, "#{internal_api_v1_invoices_path}?invoices_per_page=#{invoices_per_page}"
send_request :get, internal_api_v1_invoices_path(invoices_per_page:)
expect(response).to have_http_status(:ok)
expect(json_response["invoices"].size).to eq(10)
end

it "throws 400 bad_request error if invoices_per_page is less than or equal to zero" do
invoices_per_page = 0
send_request :get, "#{internal_api_v1_invoices_path}?invoices_per_page=#{invoices_per_page}"
send_request :get, internal_api_v1_invoices_path(invoices_per_page:)
expect(response).to have_http_status(:bad_request)
end
end

describe "page param" do
it "returns invoices offset by page" do
page = 2
send_request :get, "#{internal_api_v1_invoices_path}?page=#{page}&invoices_per_page=5"
page, invoices_per_page = 2, 5
send_request :get, internal_api_v1_invoices_path(page:, invoices_per_page:)
expect(response).to have_http_status(:ok)
expect(json_response["invoices"].size).to eq(5)
end

it "throws 400 bad_request error if page is less than or equal to zero" do
page = -1
send_request :get, "#{internal_api_v1_invoices_path}?page=#{page}&invoices_per_page=5"
page, invoices_per_page = -1, 5
send_request :get, internal_api_v1_invoices_path(page:, invoices_per_page:)
expect(response).to have_http_status(:bad_request)
end

it "throws 400 bad_request error if page overflows the total number of invoices" do
page = 10
send_request :get, "#{internal_api_v1_invoices_path}?page=#{page}&invoices_per_page=5"
page, invoices_per_page = 10, 5
send_request :get, internal_api_v1_invoices_path(page:, invoices_per_page:)
expect(response).to have_http_status(:bad_request)
end
end

describe "from param" do
it "returns invoices issued on or after from" do
from = Date.parse("2021-01-01")
send_request :get, "#{internal_api_v1_invoices_path}?from=#{from}"
send_request :get, internal_api_v1_invoices_path(from:)
expected_invoices = company.invoices.select { |inv| !inv.issue_date.before?(from) }
expect(response).to have_http_status(:ok)
expect(
Expand All @@ -71,7 +71,7 @@
describe "to param" do
it "returns invoices issued on or before to" do
to = Date.parse("2021-01-01")
send_request :get, "#{internal_api_v1_invoices_path}?to=#{to}"
send_request :get, internal_api_v1_invoices_path(to:)
expected_invoices = company.invoices.select { |inv| !inv.issue_date.after?(to) }
expect(response).to have_http_status(:ok)
expect(
Expand All @@ -87,7 +87,7 @@
describe "client_ids[] param" do
it "returns invoices generated for clients specified by client_ids[]" do
client_ids = [9, 15, 29]
send_request :get, "#{internal_api_v1_invoices_path}?client_ids[]=#{client_ids[0]}&client_ids[]=#{client_ids[1]}&client_ids[]=#{client_ids[2]}"
send_request :get, internal_api_v1_invoices_path(client_ids:)
expected_invoices = company.invoices.select { |inv| client_ids.include?(inv.client_id) }
expect(response).to have_http_status(:ok)
expect(
Expand All @@ -103,8 +103,7 @@
describe "statuses[] param" do
it "returns invoices with statuses specified by statuses[]" do
statuses = [:draft, :overdue, :paid]
send_request :get, "#{internal_api_v1_invoices_path}?statuses[]=#{statuses[0]}&statuses[]=#{statuses[1]}&statuses[]=#{statuses[2]}"
expected_invoices = company.invoices.select { |inv| statuses.include?(inv.status.to_sym) }
send_request :get, internal_api_v1_invoices_path(statuses:)
expect(response).to have_http_status(:ok)
end
end
Expand Down