Skip to content

Commit

Permalink
[#162477] Add "Order for" in purchase email (#4817)
Browse files Browse the repository at this point in the history
* Add order for in order receipt and product order emails
  • Loading branch information
joaquinco authored Dec 9, 2024
1 parent c921238 commit c2cd740
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 36 deletions.
10 changes: 5 additions & 5 deletions app/mailers/purchase_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def product_order_notification(order_detail, recipient)
@order_detail = OrderDetailPresenter.new(order_detail)
attach_reservation_ical(order_detail.reservation) if order_detail.reservation.present?
subject = text("views.purchase_notifier.product_order_notification.subject", product: order_detail.product)
send_nucore_mail to: recipient, subject: subject, reply_to: @order.created_by_user.email
send_nucore_mail to: recipient, subject:, reply_to: @order.created_by_user.email
end

# Notifies the specified facility staff member if any order is placed within a facility
def order_notification(order, recipient)
@order = order
attach_all_icals_from_order(@order)
subject = text("views.purchase_notifier.order_notification.subject")
send_nucore_mail to: recipient, subject: subject, reply_to: @order.created_by_user.email, template_name: "order_receipt"
send_nucore_mail to: recipient, subject:, reply_to: @order.created_by_user.email, template_name: "order_receipt"
end

# Custom order forms send out a confirmation email when filled out by a
Expand All @@ -39,7 +39,7 @@ def order_receipt(args)
private

def attach_all_icals_from_order(order)
order.order_details.map(&:reservation).compact.each do |reservation|
order.order_details.filter_map(&:reservation).each do |reservation|
attach_reservation_ical(reservation)
end
end
Expand All @@ -53,9 +53,9 @@ def attach_reservation_ical(reservation)

def send_nucore_mail(to:, subject:, reply_to: nil, template_name: nil)
if reply_to
mail(subject: subject, to: to, template_name: template_name, reply_to: reply_to)
mail(subject:, to:, template_name:, reply_to:)
else
mail(subject: subject, to: to, template_name: template_name)
mail(subject:, to:, template_name:)
end
end

Expand Down
5 changes: 5 additions & 0 deletions app/views/purchase_notifier/order_receipt.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
2 changes: 2 additions & 0 deletions app/views/purchase_notifier/order_receipt.text.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#{Order.human_attribute_name(:ordered_at)}: #{l(@order.initial_ordered_at, format: :receipt)}
#{Facility.model_name.human}: #{@order.facility}
#{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
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
2 changes: 2 additions & 0 deletions config/locales/en.purchase_notifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ en:
purchase_notifier:
product_order_notification:
subject: "%{product} Order Notification"
order_for: Order For
order_notification:
subject: "!app_name! Order Notification"
order_receipt:
subject: "!app_name! Order Receipt"
intro: "Thank you for your order on the !app_name! website."
order_for: Order For
outro: "All prices are estimates. Actual cost is assigned when the order is complete."
total: Total
105 changes: 75 additions & 30 deletions spec/mailers/purchase_notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,84 @@
RSpec.describe PurchaseNotifier do
let(:email) { ActionMailer::Base.deliveries.last }
let(:facility) { create(:setup_facility) }
let(:order) { create(:purchased_order, product: product) }
let(:product) { create(:setup_instrument, facility: facility) }
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 @@ -53,7 +91,7 @@

before(:each) do
order.order_details.first.update_attribute(:note, note) if note.present?
described_class.order_receipt(order: order, user: user).deliver_now
described_class.order_receipt(order:, user:).deliver_now
end

it "generates a receipt", :aggregate_failures do
Expand All @@ -65,11 +103,13 @@
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
let(:administrator) { create(:user, :administrator) }
let(:order) { create(:purchased_order, product: product, created_by: administrator.id) }
let(:order) { create(:purchased_order, product:, created_by: administrator.id) }

it "mentions who placed the order in the receipt", :aggregate_failures do
expect(email.html_part.to_s)
Expand All @@ -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*") }
Expand Down

0 comments on commit c2cd740

Please sign in to comment.