Closed dbvnz closed 4 years ago
I've come to a solution for this, new working code snippet below for reference.
This may be a bit hacky as it's setting the complete_record_downloaded
flag on the object which doesn't quite match the state of the object so there could be side effects for other methods I'm not aware of, but for my purposes this appears to work as intended.
The find_in_batches
method retrieves the line items along with the other necessary Invoice attributes for updating them successfully so that solves the issue I was having.
After reading the Xero API documentation again I found that sending an update to an Invoice requires listing at least all the Line Item IDs otherwise they'll be removed during the update.
issue_date = Date.new(2019,11,22)
# Retrieving this broad selection of invoices here as it's used later in this worker for other purposes
xero_all_invoices = []
xeroizer.Invoice.find_in_batches(where: "AmountDue>0") do |batch|
batch.each do |invoice|
invoice.complete_record_downloaded = true
xero_all_invoices << invoice
end
end
xero_invoices = xero_all_invoices.select{|i| i.date == issue_date && i.status == 'DRAFT'}
xeroizer.Invoice.batch_save do
xero_invoices.each{|i| i.status = 'AUTHORISED' }
end
# Save successful
I'm using the API to retrieve all Invoices in this month, then retrieving all CreditNotes, then I plan to allocate credits as appropriate.
Before doing that, I need to authorise all my Invoices and CreditNotes. When I get to the point of saving the updates, I'm hitting the
Xeroizer::OAuth::RateLimitExceeded
exception, being thrown by the validation step which seems to be individually retrieving each record via #download_complete_record! at lib/xeroizer/record/record_association_helper.rb:131.I've tried manually setting the complete_record_downloaded flag on all the objects before saving them but then I'm getting validation errors saying the invoices have no line items, which is expected.
This is what I'm trying to do, is there a different way I should be approaching this so it doesn't try to revalidate all the records before sending the updates?
The relevant part of the backtrace from the exception is as follows: