Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: second-round review findings on the email template editor
Damian Legawiec6fa54fd4126b
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsxindex 17265622ec..a3fe086ae9 100644--- a/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx+++ b/packages/dashboard/src/routes/_authenticated/$storeId/settings/emails/templates/$templateId.tsx@@ -301,13 +301,17 @@ function EmailTemplateEditor({confirmLabel: t('admin.email_templates.actions.revert'),variant: 'destructive',},- // The shared version has no draft anyone edits here, so there is no copy- // to check against; this language's draft is not the one it affects.- () =>- revert.mutateAsync({- language: versionLanguage,- lock_version: forEveryLanguage ? undefined : lockVersion,- }),+ async () => {+ if (!forEveryLanguage) return revert.mutateAsync({ language, lock_version: lockVersion })++ // The shared version has no draft anyone edits here, so there is no+ // copy to check; its answer is for every language, so this language's+ // own state (and its draft) is read again instead.+ await revert.mutateAsync({ language: versionLanguage })+ const latest = await onFetchLatest()+ if (!latest) throw new Error(t('admin.errors.failed_to_load'))+ return latest+ },)const handleRestore = async (revisionId: string) => {diff --git a/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rbindex 47dadea03d..d35f81c1d8 100644--- a/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/email_templates/revisions_controller.rb@@ -8,6 +8,8 @@ module Spreeclass RevisionsController < ResourceControllerinclude Spree::Api::V3::Admin::EmailTemplateLookup+ before_action { authorize! :read, Spree::EmailTemplate }+protecteddef model_classdiff --git a/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rbindex 060b1fb20d..24085ca237 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/email_templates/revisions_controller_spec.rb@@ -23,4 +23,23 @@ RSpec.describe Spree::Api::V3::Admin::EmailTemplates::RevisionsController, type:expect(json_response['data'].map { |revision| revision['id'] }).to eq([newer.prefixed_id, older.prefixed_id])expect(json_response['data'].last['published_by_type']).to eq('admin_user')end++ context 'as a staffer without the email templates permission' do+ let(:staffer) do+ create(:admin_user, :without_admin_role).tap do |user|+ create(:role_user, user: user, role: create(:role, name: 'orders_only', permissions: %w[write_orders]))+ end+ end+ let(:headers) do+ { 'Authorization' => "Bearer #{Spree::Api::V3::TestingSupport.generate_jwt(staffer, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_ADMIN)}" }+ end++ it 'cannot read the history' do+ create(:email_template_revision, email_template: template)++ get :index, params: { email_template_id: id }, as: :json++ expect(response).to have_http_status(:forbidden)+ end+ endenddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rbindex 4d08204748..7f3de97e0a 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/email_templates_controller_spec.rb@@ -52,6 +52,17 @@ RSpec.describe Spree::Api::V3::Admin::EmailTemplatesController, type: :controlleexpect(json_response).to include('body' => 'For everyone', 'customized' => true, 'published_language' => 'any')end+ it 'stops reporting a changed default once the draft keeps its own version against the new one' do+ create(:email_template, store: store, key: editable_key, body: 'Published', base_body: 'An older default')+ current = Spree::Emails::TemplateResolver.for_mailers.find_default(editable_key)+ create(:email_template_draft, store: store, key: editable_key, body: 'Mine',+ base_subject: current.subject, base_body: current.body)++ get :show, params: { id: id }, as: :json++ expect(json_response).to include('default_changed' => false, 'base_body' => nil)+ end+it 'reads the version for the language asked for' docreate(:email_template, store: store, key: editable_key, locale: 'de', body: 'German')diff --git a/spree/core/app/models/spree/email_templates/entry.rb b/spree/core/app/models/spree/email_templates/entry.rbindex 1f338e8fc2..7aef27a983 100644--- a/spree/core/app/models/spree/email_templates/entry.rb+++ b/spree/core/app/models/spree/email_templates/entry.rb@@ -73,7 +73,7 @@ module Spree# What customers receive now.def subject- readable(customized? ? published.subject : default&.subject)+ readable(customized? ? published.subject : default&.subject, escape: false)enddef body@@ -81,7 +81,7 @@ module Spreeenddef default_subject- readable(default&.subject)+ readable(default&.subject, escape: false)enddef default_body@@ -89,7 +89,7 @@ module Spreeenddef base_subject- readable(outdated_version&.base_subject)+ readable(outdated_version&.base_subject, escape: false)enddef base_body@@ -101,15 +101,17 @@ module Spree## @return [Spree::EmailTemplate, Spree::EmailTemplateDraft, nil]def outdated_version- [draft, published].compact.find { |version| version.default_changed?(default) }+ # An open draft is what gets published next, so it alone decides.+ version = draft || published+ version if version&.default_changed?(default)endprivate- def readable(text)+ def readable(text, escape: true)return text if locale == Spree::EmailTemplate::ANY_LOCALE- Spree::Emails::TranslationInliner.call(text, locale: locale)+ Spree::Emails::TranslationInliner.call(text, locale: locale, escape: escape)endendenddiff --git a/spree/core/app/services/spree/emails/translation_inliner.rb b/spree/core/app/services/spree/emails/translation_inliner.rbindex 21fae2c697..bb9b54b851 100644--- a/spree/core/app/services/spree/emails/translation_inliner.rb+++ b/spree/core/app/services/spree/emails/translation_inliner.rb@@ -21,13 +21,16 @@ module Spree# @param source [String, nil] a template's subject or body# @param locale [String, Symbol] the language to write the text in+ # @param escape [Boolean] whether the text is HTML: false for a subject,+ # which renders unescaped as a mail header# @return [String, nil]- def self.call(source, locale:)- new(locale).call(source)+ def self.call(source, locale:, escape: true)+ new(locale, escape).call(source)end- def initialize(locale)+ def initialize(locale, escape = true)@locale = locale+ @escape = escapeenddef call(source)@@ -50,7 +53,7 @@ module Spree"{{ '#{text}' #{filters.strip} }}"else- interpolate(text, arguments) { |literal| ERB::Util.html_escape(literal) }+ interpolate(text, arguments) { |literal| literal_text(literal) }endend@@ -62,7 +65,7 @@ module Spreeif arguments.blank? && text.exclude?("'") && text !~ INTERPOLATION"{% assign #{variable} = '#{text}' %}"else- body = interpolate(text, arguments) { |literal| ERB::Util.html_escape(literal) }+ body = interpolate(text, arguments) { |literal| literal_text(literal) }body && "{% capture #{variable} %}#{body}{% endcapture %}"endend@@ -79,6 +82,10 @@ module Spreeend.joinend+ def literal_text(literal)+ @escape ? ERB::Util.html_escape(literal) : literal+ end+def parse_arguments(arguments)return {} if arguments.blank?diff --git a/spree/core/app/workflows/spree/email_templates/save_draft.rb b/spree/core/app/workflows/spree/email_templates/save_draft.rbindex fc72508b39..3186924942 100644--- a/spree/core/app/workflows/spree/email_templates/save_draft.rb+++ b/spree/core/app/workflows/spree/email_templates/save_draft.rb@@ -60,7 +60,7 @@ module Spreepublished = store.email_templates.published.where(key: key, locale: [draft.locale, Spree::EmailTemplate::ANY_LOCALE]).min_by { |template| template.locale == draft.locale ? 0 : 1 }- draft.subject = readable(published&.subject || default&.subject)+ draft.subject = readable(published&.subject || default&.subject, escape: false)draft.body = readable(published&.body || default&.body)draft.base_subject = published ? published.base_subject : default&.subjectdraft.base_body = published ? published.base_body : default&.body@@ -71,10 +71,10 @@ module Spreedraft.base_body = default&.bodyend- def readable(text)+ def readable(text, escape: true)return text if draft.locale == Spree::EmailTemplate::ANY_LOCALE- Spree::Emails::TranslationInliner.call(text, locale: draft.locale)+ Spree::Emails::TranslationInliner.call(text, locale: draft.locale, escape: escape)enddef defaultdiff --git a/spree/core/spec/services/spree/emails/translation_inliner_spec.rb b/spree/core/spec/services/spree/emails/translation_inliner_spec.rbindex 70eaa9dc1d..68b2c35e9a 100644--- a/spree/core/spec/services/spree/emails/translation_inliner_spec.rb+++ b/spree/core/spec/services/spree/emails/translation_inliner_spec.rb@@ -1,8 +1,8 @@require 'spec_helper'describe Spree::Emails::TranslationInliner do- def inline(source, locale: :en)- described_class.call(source, locale: locale)+ def inline(source, locale: :en, escape: true)+ described_class.call(source, locale: locale, escape: escape)endaround do |example|@@ -31,6 +31,10 @@ describe Spree::Emails::TranslationInliner doexpect(inline("{{ 'inliner_spec.markup' | t }}")).to eq('Tom & Jerry <shop>')end+ it 'leaves text unescaped for a subject, which renders as a plain mail header' do+ expect(inline("{{ 'inliner_spec.quoted' | t }} & more", escape: false)).to eq("Don't wait & more")+ end+it 'keeps later filters on text without values' doexpect(inline("{{ 'inliner_spec.plain' | t | upcase }}")).to eq("{{ 'Thanks for shopping' | upcase }}")end