fix order invoice generator must update the latest invoice

This commit is contained in:
Mohamed ABDELLANI
2023-08-21 16:10:05 +01:00
parent af73720157
commit aaf1d22ccc
2 changed files with 14 additions and 1 deletions

View File

@@ -13,7 +13,7 @@ class OrderInvoiceGenerator
data: invoice_data
)
elsif comparator.can_update_latest_invoice?
order.invoices.last.update!(
order.invoices.first.update!(
date: Time.zone.today,
data: invoice_data
)

View File

@@ -43,6 +43,19 @@ describe OrderInvoiceGenerator do
expect{ subject }.to change{ latest_invoice.reload.data }
.and change{ order.invoices.count }.by(0)
end
context "when there are more than one invoice" do
before do
latest_invoice.update!(number: 2, created_at: 1.day.ago)
create(:invoice, order:, number: 1, created_at: 2.days.ago)
end
it "should update the most recent invoice" do
expect{ subject }.to change{ latest_invoice.reload.data }
.and change{ latest_invoice.date }.to(Time.zone.today)
.and change{ latest_invoice.number }.by(0)
.and change{ order.invoices.count }.by(0)
end
end
end
context "when can't generate new invoice or update latest invoice" do