Skip to content

Commit

Permalink
Add BCC email to spree_store
Browse files Browse the repository at this point in the history
Adds a BCC email field to the spree store edit page.
This indicates a BCC email that you want all order confirmation
emails to be BCCed to. As indicated in #3615, this is a feature
that I think a lot of eCommerce stores are interested in, at
the very least to server as a notification of a new order.

Also adds the ability to add/update this field through the API
  • Loading branch information
seand7565 committed May 30, 2020
1 parent 1aa554f commit 449ac7d
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/app/helpers/spree/api/api_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ def required_fields_for(model)

@@store_attributes = [
:id, :name, :url, :meta_description, :meta_keywords, :seo_title,
:mail_from_address, :default_currency, :code, :default, :available_locales
:mail_from_address, :default_currency, :code, :default, :available_locales,
:bcc_email
]

@@store_credit_history_attributes = [
Expand Down
4 changes: 4 additions & 0 deletions api/openapi/solidus-api.oas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5976,6 +5976,8 @@ components:
type: integer
mail_from_address:
type: string
bcc_email:
type: string
meta_description:
type: string
meta_keywords:
Expand Down Expand Up @@ -6499,6 +6501,8 @@ components:
type: string
mail_from_address:
type: string
bcc_email:
type: string
cart_tax_country_iso:
type: string
taxonomy-input:
Expand Down
7 changes: 6 additions & 1 deletion api/spec/requests/spree/api/stores_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Spree
"meta_keywords" => nil,
"seo_title" => nil,
"mail_from_address" => "[email protected]",
"bcc_email" => nil,
"default_currency" => nil,
"code" => store.code,
"default" => true,
Expand All @@ -46,6 +47,7 @@ module Spree
"meta_keywords" => nil,
"seo_title" => nil,
"mail_from_address" => "[email protected]",
"bcc_email" => nil,
"default_currency" => nil,
"code" => non_default_store.code,
"default" => false,
Expand All @@ -64,6 +66,7 @@ module Spree
"meta_keywords" => nil,
"seo_title" => nil,
"mail_from_address" => "[email protected]",
"bcc_email" => nil,
"default_currency" => nil,
"code" => store.code,
"default" => true,
Expand All @@ -85,12 +88,14 @@ module Spree
it "can update an existing store" do
store_hash = {
url: "spree123.example.com",
mail_from_address: "[email protected]"
mail_from_address: "[email protected]",
bcc_email: "[email protected]"
}
put spree.api_store_path(store), params: { store: store_hash }
expect(response.status).to eq(200)
expect(store.reload.url).to eql "spree123.example.com"
expect(store.reload.mail_from_address).to eql "[email protected]"
expect(store.reload.bcc_email).to eql "[email protected]"
end

context "deleting a store" do
Expand Down
6 changes: 6 additions & 0 deletions backend/app/views/spree/admin/stores/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<%= f.error_message_on :mail_from_address %>
<% end %>

<%= f.field_container :bcc_email do %>
<%= f.label :bcc_email %>
<%= f.text_field :bcc_email, class: 'fullwidth' %>
<%= f.error_message_on :bcc_email %>
<% end %>

<%= f.field_container :default_currency do %>
<%= f.label :default_currency %>
<%= f.select :default_currency,
Expand Down
4 changes: 4 additions & 0 deletions core/app/mailers/spree/base_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def from_address(store)
store.mail_from_address
end

def bcc_address(store)
store.bcc_email
end

def money(amount, currency = Spree::Config[:currency])
Spree::Money.new(amount, currency: currency).to_s
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/mailers/spree/order_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def confirm_email(order, resend = false)
@store = @order.store
subject = build_subject(t('.subject'), resend)

mail(to: @order.email, from: from_address(@store), subject: subject)
mail(to: @order.email, bcc: bcc_address(@store), from: from_address(@store), subject: subject)
end

def cancel_email(order, resend = false)
Expand Down
1 change: 1 addition & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ en:
default: Default
default_currency: Default Currency
mail_from_address: Mail From Address
bcc_email: BCC Email
meta_description: Meta Description
meta_keywords: Meta Keywords
name: Site Name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddBccEmailToSpreeStores < ActiveRecord::Migration[5.2]
def change
add_column :spree_stores, :bcc_email, :string
end
end
3 changes: 2 additions & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ module PermittedAttributes

@@store_attributes = [:name, :url, :seo_title, :meta_keywords,
:meta_description, :default_currency,
:mail_from_address, :cart_tax_country_iso]
:mail_from_address, :cart_tax_country_iso,
:bcc_email]

@@taxonomy_attributes = [:name]

Expand Down
7 changes: 6 additions & 1 deletion core/spec/mailers/order_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
product = stub_model(Spree::Product, name: %{The "BEST" product})
variant = stub_model(Spree::Variant, product: product)
price = stub_model(Spree::Price, variant: variant, amount: 5.00)
store = FactoryBot.build :store, mail_from_address: "[email protected]"
store = FactoryBot.build :store, mail_from_address: "[email protected]", bcc_email: "[email protected]"
line_item = stub_model(Spree::LineItem, variant: variant, order: order, quantity: 1, price: 4.99)
allow(variant).to receive_messages(default_price: price)
allow(order).to receive_messages(line_items: [line_item])
Expand All @@ -21,6 +21,11 @@
expect(message.from).to eq ["[email protected]"]
end

it "uses the order's store for the bcc address" do
message = Spree::OrderMailer.confirm_email(order)
expect(message.bcc).to eq ["[email protected]"]
end

it "doesn't aggressively escape double quotes in confirmation body" do
confirmation_email = Spree::OrderMailer.confirm_email(order)
expect(confirmation_email.body).not_to include("&quot;")
Expand Down

0 comments on commit 449ac7d

Please sign in to comment.