From 9b80676d7d2aa23c7e60367fbc81e06a0676e145 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 12 Jul 2023 11:49:42 +0100 Subject: [PATCH] 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 --- app/mailers/spree/order_mailer.rb | 9 +++++++- spec/mailers/order_mailer_spec.rb | 37 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/mailers/spree/order_mailer.rb b/app/mailers/spree/order_mailer.rb index a2fda5d94f..9495566974 100644 --- a/app/mailers/spree/order_mailer.rb +++ b/app/mailers/spree/order_mailer.rb @@ -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 diff --git a/spec/mailers/order_mailer_spec.rb b/spec/mailers/order_mailer_spec.rb index 9a2d3c4066..86b7d96b50 100644 --- a/spec/mailers/order_mailer_spec.rb +++ b/spec/mailers/order_mailer_spec.rb @@ -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