update the logic to send invoice

When the invoices feature is enabled, for every order, we check if
1. a new invoice must be generated
2. or, the latest invoice needs to be updated

the invoice rendrer input depends on the invoices flag.
if the feature is enabled, the input is supposed to be an invoice presenter
This commit is contained in:
Mohamed ABDELLANI
2023-07-12 11:49:42 +01:00
parent 7daa4d3a63
commit 9b80676d7d
2 changed files with 45 additions and 1 deletions

View File

@@ -48,7 +48,14 @@ module Spree
def invoice_email(order_or_order_id)
@order = find_order(order_or_order_id)
pdf = InvoiceRenderer.new.render_to_string(@order)
renderer_data = if OpenFoodNetwork::FeatureToggle.enabled?(:invoices)
OrderInvoiceGenerator.new(@order).generate_or_update_latest_invoice
@order.invoices.first.presenter
else
@order
end
pdf = InvoiceRenderer.new.render_to_string(renderer_data)
attach_file("invoice-#{@order.number}.pdf", pdf)
I18n.with_locale valid_locale(@order.user) do

View File

@@ -216,4 +216,41 @@ describe Spree::OrderMailer do
end
end
end
describe "#invoice_email" do
subject(:email) { described_class.invoice_email(order) }
let(:order) { create(:completed_order_with_totals) }
let!(:invoice_data_generator){ InvoiceDataGenerator.new(order) }
let!(:invoice){
create(:invoice, order:,
data: invoice_data_generator.serialize_for_invoice)
}
let(:generator){ double(:generator) }
let(:renderer){ double(:renderer) }
before do
allow(OrderInvoiceGenerator).to receive(:new).with(order).and_return(generator)
allow(InvoiceRenderer).to receive(:new).and_return(renderer)
end
context "When invoices feature is not enabled" do
it "should call the invoice render with order as argument" do
expect(generator).not_to receive(:generate_or_update_latest_invoice)
expect(order).not_to receive(:invoices)
expect(renderer).to receive(:render_to_string).with(order)
email.deliver_now
end
end
context "When invoices feature is enabled" do
before do
Flipper.enable(:invoices)
end
it "should call the invoice renderer with invoice's presenter as argument" do
expect(generator).to receive(:generate_or_update_latest_invoice)
expect(order).to receive(:invoices).and_return([invoice])
expect(renderer).to receive(:render_to_string).with(invoice.presenter)
email.deliver_now
end
end
end
end