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
Deliver download links by email and give customers a library
Damian Legawiece89f525c4ba0
diff --git a/packages/admin-sdk/src/admin-client.ts b/packages/admin-sdk/src/admin-client.tsindex 4fa10de2dd..18d3c7118e 100644--- a/packages/admin-sdk/src/admin-client.ts+++ b/packages/admin-sdk/src/admin-client.ts@@ -1125,6 +1125,10 @@ export class AdminClient {resendConfirmation: (id: string, options?: RequestOptions): Promise<void> =>this.request<void>('POST', `/orders/${id}/resend_confirmation`, options),+ /** Re-sends the "your files are ready" email. No-op when the order has no downloads. */+ resendDigitalLinks: (id: string, options?: RequestOptions): Promise<void> =>+ this.request<void>('POST', `/orders/${id}/resend_digital_links`, options),+giftCards: {apply: (orderId: string,diff --git a/packages/dashboard-core/src/locales/ar.json b/packages/dashboard-core/src/locales/ar.jsonindex 89f71a049d..3cafa64d11 100644--- a/packages/dashboard-core/src/locales/ar.json+++ b/packages/dashboard-core/src/locales/ar.json@@ -886,6 +886,10 @@"created": "تم الإنشاء","customer_deleted": "تم حذف العميل","customer_saved": "تم حفظ العميل",+ "digital_asset_removed": "تمت إزالة الملف",+ "digital_asset_saved": "تم حفظ الملف",+ "digital_link_reset": "تمت استعادة إمكانية التنزيل",+ "digital_links_email_sent": "تم إرسال بريد التنزيل","gift_card_created": "تم إنشاء بطاقة الهدايا","invitation_accepted": "مرحبًا بك في Spree","invitation_resent": "تم إعادة إرسال الدعوة",diff --git a/packages/dashboard-core/src/locales/de.json b/packages/dashboard-core/src/locales/de.jsonindex 0ae3a7b5da..435f70baa0 100644--- a/packages/dashboard-core/src/locales/de.json+++ b/packages/dashboard-core/src/locales/de.json@@ -886,6 +886,10 @@"created": "Erstellt","customer_deleted": "Kunde gelöscht","customer_saved": "Kunde gespeichert",+ "digital_asset_removed": "Datei entfernt",+ "digital_asset_saved": "Datei gespeichert",+ "digital_link_reset": "Download-Zugriff wiederhergestellt",+ "digital_links_email_sent": "Download-E-Mail gesendet","gift_card_created": "Geschenkkarte erstellt","invitation_accepted": "Willkommen bei Spree","invitation_resent": "Einladung erneut gesendet",diff --git a/packages/dashboard-core/src/locales/en.json b/packages/dashboard-core/src/locales/en.jsonindex 7113c91209..5ef1303412 100644--- a/packages/dashboard-core/src/locales/en.json+++ b/packages/dashboard-core/src/locales/en.json@@ -888,6 +888,10 @@"created": "Created","customer_deleted": "Customer deleted","customer_saved": "Customer saved",+ "digital_asset_removed": "File removed",+ "digital_asset_saved": "File saved",+ "digital_link_reset": "Download access restored",+ "digital_links_email_sent": "Download email sent","gift_card_created": "Gift card created","invitation_accepted": "Welcome to Spree","invitation_resent": "Invitation resent",diff --git a/packages/dashboard-core/src/locales/fr.json b/packages/dashboard-core/src/locales/fr.jsonindex d18b45aa95..0c9c75f294 100644--- a/packages/dashboard-core/src/locales/fr.json+++ b/packages/dashboard-core/src/locales/fr.json@@ -886,6 +886,10 @@"created": "Créé","customer_deleted": "Client supprimé","customer_saved": "Client enregistré",+ "digital_asset_removed": "Fichier supprimé",+ "digital_asset_saved": "Fichier enregistré",+ "digital_link_reset": "Accès au téléchargement rétabli",+ "digital_links_email_sent": "E-mail de téléchargement envoyé","gift_card_created": "Carte cadeau créée","invitation_accepted": "Bienvenue sur Spree","invitation_resent": "Invitation renvoyée",diff --git a/packages/dashboard-core/src/locales/pl.json b/packages/dashboard-core/src/locales/pl.jsonindex a75981cbac..5e584f782d 100644--- a/packages/dashboard-core/src/locales/pl.json+++ b/packages/dashboard-core/src/locales/pl.json@@ -886,6 +886,10 @@"created": "Utworzono","customer_deleted": "Usunięto klienta","customer_saved": "Zapisano klienta",+ "digital_asset_removed": "Plik usunięty",+ "digital_asset_saved": "Plik zapisany",+ "digital_link_reset": "Przywrócono dostęp do pobierania",+ "digital_links_email_sent": "Wysłano e-mail z pobraniami","gift_card_created": "Utworzono kartę podarunkową","invitation_accepted": "Witamy w Spree","invitation_resent": "Ponownie wysłano zaproszenie",diff --git a/packages/dashboard-core/src/locales/zh-CN.json b/packages/dashboard-core/src/locales/zh-CN.jsonindex 7d8f0b4a41..826e585b7d 100644--- a/packages/dashboard-core/src/locales/zh-CN.json+++ b/packages/dashboard-core/src/locales/zh-CN.json@@ -886,6 +886,10 @@"created": "已创建","customer_deleted": "客户已删除","customer_saved": "客户已保存",+ "digital_asset_removed": "文件已删除",+ "digital_asset_saved": "文件已保存",+ "digital_link_reset": "下载权限已恢复",+ "digital_links_email_sent": "下载邮件已发送","gift_card_created": "礼品卡已创建","invitation_accepted": "欢迎使用 Spree","invitation_resent": "邀请已重新发送",diff --git a/packages/dashboard/src/components/spree/order-digital-links-card.tsx b/packages/dashboard/src/components/spree/order-digital-links-card.tsxindex f6652e9882..c88d91ad89 100644--- a/packages/dashboard/src/components/spree/order-digital-links-card.tsx+++ b/packages/dashboard/src/components/spree/order-digital-links-card.tsx@@ -4,6 +4,7 @@ import {Badge,Button,Card,+ CardAction,CardContent,CardHeader,CardTitle,@@ -15,9 +16,9 @@ import {TableRow,useConfirm,} from '@spree/dashboard-ui'-import { RotateCcwIcon } from 'lucide-react'+import { MailIcon, RotateCcwIcon } from 'lucide-react'import { useTranslation } from 'react-i18next'-import { useResetDigitalLink } from '../../hooks/use-digital-assets'+import { useResendDigitalLinks, useResetDigitalLink } from '../../hooks/use-digital-assets'interface LinkRow {link: DigitalLink@@ -29,6 +30,7 @@ export function OrderDigitalLinksCard({ order }: { order: Order }) {const { timezone } = useStore()const confirm = useConfirm()const resetLink = useResetDigitalLink(order.id)+ const resendEmail = useResendDigitalLinks(order.id)const rows: LinkRow[] = (order.items ?? []).flatMap((item) =>(item.digital_links ?? []).map((link) => ({ link, itemName: item.name })),@@ -58,6 +60,18 @@ export function OrderDigitalLinksCard({ order }: { order: Order }) {<Card><CardHeader><CardTitle>{t('admin.digital_links.title')}</CardTitle>+ <CardAction>+ <Button+ type="button"+ variant="outline"+ size="sm"+ disabled={resendEmail.isPending}+ onClick={() => resendEmail.mutate(undefined)}+ >+ <MailIcon className="mr-2 size-4" />+ {t('admin.digital_links.resend_email')}+ </Button>+ </CardAction></CardHeader><CardContent><div className="overflow-x-auto">diff --git a/packages/dashboard/src/hooks/use-digital-assets.ts b/packages/dashboard/src/hooks/use-digital-assets.tsindex a9b0e2d3aa..f8fc852752 100644--- a/packages/dashboard/src/hooks/use-digital-assets.ts+++ b/packages/dashboard/src/hooks/use-digital-assets.ts@@ -48,6 +48,13 @@ export function useDeleteDigitalAsset(productId: string) {})}+export function useResendDigitalLinks(orderId: string) {+ return useResourceMutation({+ mutationFn: () => adminClient.orders.resendDigitalLinks(orderId),+ successMessage: i18n.t('admin.messages.digital_links_email_sent'),+ })+}+export function useResetDigitalLink(orderId: string) {const { storeId } = useStore()diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.jsonindex f1d1f447eb..c6b695d8fe 100644--- a/packages/dashboard/src/locales/ar.json+++ b/packages/dashboard/src/locales/ar.json@@ -870,6 +870,7 @@"status": "الحالة"},"never_expires": "بلا انتهاء",+ "resend_email": "إعادة إرسال البريد","reset": "إعادة تعيين","reset_description": "سيحصل العميل على تنزيلات جديدة لـ «{{name}}» وتبدأ مدة الانتهاء من جديد.","reset_title": "استعادة إمكانية التنزيل؟",diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.jsonindex 2852466e49..50a01e6951 100644--- a/packages/dashboard/src/locales/de.json+++ b/packages/dashboard/src/locales/de.json@@ -853,6 +853,7 @@"status": "Status"},"never_expires": "Kein Ablauf",+ "resend_email": "E-Mail erneut senden","reset": "Zurücksetzen","reset_description": "Der Kunde erhält neue Downloads für „{{name}}“ und die Ablauffrist beginnt von vorn.","reset_title": "Download-Zugriff wiederherstellen?",diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.jsonindex 7fc8464683..86ec937ef8 100644--- a/packages/dashboard/src/locales/en.json+++ b/packages/dashboard/src/locales/en.json@@ -853,6 +853,7 @@"status": "Status"},"never_expires": "No expiry",+ "resend_email": "Resend email","reset": "Reset","reset_description": "The customer gets a fresh set of downloads for \"{{name}}\" and the expiry clock restarts.","reset_title": "Restore download access?",diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.jsonindex 3c04773985..84477a5a70 100644--- a/packages/dashboard/src/locales/fr.json+++ b/packages/dashboard/src/locales/fr.json@@ -853,6 +853,7 @@"status": "Statut"},"never_expires": "Pas d'expiration",+ "resend_email": "Renvoyer l'e-mail","reset": "Réinitialiser","reset_description": "Le client obtient de nouveaux téléchargements pour « {{name}} » et le délai d'expiration repart à zéro.","reset_title": "Rétablir l'accès au téléchargement ?",diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.jsonindex c0289aca53..6ac7d7284b 100644--- a/packages/dashboard/src/locales/pl.json+++ b/packages/dashboard/src/locales/pl.json@@ -871,6 +871,7 @@"status": "Status"},"never_expires": "Bez wygaśnięcia",+ "resend_email": "Wyślij e-mail ponownie","reset": "Zresetuj","reset_description": "Klient otrzyma nowe pobrania pliku „{{name}}”, a czas wygaśnięcia zacznie biec od nowa.","reset_title": "Przywrócić dostęp do pobierania?",diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.jsonindex 2ddfe94444..f785eabfc6 100644--- a/packages/dashboard/src/locales/zh-CN.json+++ b/packages/dashboard/src/locales/zh-CN.json@@ -848,6 +848,7 @@"status": "状态"},"never_expires": "不过期",+ "resend_email": "重新发送邮件","reset": "重置","reset_description": "客户将获得“{{name}}”的全新下载次数,过期时间重新开始计算。","reset_title": "恢复下载权限?",diff --git a/packages/sdk/src/store-client.ts b/packages/sdk/src/store-client.tsindex 5e233a2759..190ca06feb 100644--- a/packages/sdk/src/store-client.ts+++ b/packages/sdk/src/store-client.ts@@ -33,6 +33,7 @@ import type {Currency,Customer,DeliveryMethod,+ DigitalLink,GiftCard,Locale,LoginCredentials,@@ -1067,6 +1068,37 @@ export class StoreClient {this.request<StoreCredit>('GET', `/customers/me/store_credits/${id}`, options),},+ /**+ * Nested resource: Digital Links+ *+ * Everything the signed-in customer can download, across all their+ * completed orders — the data behind a "my downloads" page.+ */+ digitalLinks: {+ /**+ * List the customer's downloads, newest first.+ */+ list: (+ params?: ListParams,+ options?: RequestOptions,+ ): Promise<PaginatedResponse<DigitalLink>> =>+ this.request<PaginatedResponse<DigitalLink>>('GET', '/customers/me/digital_links', {+ ...options,+ params: transformListParams({ ...params }),+ }),++ /**+ * Get one download by ID.+ */+ /**+ * Get one download by ID. Each record carries a `download_url` — point the+ * browser at it; the response redirects to short-lived storage, so follow+ * redirects rather than reading the body.+ */+ get: (id: string, options?: RequestOptions): Promise<DigitalLink> =>+ this.request<DigitalLink>('GET', `/customers/me/digital_links/${id}`, options),+ },+/*** Nested resource: Orders (customer order history)*/diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rbindex f8003866a5..7ed31af9bb 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb@@ -8,7 +8,7 @@ module Spreescoped_resource :ordersskip_before_action :set_resource, only: [:index, :create]- before_action :set_resource, only: [:show, :update, :destroy, :complete, :cancel, :approve, :resume, :resend_confirmation]+ before_action :set_resource, only: [:show, :update, :destroy, :complete, :cancel, :approve, :resume, :resend_confirmation, :resend_digital_links]# POST /api/v3/admin/ordersdef create@@ -100,6 +100,12 @@ module Spreerender json: serialize_resource(@resource)end+ # POST /api/v3/admin/orders/:id/resend_digital_links+ def resend_digital_links+ @resource.publish_event('order.resend_digital_links_email')+ render json: serialize_resource(@resource)+ end+protecteddef model_class@@ -133,7 +139,7 @@ module Spree# Map state transition actions to :update permissiondef authorize_resource!(resource = @resource, action = action_name.to_sym)mapped_action = case action- when :complete, :cancel, :approve, :resume, :resend_confirmation+ when :complete, :cancel, :approve, :resume, :resend_confirmation, :resend_digital_links:updateelseactiondiff --git a/spree/api/app/controllers/spree/api/v3/store/customer/digital_links_controller.rb b/spree/api/app/controllers/spree/api/v3/store/customer/digital_links_controller.rbnew file mode 100644index 0000000000..1c003c6d83--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/store/customer/digital_links_controller.rb@@ -0,0 +1,37 @@+module Spree+ module Api+ module V3+ module Store+ module Customer+ # Everything the signed-in customer can download, across all their+ # orders — the storefront's "my downloads" page. Individual links are+ # also nested in each order, but a library is unusable that way.+ class DigitalLinksController < ResourceController+ prepend_before_action :require_authentication!++ protected++ def model_class+ Spree::DigitalLink+ end++ def serializer_class+ Spree.api.digital_link_serializer+ end++ def scope+ Spree::DigitalLink.+ joins(line_item: :order).+ where(spree_orders: { id: current_user.orders.complete.where(store: current_store) }).+ order(created_at: :desc)+ end++ def scope_includes+ [digital_asset: { attachment_attachment: :blob }]+ end+ end+ end+ end+ end+ end+enddiff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex f9b9b2b298..1559213286 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -111,6 +111,7 @@ Spree::Core::Engine.add_routes do# upserting one kind without knowing whether it exists yet.resources :tax_identifiers, only: [:index], controller: 'tax_identifiers'resource :tax_identifier, only: [:show, :update, :destroy], controller: 'tax_identifiers'+ resources :digital_links, only: [:index, :show]resources :payment_setup_sessions, only: [:create, :show] domember dopatch :complete@@ -660,6 +661,7 @@ Spree::Core::Engine.add_routes dopatch :approvepatch :resumepost :resend_confirmation+ post :resend_digital_linksendresources :items, only: [:index, :show, :create, :update, :destroy], controller: 'orders/items'diff --git a/spree/api/spec/controllers/spree/api/v3/store/customer/digital_links_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/customer/digital_links_controller_spec.rbnew file mode 100644index 0000000000..d0862ed932--- /dev/null+++ b/spree/api/spec/controllers/spree/api/v3/store/customer/digital_links_controller_spec.rb@@ -0,0 +1,63 @@+require 'spec_helper'++RSpec.describe Spree::Api::V3::Store::Customer::DigitalLinksController, type: :controller do+ render_views++ include_context 'API v3 Store'++ let(:order) { create(:completed_order_with_totals, store: store, customer: user) }+ let(:line_item) { order.line_items.first }+ let(:digital_asset) { create(:digital_asset, variant: line_item.variant) }+ let!(:digital_link) { create(:digital_link, digital_asset: digital_asset, line_item: line_item) }++ describe 'GET #index' do+ context 'when signed in' do+ before { request.headers.merge!(bearer_headers) }++ it 'lists everything the customer can download' do+ get :index++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].map { |l| l['id'] }).to include(digital_link.prefixed_id)+ end++ it 'includes a download url for each link' do+ get :index++ expect(json_response['data'].first['download_url']).to include(digital_link.token)+ end++ it 'excludes links belonging to another customer' do+ other_order = create(:completed_order_with_totals, store: store)+ other_item = other_order.line_items.first+ other_link = create(:digital_link,+ digital_asset: create(:digital_asset, variant: other_item.variant),+ line_item: other_item)++ get :index++ expect(json_response['data'].map { |l| l['id'] }).not_to include(other_link.prefixed_id)+ end++ it 'excludes links from incomplete orders' do+ pending_order = create(:order_with_line_items, store: store, customer: user)+ pending_item = pending_order.line_items.first+ pending_link = create(:digital_link,+ digital_asset: create(:digital_asset, variant: pending_item.variant),+ line_item: pending_item)++ get :index++ expect(json_response['data'].map { |l| l['id'] }).not_to include(pending_link.prefixed_id)+ end+ end++ context 'when not signed in' do+ it 'is unauthorized' do+ get :index++ expect(response).to have_http_status(:unauthorized)+ end+ end+ end+enddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex b5ee6ab54e..522120663f 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -874,6 +874,14 @@ en:developers: Developersdigital:digital_delivery: Digital Delivery+ digital_asset_mailer:+ files_ready_email:+ available_until: Available until %{date}+ dear_customer: Dear %{name},+ instructions: The files from your order %{number} are ready to download.+ store_team: "%{store_name} Team"+ subject: Your files are ready+ thanks: Thank you for your business.digital_assets: Digital assetsdiscontinue_on: Discontinue Ondiscontinued: Discontinueddiff --git a/spree/emails/app/helpers/spree/digital_asset_helper.rb b/spree/emails/app/helpers/spree/digital_asset_helper.rbnew file mode 100644index 0000000000..364c37a4ce--- /dev/null+++ b/spree/emails/app/helpers/spree/digital_asset_helper.rb@@ -0,0 +1,16 @@+module Spree+ module DigitalAssetHelper+ # Emails need an absolute link — the route helper only produces a path, and+ # the download endpoint lives on the backend rather than the storefront.+ #+ # @param digital_link [Spree::DigitalLink]+ # @return [String]+ def digital_link_download_url(digital_link)+ path = Spree::Core::Engine.routes.url_helpers.api_v3_store_digital_link_download_path(+ token: digital_link.token+ )++ URI.join(digital_link.order.store.formatted_url, path).to_s+ end+ end+enddiff --git a/spree/emails/app/mailers/spree/digital_asset_mailer.rb b/spree/emails/app/mailers/spree/digital_asset_mailer.rbnew file mode 100644index 0000000000..ae600e9f4f--- /dev/null+++ b/spree/emails/app/mailers/spree/digital_asset_mailer.rb@@ -0,0 +1,27 @@+module Spree+ class DigitalAssetMailer < BaseMailer+ helper Spree::MailHelper+ helper Spree::DigitalAssetHelper++ # Sent once an order's downloads are ready. Kept separate from the order+ # confirmation so it can be re-sent on its own, and so a storefront that+ # replaces the confirmation email does not silently lose the links.+ def files_ready_email(order, resend = false)+ @order = order.respond_to?(:id) ? order : Spree::Order.find(order)+ @digital_links = @order.digital_links+ return if @digital_links.empty?++ current_store = @order.store+ with_store_locale(current_store, @order.locale) do+ subject = order_email_subject(+ current_store,+ Spree.t('digital_asset_mailer.files_ready_email.subject'),+ @order.number,+ resend: resend+ )+ mail(to: @order.email, subject: subject, store_url: current_store.storefront_url,+ template_path: 'spree/digital_asset_mailer', template_name: 'files_ready_email')+ end+ end+ end+enddiff --git a/spree/emails/app/subscribers/spree/digital_asset_email_subscriber.rb b/spree/emails/app/subscribers/spree/digital_asset_email_subscriber.rbnew file mode 100644index 0000000000..63ab08f77a--- /dev/null+++ b/spree/emails/app/subscribers/spree/digital_asset_email_subscriber.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++module Spree+ class DigitalAssetEmailSubscriber < Spree::Subscriber+ # Links are created during completion, before order.placed fires, so the+ # order already knows everything the email needs.+ subscribes_to 'order.placed', 'order.resend_digital_links_email'++ on 'order.placed', :send_files_ready_email+ on 'order.resend_digital_links_email', :resend_files_ready_email++ private++ def send_files_ready_email(event)+ deliver(event)+ end++ def resend_files_ready_email(event)+ deliver(event, resend: true)+ end++ def deliver(event, resend: false)+ order = find_order(event)+ return unless order+ return if order.digital_links.empty?+ return unless order.store.prefers_send_consumer_transactional_emails?++ DigitalAssetMailer.files_ready_email(order.id, resend).deliver_later+ end++ def find_order(event)+ Spree::Order.find_by_prefix_id(event.payload['id'])+ end+ end+enddiff --git a/spree/emails/app/views/spree/digital_asset_mailer/files_ready_email.html.erb b/spree/emails/app/views/spree/digital_asset_mailer/files_ready_email.html.erbnew file mode 100644index 0000000000..42c722859b--- /dev/null+++ b/spree/emails/app/views/spree/digital_asset_mailer/files_ready_email.html.erb@@ -0,0 +1,20 @@+<%= render layout: 'spree/shared/mailer_hero' do %>+ <h1><%= Spree.t('digital_asset_mailer.files_ready_email.subject') %></h1>+ <p class="greeting"><%= Spree.t('digital_asset_mailer.files_ready_email.dear_customer', name: name_for(@order)) %></p>+ <p><%= Spree.t('digital_asset_mailer.files_ready_email.instructions', number: @order.number) %></p>+<% end %>++<table width="100%" cellpadding="0" cellspacing="0" border="0">+ <% @digital_links.each do |digital_link| %>+ <tr>+ <td style="padding: 8px 0;">+ <a href="<%= digital_link_download_url(digital_link) %>"><%= digital_link.filename %></a>+ <% if digital_link.expires_at %>+ <div style="font-size: 12px; color: #666;">+ <%= Spree.t('digital_asset_mailer.files_ready_email.available_until', date: l(digital_link.expires_at.to_date, format: :long)) %>+ </div>+ <% end %>+ </td>+ </tr>+ <% end %>+</table>diff --git a/spree/emails/app/views/spree/digital_asset_mailer/files_ready_email.text.erb b/spree/emails/app/views/spree/digital_asset_mailer/files_ready_email.text.erbnew file mode 100644index 0000000000..b8ef1c1349--- /dev/null+++ b/spree/emails/app/views/spree/digital_asset_mailer/files_ready_email.text.erb@@ -0,0 +1,10 @@+<%= Spree.t('digital_asset_mailer.files_ready_email.dear_customer', name: name_for(@order)) %>++<%= Spree.t('digital_asset_mailer.files_ready_email.instructions', number: @order.number) %>++<% @digital_links.each do |digital_link| %>+<%= digital_link.filename %>+<%= digital_link_download_url(digital_link) %>+<% if digital_link.expires_at %><%= Spree.t('digital_asset_mailer.files_ready_email.available_until', date: l(digital_link.expires_at.to_date, format: :long)) %>+<% end %>+<% end %>diff --git a/spree/emails/spec/mailers/spree/digital_asset_mailer_spec.rb b/spree/emails/spec/mailers/spree/digital_asset_mailer_spec.rbnew file mode 100644index 0000000000..d6b8fa67d1--- /dev/null+++ b/spree/emails/spec/mailers/spree/digital_asset_mailer_spec.rb@@ -0,0 +1,37 @@+require 'spec_helper'+require 'email_spec'++describe Spree::DigitalAssetMailer, type: :mailer do+ include EmailSpec::Helpers+ include EmailSpec::Matchers++ let(:store) { @default_store }+ let(:order) { create(:order_with_line_items, store: store, email: '[email protected]') }+ let(:line_item) { order.line_items.first }+ let(:digital_asset) { create(:digital_asset, variant: line_item.variant) }+ let!(:digital_link) { create(:digital_link, digital_asset: digital_asset, line_item: line_item) }++ subject(:message) { described_class.files_ready_email(order) }++ it 'goes to the customer' do+ expect(message.to).to eq([order.email])+ expect(message.from).to eq([store.mail_from_address])+ end++ it 'names the order in the subject' do+ expect(message.subject).to include(order.number)+ end++ it 'links every purchased file with an absolute url' do+ body = message.body.encoded++ expect(body).to include(digital_link.filename.to_s)+ expect(body).to match(%r{https?://[^"\s]+/api/v3/store/digital_links/#{digital_link.token}})+ end++ it 'is not sent when the order has no downloads' do+ plain_order = create(:order_with_line_items, store: store, email: '[email protected]')++ expect(described_class.files_ready_email(plain_order).to).to be_nil+ end+enddiff --git a/spree/emails/spec/subscribers/spree/digital_asset_email_subscriber_spec.rb b/spree/emails/spec/subscribers/spree/digital_asset_email_subscriber_spec.rbnew file mode 100644index 0000000000..1ed942f92f--- /dev/null+++ b/spree/emails/spec/subscribers/spree/digital_asset_email_subscriber_spec.rb@@ -0,0 +1,54 @@+# frozen_string_literal: true++require 'spec_helper'++RSpec.describe Spree::DigitalAssetEmailSubscriber do+ let(:store) { @default_store }+ let(:order) { create(:completed_order_with_totals, store: store) }+ let(:line_item) { order.line_items.first }+ let(:subscriber) { described_class.new }++ def mock_event(order)+ double('Event', payload: { 'id' => order.prefixed_id })+ end++ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: true))+ end++ context 'when the order has downloads' do+ before do+ digital_asset = create(:digital_asset, variant: line_item.variant)+ create(:digital_link, digital_asset: digital_asset, line_item: line_item)+ end++ it 'sends the files-ready email on order.placed' do+ expect(Spree::DigitalAssetMailer).to receive(:files_ready_email).+ with(order.id, false).and_return(double(deliver_later: true))++ subscriber.send(:send_files_ready_email, mock_event(order))+ end++ it 'marks a resend as such so the subject says so' do+ expect(Spree::DigitalAssetMailer).to receive(:files_ready_email).+ with(order.id, true).and_return(double(deliver_later: true))++ subscriber.send(:resend_files_ready_email, mock_event(order))+ end++ it 'stays quiet when the store suppresses consumer emails' do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: false))+ expect(Spree::DigitalAssetMailer).not_to receive(:files_ready_email)++ subscriber.send(:send_files_ready_email, mock_event(order))+ end+ end++ context 'when the order has no downloads' do+ it 'sends nothing' do+ expect(Spree::DigitalAssetMailer).not_to receive(:files_ready_email)++ subscriber.send(:send_files_ready_email, mock_event(order))+ end+ end+end