Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Wire coupon-code CSV export into the promotion editor

Damian Legawiec62c1b9179239
diff --git a/packages/admin/src/routes/_authenticated/$storeId/promotions/$promotionId.tsx b/packages/admin/src/routes/_authenticated/$storeId/promotions/$promotionId.tsx
index 8c4096af71..614ec3560a 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/promotions/$promotionId.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/promotions/$promotionId.tsx
@@ -1,6 +1,6 @@
import type { PromotionAction, PromotionRule, ResourceTypeDefinition } from '@spree/admin-sdk'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
-import { PlusIcon, TrashIcon } from 'lucide-react'
+import { DownloadIcon, PlusIcon, TrashIcon } from 'lucide-react'
import { useEffect, useState } from 'react'
import { Controller, useForm } from 'react-hook-form'
import { Can } from '@/components/spree/can'
@@ -30,6 +30,7 @@ import {
} from '@/components/ui/sheet'
import { Switch } from '@/components/ui/switch'
import { Textarea } from '@/components/ui/textarea'
+import { useExport } from '@/hooks/use-export'
import {
useCreatePromotionAction,
useCreatePromotionRule,
@@ -852,15 +853,40 @@ function PromotionCouponCodesCard({ promotionId }: { promotionId: string }) {
const totalCount = codesData?.meta?.count ?? codes.length
const totalPages = codesData?.meta?.pages ?? 1
+ const exportMutation = useExport()
+ function handleExport() {
+ exportMutation.mutate({
+ type: 'Spree::Exports::CouponCodes',
+ record_selection: 'filtered',
+ search_params: { promotion_id_eq: promotionId },
+ })
+ }
+
return (
<Card>
<CardHeader>
- <CardTitle>Coupon codes</CardTitle>
- <p className="text-sm text-muted-foreground">
- {totalCount > 0
- ? `${totalCount} auto-generated codes. Read-only; regenerate by changing the promotion's number-of-codes setting.`
- : "Auto-generated codes for this promotion. Codes are read-only; regenerate by changing the promotion's number-of-codes setting."}
- </p>
+ <div className="flex items-start justify-between gap-3">
+ <div>
+ <CardTitle>Coupon codes</CardTitle>
+ <p className="text-sm text-muted-foreground">
+ {totalCount > 0
+ ? `${totalCount} auto-generated codes. Read-only; regenerate by changing the promotion's number-of-codes setting.`
+ : "Auto-generated codes for this promotion. Codes are read-only; regenerate by changing the promotion's number-of-codes setting."}
+ </p>
+ </div>
+ {totalCount > 0 && (
+ <Button
+ type="button"
+ size="sm"
+ variant="outline"
+ onClick={handleExport}
+ disabled={exportMutation.isPending}
+ >
+ <DownloadIcon className="size-4" />
+ {exportMutation.isPending ? 'Exporting…' : 'Export CSV'}
+ </Button>
+ )}
+ </div>
</CardHeader>
<CardContent>
{codes.length === 0 ? (
diff --git a/spree/core/app/models/spree/export.rb b/spree/core/app/models/spree/export.rb
index b476a742c3..f53037c8ab 100644
--- a/spree/core/app/models/spree/export.rb
+++ b/spree/core/app/models/spree/export.rb
@@ -130,12 +130,34 @@ module Spree
def records_to_export
if search_params.present?
- scope.ransack(search_params.is_a?(String) ? JSON.parse(search_params.to_s).to_h : search_params)
+ params = search_params.is_a?(String) ? JSON.parse(search_params.to_s).to_h : search_params
+ scope.ransack(decode_prefixed_id_filters(params))
else
scope.ransack
end.result
end
+ # Replace any prefixed IDs in `search_params` with their raw DB IDs so
+ # Ransack can match them. Without this, an admin filtering an export by
+ # a foreign key (`promotion_id_eq: 'promo_xxx'`, `vendor_id_in: [...]`)
+ # would always get zero rows. We only touch values that look like
+ # prefixed IDs — anything else (numeric IDs, code strings, ranges,
+ # state names) passes through untouched.
+ def decode_prefixed_id_filters(params)
+ params.transform_values { |value| decode_search_value(value) }
+ end
+
+ def decode_search_value(value)
+ case value
+ when String
+ Spree::PrefixedId.prefixed_id?(value) ? (Spree::PrefixedId.decode_prefixed_id(value) || value) : value
+ when Array
+ value.map { |v| decode_search_value(v) }
+ else
+ value
+ end
+ end
+
def scope_includes
[]
end
diff --git a/spree/core/spec/models/spree/export_spec.rb b/spree/core/spec/models/spree/export_spec.rb
index f90034d402..cabdf863b0 100644
--- a/spree/core/spec/models/spree/export_spec.rb
+++ b/spree/core/spec/models/spree/export_spec.rb
@@ -78,6 +78,24 @@ RSpec.describe Spree::Export, :job, type: :model do
expect(export.records_to_export.count).to eq(matching_products.count)
end
end
+
+ context 'with a prefixed-id filter' do
+ let(:target) { matching_products.first }
+ let(:search_params) { { id_eq: target.prefixed_id }.to_json }
+
+ it 'decodes prefixed IDs before handing to Ransack' do
+ expect(export.records_to_export).to contain_exactly(target)
+ end
+ end
+
+ context 'with a prefixed-id array filter' do
+ let(:targets) { matching_products.first(2) }
+ let(:search_params) { { id_in: targets.map(&:prefixed_id) }.to_json }
+
+ it 'decodes each value' do
+ expect(export.records_to_export).to match_array(targets)
+ end
+ end
end
describe '#send_export_done_email' do