Skip to content

Commit

Permalink
Add order for in product order notification
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinco committed Dec 6, 2024
1 parent 1f35a50 commit 4b96531
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.purchase_notifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
141 changes: 71 additions & 70 deletions spec/mailers/purchase_notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { "[email protected]" }

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) { "[email protected]" }

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

Expand All @@ -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
Expand All @@ -78,55 +118,16 @@
.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*") }
it { expect(email.html_part.to_s).to include("*NOTE CONTENT*") }
end
end
end

context "order for" do
let(:order_for_field) { "Order For" }
let(:recipient) { "[email protected]" }

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

0 comments on commit 4b96531

Please sign in to comment.