-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* API for invoice creation * API to update an invoice * Remove unnecessary if clause * use partials to dry up * abstract client.find and fix specs
- Loading branch information
Apoorv Mishra
authored
Apr 14, 2022
1 parent
c7ea5ff
commit 6b8638a
Showing
8 changed files
with
212 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
json.key_format! camelize: :lower | ||
json.deep_format_keys! | ||
|
||
json.id invoice.id | ||
json.invoice_number invoice.invoice_number | ||
json.issue_date invoice.issue_date | ||
json.due_date invoice.due_date | ||
json.reference invoice.reference | ||
json.amount invoice.amount | ||
json.outstanding_amount invoice.outstanding_amount | ||
json.amount_paid invoice.amount_paid | ||
json.amount_due invoice.amount_due | ||
json.discount invoice.discount | ||
json.tax invoice.tax | ||
json.status invoice.status | ||
json.client do | ||
json.name client.name | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! "invoice", locals: { invoice:, client: } |
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,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! "invoice", locals: { invoice:, client: } |
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,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "InternalApi::V1::Invoices#create", type: :request do | ||
let(:company) do | ||
create(:company, clients: create_list(:client_with_invoices, 5)) | ||
end | ||
|
||
let(:user) { create(:user, current_workspace_id: company.id) } | ||
|
||
context "when user is admin" do | ||
before do | ||
create(:company_user, company:, user:) | ||
user.add_role :admin, company | ||
sign_in user | ||
end | ||
|
||
describe "invoice creation" do | ||
it "creates invoice successfully" do | ||
invoice = attributes_for( | ||
:invoice, | ||
client: company.clients.first, | ||
client_id: company.clients.first.id, | ||
status: :draft) | ||
send_request :post, internal_api_v1_invoices_path(invoice:) | ||
expect(response).to have_http_status(:ok) | ||
expected_attrs = ["amount", "amountDue", "amountPaid", | ||
"client", "discount", "dueDate", "id", | ||
"invoiceNumber", "issueDate", "outstandingAmount", | ||
"reference", "status", "tax"] | ||
expect(json_response.keys.sort).to match(expected_attrs) | ||
end | ||
|
||
it "throws 422 if client doesn't exist" do | ||
send_request :post, internal_api_v1_invoices_path( | ||
invoice: { | ||
client_id: 100000, | ||
invoice_number: "INV0001", | ||
reference: "bar", | ||
issue_date: "2022-01-01", | ||
due_date: "2022-01-31" | ||
}) | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
expect(json_response["errors"]["client"].first).to eq("must exist") | ||
end | ||
end | ||
end | ||
|
||
context "when user is employee" do | ||
before do | ||
create(:company_user, company:, user:) | ||
user.add_role :employee, company | ||
sign_in user | ||
send_request :post, internal_api_v1_invoices_path | ||
end | ||
|
||
it "is not be permitted to generate an invoice" do | ||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
context "when unauthenticated" do | ||
it "is not be permitted to generate an invoice" do | ||
send_request :post, internal_api_v1_invoices_path | ||
expect(response).to have_http_status(:unauthorized) | ||
expect(json_response["error"]).to eq("You need to sign in or sign up before continuing.") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "InternalApi::V1::Invoices#update", type: :request do | ||
let(:company) do | ||
create(:company, clients: create_list(:client_with_invoices, 5)) | ||
end | ||
|
||
let(:user) { create(:user, current_workspace_id: company.id) } | ||
|
||
context "when user is admin" do | ||
before do | ||
create(:company_user, company:, user:) | ||
user.add_role :admin, company | ||
sign_in user | ||
end | ||
|
||
describe "invoice updation" do | ||
it "updates invoice successfully" do | ||
send_request :patch, internal_api_v1_invoice_path( | ||
id: company.clients.first.invoices.first.id, params: { | ||
invoice: { | ||
reference: "foo" | ||
} | ||
}) | ||
expect(response).to have_http_status(:ok) | ||
expect(json_response["reference"]).to eq("foo") | ||
end | ||
|
||
it "throws 422 if client doesn't exist" do | ||
send_request :patch, internal_api_v1_invoice_path( | ||
id: company.clients.first.invoices.first.id, params: { | ||
invoice: { | ||
client_id: 100000, | ||
reference: "foo" | ||
} | ||
}) | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
expect(json_response["errors"]["client"].first).to eq("must exist") | ||
end | ||
end | ||
end | ||
|
||
context "when user is employee" do | ||
before do | ||
create(:company_user, company:, user:) | ||
user.add_role :employee, company | ||
sign_in user | ||
send_request :patch, internal_api_v1_invoice_path(id: company.clients.first.invoices.first.id) | ||
end | ||
|
||
it "is not be permitted to update an invoice" do | ||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
context "when unauthenticated" do | ||
it "is not be permitted to update an invoice" do | ||
send_request :patch, internal_api_v1_invoice_path(id: 1) | ||
expect(response).to have_http_status(:unauthorized) | ||
expect(json_response["error"]).to eq("You need to sign in or sign up before continuing.") | ||
end | ||
end | ||
end |