-
Notifications
You must be signed in to change notification settings - Fork 77
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
[BE]APIs to save invoices #256
Conversation
Current Code Coverage Percent of this PR:91.28 %Files having coverage below 100%
|
|
||
def update | ||
authorize invoice | ||
if invoice.update!(invoice_params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never use bang with update for an if condition. if something fails it will raise an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine since we're handling the errors in error_handler.rb
. It'll show up in the API response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, if you send
{
"reference": "foo",
"issue_date": "2022-02-01",
"due_date": "2022-01-31"
}
you get
{
"errors": {
"due_date": [
"must be greater than or equal to 2022-02-01"
]
},
"notice": "Failed to saved changes"
}
Which is fine, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will rescue the error, but it wont return any boolean. So you can remove if condition, because there is no use of if condition here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, yes if
is unnecessary. Thanks for pointing out. Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One suggestion
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These attributes are repeating in both create and update JSON builder, we can extract them into a _record.json.builder
partial and use json.partial!
to reuse it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip! Fixed.
authorize Invoice | ||
render :create, locals: { | ||
invoice: Invoice.create!(invoice_params), | ||
client: Client.find(invoice_params[:client_id]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move Client.find
into a different method load_client
and use that instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
invoice: { | ||
client_id: company.clients.first.id, | ||
invoice_number: "INV0001", | ||
reference: "bar", | ||
issue_date: "2022-01-01", | ||
due_date: "2022-01-31" | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invoice: { | |
client_id: company.clients.first.id, | |
invoice_number: "INV0001", | |
reference: "bar", | |
issue_date: "2022-01-01", | |
due_date: "2022-01-31" | |
}) | |
invoice: attributes_for :invoice, client: company.clients.first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Notion card
https://www.notion.so/saeloun/6afc9500b2cc42af86be55f37894fb09?v=b4528f26f4424af7beaf036262796cb3&p=28db3bab184142269bb66230afa67635
Summary
This contains APIs to create and update invoices.
Preview
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Checklist: