diff --git a/app/views/purchase_notifier/product_order_notification.html.haml b/app/views/purchase_notifier/product_order_notification.html.haml index cc2199988b..7f96fcbdd6 100644 --- a/app/views/purchase_notifier/product_order_notification.html.haml +++ b/app/views/purchase_notifier/product_order_notification.html.haml @@ -12,6 +12,11 @@ %td %strong= OrderDetail.human_attribute_name(:ordered_by) %td= mail_to @order.created_by_user.email, @order.created_by_user.full_name + - if @order.created_by != @order.user_id + %tr + %td + %strong= text(".order_for") + %td= mail_to @order.user.email, @order.user.full_name %tr %td %strong= Account.model_name.human diff --git a/app/views/purchase_notifier/product_order_notification.text.haml b/app/views/purchase_notifier/product_order_notification.text.haml index f854594f81..6e742058f4 100644 --- a/app/views/purchase_notifier/product_order_notification.text.haml +++ b/app/views/purchase_notifier/product_order_notification.text.haml @@ -1,6 +1,9 @@ #{Order.human_attribute_name(:ordered_at)}: #{l(@order_detail.ordered_at, format: :receipt)} #{Facility.model_name.human}: #{@order.facility} -#{OrderDetail.human_attribute_name(:ordered_by)}: #{@order.created_by_user.full_name} +#{OrderDetail.human_attribute_name(:ordered_by)}: #{@order.created_by_user.full_name}, #{@order.created_by_user.email} + +- if @order.created_by != @order.user_id + #{text(".order_for")}: #{@order.user.full_name}, #{@order.user.email} #{Account.model_name.human}: #{@order.account} = OrderDetail.model_name.to_s.titleize.pluralize diff --git a/config/locales/en.purchase_notifier.yml b/config/locales/en.purchase_notifier.yml index e93984c660..a4fcab5714 100644 --- a/config/locales/en.purchase_notifier.yml +++ b/config/locales/en.purchase_notifier.yml @@ -3,6 +3,7 @@ en: purchase_notifier: product_order_notification: subject: "%{product} Order Notification" + order_for: Order For order_notification: subject: "!app_name! Order Notification" order_receipt: diff --git a/spec/mailers/purchase_notifier_spec.rb b/spec/mailers/purchase_notifier_spec.rb index 5d4c337855..19b59309bf 100644 --- a/spec/mailers/purchase_notifier_spec.rb +++ b/spec/mailers/purchase_notifier_spec.rb @@ -8,43 +8,81 @@ let(:order) { create(:purchased_order, product:) } let(:product) { create(:setup_instrument, facility:) } let(:user) { order.user } + let(:other_user) { create(:user) } describe ".order_notification" do - before { described_class.order_notification(order, recipient).deliver_now } - + let(:deliver_mail) { described_class.order_notification(order, recipient).deliver_now } let(:recipient) { "orders@example.net" } - it "generates an order notification", :aggregate_failures do - expect(email.to).to eq [recipient] - expect(email.subject).to include("Order Notification") - expect(email.html_part.to_s).to match(/Ordered By.+#{user.full_name}/m) - expect(email.reply_to).to eq [order.created_by_user.email] - expect(email.text_part.to_s).to include("Ordered By: #{user.full_name}") - expect(email.html_part.to_s).to match(/Payment Source.+#{order.account}/m) - expect(email.text_part.to_s).to include("Payment Source: #{order.account}") - expect(email.html_part.to_s).not_to include("Thank you for your order") - expect(email.text_part.to_s).not_to include("Thank you for your order") + context "when created by order user" do + before { deliver_mail } + + it "generates an order notification", :aggregate_failures do + expect(email.to).to eq [recipient] + expect(email.subject).to include("Order Notification") + expect(email.html_part.to_s).to match(/Ordered By.+#{user.full_name}/m) + expect(email.reply_to).to eq [order.created_by_user.email] + expect(email.text_part.to_s).to include("Ordered By: #{user.full_name}") + expect(email.html_part.to_s).to match(/Payment Source.+#{order.account}/m) + expect(email.text_part.to_s).to include("Payment Source: #{order.account}") + expect(email.html_part.to_s).not_to include("Thank you for your order") + expect(email.text_part.to_s).not_to include("Thank you for your order") + expect(email.html_part.to_s).to_not include("Order For") + expect(email.text_part.to_s).to_not include("Order For") + end + end + + context "when created on behalf of user" do + before do + order.update_attribute(:created_by_user, other_user) + deliver_mail + end + + it "does include order for" do + expect(email.html_part.to_s).to include("Order For") + expect(email.text_part.to_s).to include("Order For") + end end end describe ".product_order_notification" do - before { described_class.product_order_notification(order_detail, recipient).deliver_now } - let(:order_detail) { order.order_details.first } let(:recipient) { "orders@example.net" } - it "generates a product order notification", :aggregate_failures do - expect(email.to).to eq [recipient] - expect(email.subject).to include("#{product} Order Notification") - expect(email.html_part.to_s).to include(order_detail.to_s) - expect(email.text_part.to_s).to include(order_detail.to_s) - expect(email.html_part.to_s).to match(/Ordered By.+#{user.full_name}/m) - expect(email.text_part.to_s).to include("Ordered By: #{user.full_name}") - expect(email.reply_to).to eq [order.created_by_user.email] - expect(email.html_part.to_s).to match(/Payment Source.+#{order.account}/m) - expect(email.text_part.to_s).to include("Payment Source: #{order.account}") - expect(email.html_part.to_s).not_to include("Thank you for your order") - expect(email.text_part.to_s).not_to include("Thank you for your order") + let(:deliver_mail) do + described_class.product_order_notification(order_detail, recipient).deliver_now + end + + context "when created by order user" do + before { deliver_mail } + + it "generates a product order notification", :aggregate_failures do + expect(email.to).to eq [recipient] + expect(email.subject).to include("#{product} Order Notification") + expect(email.html_part.to_s).to include(order_detail.to_s) + expect(email.text_part.to_s).to include(order_detail.to_s) + expect(email.html_part.to_s).to match(/Ordered By.+#{user.full_name}/m) + expect(email.text_part.to_s).to include("Ordered By: #{user.full_name}") + expect(email.reply_to).to eq [order.created_by_user.email] + expect(email.html_part.to_s).to match(/Payment Source.+#{order.account}/m) + expect(email.text_part.to_s).to include("Payment Source: #{order.account}") + expect(email.html_part.to_s).not_to include("Thank you for your order") + expect(email.text_part.to_s).not_to include("Thank you for your order") + expect(email.html_part.to_s).to_not include("Order For") + expect(email.text_part.to_s).to_not include("Order For") + end + end + + context "when created on behalf of user" do + before do + order.update_attribute(:created_by_user, other_user) + deliver_mail + end + + it "does include order for" do + expect(email.html_part.to_s).to include("Order For") + expect(email.text_part.to_s).to include("Order For") + end end end @@ -65,6 +103,8 @@ expect(email.text_part.to_s).to include("Payment Source: #{order.account}") expect(email.html_part.to_s).to include("Thank you for your order") expect(email.text_part.to_s).to include("Thank you for your order") + expect(email.html_part.to_s).to_not include("Order For") + expect(email.text_part.to_s).to_not include("Order For") end context "when ordered on behalf of another user" do @@ -78,6 +118,11 @@ .to include("Ordered By: #{administrator.full_name}") end + it "does include order for" do + expect(email.html_part.to_s).to include("Order For") + expect(email.text_part.to_s).to include("Order For") + end + context "with a note" do let(:note) { "*NOTE CONTENT*" } it { expect(email.text_part.to_s).to include("*NOTE CONTENT*") } @@ -85,48 +130,4 @@ end end end - - context "order for" do - let(:order_for_field) { "Order For" } - let(:recipient) { "test@example.com" } - - context "order created by admin" do - let(:admin) { create(:user, :administrator) } - - before do - order.update_attribute(:created_by_user, admin) - described_class.order_notification(order, recipient).deliver_now - end - - shared_examples "includes involved users info" do |part| - let(:mail_content) { email.send(part).to_s } - - it "includes created by and order for in #{part}" do - expect(mail_content).to include(order_for_field) - expect(mail_content).to include(order.user.full_name) - expect(mail_content).to include(order.user.email) - expect(mail_content).to include(order.created_by_user.email) - expect(mail_content).to include(order.created_by_user.full_name) - end - end - - include_examples "includes involved users info", :html_part - include_examples "includes involved users info", :text_part - end - - context "order created by user" do - before do - order.update_attribute(:user, order.created_by_user) - described_class.order_notification(order, recipient).deliver_now - end - - it "does not include order for" do - expect(email.html_part.to_s).to_not include(order_for_field) - end - - it "does not include order for" do - expect(email.text_part.to_s).to_not include(order_for_field) - end - end - end end