From 449ac7da56818bae0203a55b11160bc4b62d57c6 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 30 May 2020 11:52:03 -0500 Subject: [PATCH] Add BCC email to spree_store 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 --- api/app/helpers/spree/api/api_helpers.rb | 3 ++- api/openapi/solidus-api.oas.yml | 4 ++++ api/spec/requests/spree/api/stores_controller_spec.rb | 7 ++++++- backend/app/views/spree/admin/stores/_form.html.erb | 6 ++++++ core/app/mailers/spree/base_mailer.rb | 4 ++++ core/app/mailers/spree/order_mailer.rb | 2 +- core/config/locales/en.yml | 1 + .../20200530111458_add_bcc_email_to_spree_stores.rb | 7 +++++++ core/lib/spree/permitted_attributes.rb | 3 ++- core/spec/mailers/order_mailer_spec.rb | 7 ++++++- 10 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 core/db/migrate/20200530111458_add_bcc_email_to_spree_stores.rb diff --git a/api/app/helpers/spree/api/api_helpers.rb b/api/app/helpers/spree/api/api_helpers.rb index 76d4510867a..18b2bbd4c29 100644 --- a/api/app/helpers/spree/api/api_helpers.rb +++ b/api/app/helpers/spree/api/api_helpers.rb @@ -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 = [ diff --git a/api/openapi/solidus-api.oas.yml b/api/openapi/solidus-api.oas.yml index 02bcecbaf29..0a3d6a90503 100644 --- a/api/openapi/solidus-api.oas.yml +++ b/api/openapi/solidus-api.oas.yml @@ -5976,6 +5976,8 @@ components: type: integer mail_from_address: type: string + bcc_email: + type: string meta_description: type: string meta_keywords: @@ -6499,6 +6501,8 @@ components: type: string mail_from_address: type: string + bcc_email: + type: string cart_tax_country_iso: type: string taxonomy-input: diff --git a/api/spec/requests/spree/api/stores_controller_spec.rb b/api/spec/requests/spree/api/stores_controller_spec.rb index 7ed6d6f52df..b9266fdbd47 100644 --- a/api/spec/requests/spree/api/stores_controller_spec.rb +++ b/api/spec/requests/spree/api/stores_controller_spec.rb @@ -33,6 +33,7 @@ module Spree "meta_keywords" => nil, "seo_title" => nil, "mail_from_address" => "spree@example.org", + "bcc_email" => nil, "default_currency" => nil, "code" => store.code, "default" => true, @@ -46,6 +47,7 @@ module Spree "meta_keywords" => nil, "seo_title" => nil, "mail_from_address" => "spree@example.org", + "bcc_email" => nil, "default_currency" => nil, "code" => non_default_store.code, "default" => false, @@ -64,6 +66,7 @@ module Spree "meta_keywords" => nil, "seo_title" => nil, "mail_from_address" => "spree@example.org", + "bcc_email" => nil, "default_currency" => nil, "code" => store.code, "default" => true, @@ -85,12 +88,14 @@ module Spree it "can update an existing store" do store_hash = { url: "spree123.example.com", - mail_from_address: "me@example.com" + mail_from_address: "me@example.com", + bcc_email: "bcc@example.net" } 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 "me@example.com" + expect(store.reload.bcc_email).to eql "bcc@example.net" end context "deleting a store" do diff --git a/backend/app/views/spree/admin/stores/_form.html.erb b/backend/app/views/spree/admin/stores/_form.html.erb index 2b2de57d853..5f4902f9402 100644 --- a/backend/app/views/spree/admin/stores/_form.html.erb +++ b/backend/app/views/spree/admin/stores/_form.html.erb @@ -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, diff --git a/core/app/mailers/spree/base_mailer.rb b/core/app/mailers/spree/base_mailer.rb index e47d86165b6..34404bba393 100644 --- a/core/app/mailers/spree/base_mailer.rb +++ b/core/app/mailers/spree/base_mailer.rb @@ -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 diff --git a/core/app/mailers/spree/order_mailer.rb b/core/app/mailers/spree/order_mailer.rb index 120ade51573..ed60ffa8dc0 100644 --- a/core/app/mailers/spree/order_mailer.rb +++ b/core/app/mailers/spree/order_mailer.rb @@ -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) diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index 281b4c7668b..cdc9f2d0cf6 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -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 diff --git a/core/db/migrate/20200530111458_add_bcc_email_to_spree_stores.rb b/core/db/migrate/20200530111458_add_bcc_email_to_spree_stores.rb new file mode 100644 index 00000000000..f0351b8f616 --- /dev/null +++ b/core/db/migrate/20200530111458_add_bcc_email_to_spree_stores.rb @@ -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 diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb index 3a646d8bd3c..95e80d29276 100644 --- a/core/lib/spree/permitted_attributes.rb +++ b/core/lib/spree/permitted_attributes.rb @@ -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] diff --git a/core/spec/mailers/order_mailer_spec.rb b/core/spec/mailers/order_mailer_spec.rb index 438061caefe..0fa91fa6144 100644 --- a/core/spec/mailers/order_mailer_spec.rb +++ b/core/spec/mailers/order_mailer_spec.rb @@ -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: "store@example.com" + store = FactoryBot.build :store, mail_from_address: "store@example.com", bcc_email: "bcc@example.com" 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]) @@ -21,6 +21,11 @@ expect(message.from).to eq ["store@example.com"] end + it "uses the order's store for the bcc address" do + message = Spree::OrderMailer.confirm_email(order) + expect(message.bcc).to eq ["bcc@example.com"] + 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(""")