合并:接入拍下未付款自动改价规则
This commit is contained in:
commit
44f0b5c559
39 files changed
+1433
-161
No files matched your search
@@ -146,6 +146,7 @@ export const updateShippingRule = async (rule: Partial<ShippingRule>): Promise<O
|
||||
const triggerType = rule.trigger_type || 'order_paid';
|
||||
// triggerName 触发名称,用于当前 API 处理流程。
|
||||
const triggerName: Record<string, string> = {
|
||||
order_created: '拍下未付款自动改价',
|
||||
order_paid: '付款后自动发货',
|
||||
buyer_reviewed: '评价后发送赠品',
|
||||
review_missing_timeout: '超时未评价求评价',
|
||||
|
||||
@@ -158,9 +158,9 @@ export interface Item {
|
||||
}
|
||||
|
||||
/** 由当前 feature adapter 归一后的 AutomationTriggerType UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export type AutomationTriggerType = 'order_paid' | 'buyer_reviewed' | 'review_missing_timeout';
|
||||
export type AutomationTriggerType = 'order_created' | 'order_paid' | 'buyer_reviewed' | 'review_missing_timeout';
|
||||
/** 由当前 feature adapter 归一后的 AutomationActionType UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export type AutomationActionType = 'confirm_shipment' | 'send_card' | 'send_text';
|
||||
export type AutomationActionType = 'confirm_shipment' | 'send_card' | 'send_text' | 'adjust_price';
|
||||
|
||||
// Rules
|
||||
/** 由当前 feature adapter 归一后的 ShippingRule UI 模型;不直接暴露 HTTP DTO。 */
|
||||
|
||||
@@ -4,6 +4,7 @@ Bot,
|
||||
CheckCircle2,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
CircleDollarSign,
|
||||
Clock3,
|
||||
Edit,
|
||||
Layers3,
|
||||
@@ -25,7 +26,7 @@ import { useRulesData } from '../hooks';
|
||||
import { filterAutomationIssues } from '../issueState';
|
||||
import { useRuleActions } from '../ruleActions';
|
||||
import type { AutomationTriggerType,RulesProps,RulesTab } from '../types';
|
||||
import { accentClasses,accountLabel,actionSummary,buildReviewConfig,cardActionsForTrigger,statusPill,triggerMeta,triggerOrder } from '../utils';
|
||||
import { accentClasses,accountLabel,actionSummary,adjustPriceTarget,buildReviewConfig,cardActionsForTrigger,statusPill,triggerMeta,triggerOrder } from '../utils';
|
||||
|
||||
// Rules 是规则 feature 在旧页面目录下保留的兼容入口组件。
|
||||
const Rules: React.FC<RulesProps> = ({ initialDeliveryTarget, onDeliveryTargetHandled }) => {
|
||||
@@ -105,7 +106,7 @@ const Rules: React.FC<RulesProps> = ({ initialDeliveryTarget, onDeliveryTargetHa
|
||||
editingAutomationRule, setEditingAutomationRule,
|
||||
editingReplyRule, setEditingReplyRule, defaultForm, setDefaultForm, selectedRuleItem, isMultiSpecRule, currentTrigger,
|
||||
currentMeta, reviewConfig, displayVariants, openAutomationRule, openNewAutomationRule, handleTriggerChange,
|
||||
handleAutomationItemChange, updateVariant, appendDeliveryContent, handleSaveAutomationRule, handleDeleteAutomation,
|
||||
handleAutomationItemChange, updateVariant, updateAdjustPriceTarget, updateAdjustPriceNotifyText, appendDeliveryContent, handleSaveAutomationRule, handleDeleteAutomation,
|
||||
handleToggleAutomation, handleResolveRunIssue, handleResolveDeferredIssue, handleAddReplyRule, handleSaveReplyRule,
|
||||
handleDeleteReply, openDefaultReplyModal, handleSaveDefaultReply, handleDeleteDefaultReply, handleClearDefaultReplyRecords,
|
||||
} = ruleActions;
|
||||
@@ -722,7 +723,7 @@ const Rules: React.FC<RulesProps> = ({ initialDeliveryTarget, onDeliveryTargetHa
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{selectedRuleItem && currentTrigger !== 'review_missing_timeout' && (
|
||||
{selectedRuleItem && currentTrigger !== 'review_missing_timeout' && currentTrigger !== 'order_created' && (
|
||||
<div className="mt-4 rounded-2xl bg-gray-50 border border-gray-100 p-4">
|
||||
<div className="flex flex-wrap items-center gap-2 mb-2">
|
||||
<span className="px-3 py-1.5 rounded-lg bg-gray-100 text-gray-700 text-xs font-bold">{selectedRuleItem.item_title || selectedRuleItem.item_id}</span>
|
||||
@@ -738,7 +739,43 @@ const Rules: React.FC<RulesProps> = ({ initialDeliveryTarget, onDeliveryTargetHa
|
||||
)}
|
||||
</section>
|
||||
|
||||
{currentTrigger !== 'review_missing_timeout' ? (
|
||||
{currentTrigger === 'order_created' ? (
|
||||
<section className="bg-white rounded-3xl border border-gray-100 p-5">
|
||||
<div className="flex items-center gap-2 mb-4">
|
||||
<CircleDollarSign className="w-5 h-5 text-violet-600" />
|
||||
<h4 className="font-black text-gray-900">改价设置</h4>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-bold text-gray-700 mb-2">目标价格(元)</label>
|
||||
<input
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
value={adjustPriceTarget(editingAutomationRule.actions)}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => updateAdjustPriceTarget(event.target.value)}
|
||||
placeholder="例如:9.9"
|
||||
className="w-full ios-input px-4 py-3 rounded-xl"
|
||||
/>
|
||||
<p className="text-xs text-gray-500 mt-2">买家拍下未付款后,系统会把该笔订单价格修改为此金额(0.01 - 1000000 元,最多两位小数)。</p>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-bold text-gray-700 mb-2">改价后提醒买家(可选)</label>
|
||||
<textarea
|
||||
value={editingAutomationRule.actions?.find(/* 当前回调处理集合中的单个元素。 */ action => action.action_type === 'send_text')?.message_template || ''}
|
||||
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => updateAdjustPriceNotifyText(event.target.value)}
|
||||
placeholder="例如:已为您改好价格,请尽快支付哦~"
|
||||
className="w-full ios-input px-4 py-3 rounded-xl h-24 resize-none"
|
||||
/>
|
||||
<p className="text-xs text-gray-500 mt-2">留空则只改价不发送消息。</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-4 rounded-2xl bg-violet-50 border border-violet-100 p-4">
|
||||
<p className="text-xs leading-5 text-violet-700">
|
||||
改价仅对买家尚未付款的订单生效;订单已付款、已关闭或平台限制改价时任务会记录失败原因。建议配合商品说明引导买家「先拍下再等改价」。
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
) : currentTrigger !== 'review_missing_timeout' ? (
|
||||
<section className="bg-white rounded-3xl border border-gray-100 p-5">
|
||||
<div className="flex items-start justify-between gap-4 mb-4">
|
||||
<div>
|
||||
|
||||
@@ -110,6 +110,44 @@ describe('useRuleActions', /* 当前回调验证规则页面动作协调器的
|
||||
hook.unmount();
|
||||
});
|
||||
|
||||
test('拍下改价规则校验目标价格并剔除空提醒动作', /* 当前回调验证拍下改价草稿与保存边界。 */ async () => {
|
||||
// hook 是规则动作 Hook 的真实 React 状态实例。
|
||||
const hook = renderHook(() => useRuleActionsHarness());
|
||||
act(/* 当前回调打开拍下改价规则弹窗。 */ () => hook.result.current.openNewAutomationRule('order_created'));
|
||||
expect(hook.result.current.editingAutomationRule?.variants).toEqual([]);
|
||||
|
||||
// 目标价格为空时保存被拒绝且不触达接口。
|
||||
await act(/* 当前回调执行空价格保存。 */ async () => hook.result.current.handleSaveAutomationRule());
|
||||
expect(updateShippingMock).not.toHaveBeenCalled();
|
||||
|
||||
act(/* 当前回调写入合法目标价格。 */ () => hook.result.current.updateAdjustPriceTarget('9.9'));
|
||||
act(/* 当前回调写入空白提醒文案。 */ () => hook.result.current.updateAdjustPriceNotifyText(' '));
|
||||
await act(/* 当前回调执行拍下改价保存。 */ async () => hook.result.current.handleSaveAutomationRule());
|
||||
expect(updateShippingMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
trigger_type: 'order_created',
|
||||
variants: [],
|
||||
actions: [expect.objectContaining({ action_type: 'adjust_price', config_json: '{"target_price":"9.9"}' })],
|
||||
}));
|
||||
hook.unmount();
|
||||
});
|
||||
|
||||
test('拍下改价规则保存带文案的可选提醒动作', /* 当前回调验证改价提醒文案的保存。 */ async () => {
|
||||
// hook 是规则动作 Hook 的真实 React 状态实例。
|
||||
const hook = renderHook(() => useRuleActionsHarness());
|
||||
act(/* 当前回调打开拍下改价规则弹窗。 */ () => hook.result.current.openNewAutomationRule('order_created'));
|
||||
act(/* 当前回调写入合法目标价格。 */ () => hook.result.current.updateAdjustPriceTarget('12'));
|
||||
act(/* 当前回调写入买家提醒文案。 */ () => hook.result.current.updateAdjustPriceNotifyText('已改价,请支付'));
|
||||
await act(/* 当前回调执行拍下改价保存。 */ async () => hook.result.current.handleSaveAutomationRule());
|
||||
expect(updateShippingMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
trigger_type: 'order_created',
|
||||
actions: [
|
||||
expect.objectContaining({ action_type: 'adjust_price', config_json: '{"target_price":"12"}' }),
|
||||
expect.objectContaining({ action_type: 'send_text', message_template: '已改价,请支付' }),
|
||||
],
|
||||
}));
|
||||
hook.unmount();
|
||||
});
|
||||
|
||||
test('关键词和默认回复动作分别读取、保存并刷新', /* 当前回调验证两个回复页签的动作边界。 */ async () => {
|
||||
// hook 是规则动作 Hook 的真实 React 状态实例。
|
||||
const hook = renderHook(() => useRuleActionsHarness());
|
||||
|
||||
@@ -16,7 +16,7 @@ updateShippingRule,
|
||||
} from './api';
|
||||
import { finishRuleSubmission,idleRuleSubmitState,startRuleSubmission,type RuleSubmitState } from './interactionState';
|
||||
import type { AutomationTriggerType,Card,DefaultReplyForm,Item,ReplyRule,RulesProps,RulesTab,ShippingRule,ShippingVariant } from './types';
|
||||
import { boolFlag,buildReviewConfig,cardActionsForTrigger,defaultRuleName,emptyVariant,parseJSONObject,shouldReplaceGeneratedName,triggerMeta } from './utils';
|
||||
import { adjustPriceTarget,boolFlag,buildAdjustPriceConfig,buildReviewConfig,cardActionsForTrigger,defaultRuleName,emptyVariant,isValidAdjustPrice,parseJSONObject,shouldReplaceGeneratedName,triggerMeta } from './utils';
|
||||
|
||||
// RuleActionsOptions 描述规则动作协调器依赖的页面数据、刷新函数和外部联动目标。
|
||||
export interface RuleActionsOptions {
|
||||
@@ -106,6 +106,10 @@ export interface RuleActionsState {
|
||||
handleAutomationItemChange: (itemID: string) => void;
|
||||
// updateVariant 更新指定发货规格的字段。
|
||||
updateVariant: (index: number, patch: Partial<ShippingVariant>) => void;
|
||||
// updateAdjustPriceTarget 更新拍下改价草稿的目标价格。
|
||||
updateAdjustPriceTarget: (targetPrice: string) => void;
|
||||
// updateAdjustPriceNotifyText 更新拍下改价草稿的可选买家提醒文案。
|
||||
updateAdjustPriceNotifyText: (text: string) => void;
|
||||
// appendDeliveryContent 追加一行发货规格。
|
||||
appendDeliveryContent: () => void;
|
||||
// handleSaveAutomationRule 保存当前自动化规则。
|
||||
@@ -198,7 +202,7 @@ export const useRuleActions = ({
|
||||
name: defaultRuleName(trigger, itemLabel), trigger_type: trigger, cookie_id: cookieID, item_id: itemID,
|
||||
item_title: item?.item_title || '', item_keyword: itemLabel, card_group_id: 0, priority: 100, enabled: true,
|
||||
config_json: trigger === 'review_missing_timeout' ? buildReviewConfig() : '{}', actions: cardActionsForTrigger(trigger),
|
||||
variants: trigger === 'review_missing_timeout' ? [] : [emptyVariant()],
|
||||
variants: trigger === 'review_missing_timeout' || trigger === 'order_created' ? [] : [emptyVariant()],
|
||||
};
|
||||
}, [items, selectedAccountId]);
|
||||
|
||||
@@ -211,7 +215,7 @@ export const useRuleActions = ({
|
||||
trigger_type: trigger,
|
||||
config_json: trigger === 'review_missing_timeout' ? buildReviewConfig(rule.config_json) : (rule.config_json || '{}'),
|
||||
actions: rule.actions?.length ? rule.actions.map(/* 当前回调复制自动化动作。 */ action => ({ ...action })) : cardActionsForTrigger(trigger, rule.card_group_id),
|
||||
variants: rule.variants?.length ? rule.variants.map(/* 当前回调复制发货规格。 */ variant => ({ ...variant })) : (trigger === 'review_missing_timeout' ? [] : [emptyVariant()]),
|
||||
variants: rule.variants?.length ? rule.variants.map(/* 当前回调复制发货规格。 */ variant => ({ ...variant })) : (trigger === 'review_missing_timeout' || trigger === 'order_created' ? [] : [emptyVariant()]),
|
||||
});
|
||||
setShowAutomationModal(true);
|
||||
}, []);
|
||||
@@ -288,7 +292,7 @@ export const useRuleActions = ({
|
||||
name: shouldReplaceGeneratedName(editingAutomationRule.name) ? defaultRuleName(trigger, itemLabel) : editingAutomationRule.name,
|
||||
card_group_id: currentCardID, config_json: trigger === 'review_missing_timeout' ? buildReviewConfig(editingAutomationRule.config_json) : '{}',
|
||||
actions: cardActionsForTrigger(trigger, currentCardID),
|
||||
variants: trigger === 'review_missing_timeout' ? [] : (editingAutomationRule.variants?.length ? editingAutomationRule.variants : [{ ...emptyVariant(), card_id: currentCardID }]),
|
||||
variants: trigger === 'review_missing_timeout' || trigger === 'order_created' ? [] : (editingAutomationRule.variants?.length ? editingAutomationRule.variants : [{ ...emptyVariant(), card_id: currentCardID }]),
|
||||
});
|
||||
}, [editingAutomationRule, selectedRuleItem]);
|
||||
|
||||
@@ -313,6 +317,35 @@ export const useRuleActions = ({
|
||||
setEditingAutomationRule({ ...editingAutomationRule, variants: next, card_group_id: next[0]?.card_id || 0 });
|
||||
}, [displayVariants, editingAutomationRule]);
|
||||
|
||||
// updateAdjustPriceTarget 更新拍下改价草稿中的目标价格。
|
||||
const updateAdjustPriceTarget = useCallback(/* adjustPriceAction 更新改价目标价格。 */ (targetPrice: string) => {
|
||||
if (!editingAutomationRule) return;
|
||||
// baseActions 保存至少包含改价动作的当前动作列表。
|
||||
const baseActions = editingAutomationRule.actions?.some(/* 当前回调查找改价动作。 */ action => action.action_type === 'adjust_price')
|
||||
? editingAutomationRule.actions
|
||||
: cardActionsForTrigger('order_created');
|
||||
setEditingAutomationRule({
|
||||
...editingAutomationRule,
|
||||
actions: baseActions.map(/* 当前回调把新目标价格写入改价动作配置。 */ action =>
|
||||
action.action_type === 'adjust_price' ? { ...action, config_json: buildAdjustPriceConfig(targetPrice) } : action),
|
||||
});
|
||||
}, [editingAutomationRule]);
|
||||
|
||||
// updateAdjustPriceNotifyText 更新拍下改价草稿中的可选买家提醒文案。
|
||||
const updateAdjustPriceNotifyText = useCallback(/* adjustNotifyAction 更新改价提醒文案。 */ (text: string) => {
|
||||
if (!editingAutomationRule) return;
|
||||
// actions 保存当前动作列表副本。
|
||||
const actions = (editingAutomationRule.actions || []).slice();
|
||||
// textIndex 是可选提醒动作在列表中的位置。
|
||||
const textIndex = actions.findIndex(/* 当前回调查找提醒文案动作。 */ action => action.action_type === 'send_text');
|
||||
if (textIndex >= 0) {
|
||||
actions[textIndex] = { ...actions[textIndex], message_template: text };
|
||||
} else {
|
||||
actions.push({ action_type: 'send_text', message_template: text, enabled: true, sort_order: actions.length + 1 });
|
||||
}
|
||||
setEditingAutomationRule({ ...editingAutomationRule, actions });
|
||||
}, [editingAutomationRule]);
|
||||
|
||||
// appendDeliveryContent 在规格编辑器末尾追加一行。
|
||||
const appendDeliveryContent = useCallback(/* appendVariantAction 追加规则规格。 */ () => {
|
||||
if (!editingAutomationRule) return;
|
||||
@@ -332,7 +365,7 @@ export const useRuleActions = ({
|
||||
if (!editingAutomationRule.cookie_id) return alert('请选择账号');
|
||||
// variants 保存当前规格列表。
|
||||
const variants = editingAutomationRule.variants?.length ? editingAutomationRule.variants : [];
|
||||
if (trigger !== 'review_missing_timeout') {
|
||||
if (trigger !== 'review_missing_timeout' && trigger !== 'order_created') {
|
||||
if (!variants.length || variants.some(/* 当前回调校验卡密组是否已选择。 */ variant => !variant.card_id)) return alert(trigger === 'buyer_reviewed' ? '请选择评价赠品卡密库存' : '请选择发货卡密库存');
|
||||
if (isMultiSpecRule && variants.some(/* 当前回调校验多规格字段。 */ variant => !variant.spec_name.trim() || !variant.spec_value.trim())) return alert('多规格商品必须填写每一行的规格名称和规格值');
|
||||
}
|
||||
@@ -341,13 +374,19 @@ export const useRuleActions = ({
|
||||
const text = editingAutomationRule.actions?.find(/* 当前回调查找求评价文案动作。 */ action => action.action_type === 'send_text')?.message_template || '';
|
||||
if (!text.trim()) return alert('请填写求评价文案');
|
||||
}
|
||||
// saveActions 保存实际提交的动作列表;拍下改价规则会剔除未填写文案的可选提醒动作。
|
||||
let saveActions = editingAutomationRule.actions?.length ? editingAutomationRule.actions : cardActionsForTrigger(trigger, editingAutomationRule.card_group_id || 0);
|
||||
if (trigger === 'order_created') {
|
||||
if (!isValidAdjustPrice(adjustPriceTarget(saveActions))) return alert('请填写 0.01 到 1000000 元、最多两位小数的目标价格');
|
||||
saveActions = saveActions.filter(/* 当前回调剔除空文案的可选提醒动作。 */ action => action.action_type !== 'send_text' || Boolean(action.message_template?.trim()));
|
||||
}
|
||||
// saveVariants 保存归一化后的发货规格。
|
||||
const saveVariants = trigger === 'review_missing_timeout' ? [] : variants.map(/* 当前回调归一化发货规格字段。 */ variant => ({ ...variant, spec_name: isMultiSpecRule ? variant.spec_name.trim() : '', spec_value: isMultiSpecRule ? variant.spec_value.trim() : '', delivery_count: Math.max(1, Number(variant.delivery_count) || 1), enabled: variant.enabled !== false }));
|
||||
const saveVariants = trigger === 'review_missing_timeout' || trigger === 'order_created' ? [] : variants.map(/* 当前回调归一化发货规格字段。 */ variant => ({ ...variant, spec_name: isMultiSpecRule ? variant.spec_name.trim() : '', spec_value: isMultiSpecRule ? variant.spec_value.trim() : '', delivery_count: Math.max(1, Number(variant.delivery_count) || 1), enabled: variant.enabled !== false }));
|
||||
setAutomationSubmitState(startRuleSubmission(automationSubmitState));
|
||||
// succeeded 记录保存是否成功。
|
||||
let succeeded = false;
|
||||
try {
|
||||
await updateShippingRule({ ...editingAutomationRule, trigger_type: trigger, name: (editingAutomationRule.name || '').trim() || defaultRuleName(trigger, selectedRuleItem?.item_title || editingAutomationRule.item_id || ''), config_json: trigger === 'review_missing_timeout' ? buildReviewConfig(editingAutomationRule.config_json) : (editingAutomationRule.config_json || '{}'), actions: editingAutomationRule.actions?.length ? editingAutomationRule.actions : cardActionsForTrigger(trigger, saveVariants[0]?.card_id || editingAutomationRule.card_group_id || 0), variants: saveVariants });
|
||||
await updateShippingRule({ ...editingAutomationRule, trigger_type: trigger, name: (editingAutomationRule.name || '').trim() || defaultRuleName(trigger, selectedRuleItem?.item_title || editingAutomationRule.item_id || ''), config_json: trigger === 'review_missing_timeout' ? buildReviewConfig(editingAutomationRule.config_json) : (editingAutomationRule.config_json || '{}'), actions: trigger === 'order_created' ? saveActions : (editingAutomationRule.actions?.length ? editingAutomationRule.actions : cardActionsForTrigger(trigger, saveVariants[0]?.card_id || editingAutomationRule.card_group_id || 0)), variants: saveVariants });
|
||||
setShowAutomationModal(false);
|
||||
await Promise.all([loadAutomationRules(), loadReferenceData()]);
|
||||
alert('保存成功');
|
||||
@@ -451,7 +490,7 @@ export const useRuleActions = ({
|
||||
automationSubmitState, replySubmitState, defaultReplySubmitState, editingAutomationRule, setEditingAutomationRule,
|
||||
editingReplyRule, setEditingReplyRule, defaultForm, setDefaultForm, selectedRuleItem, isMultiSpecRule, currentTrigger,
|
||||
currentMeta, reviewConfig, displayVariants, buildAutomationDraft, openAutomationRule, openNewAutomationRule,
|
||||
handleTriggerChange, handleAutomationItemChange, updateVariant, appendDeliveryContent, handleSaveAutomationRule,
|
||||
handleTriggerChange, handleAutomationItemChange, updateVariant, updateAdjustPriceTarget, updateAdjustPriceNotifyText, appendDeliveryContent, handleSaveAutomationRule,
|
||||
handleDeleteAutomation, handleToggleAutomation, handleResolveRunIssue, handleResolveDeferredIssue, handleAddReplyRule,
|
||||
handleSaveReplyRule, handleDeleteReply, openDefaultReplyModal, handleSaveDefaultReply, handleDeleteDefaultReply,
|
||||
handleClearDefaultReplyRecords,
|
||||
|
||||
@@ -68,7 +68,7 @@ export interface TriggerMeta {
|
||||
// flow 是该触发类型的步骤摘要。
|
||||
flow: string[];
|
||||
// accent 是页面使用的主题色名称。
|
||||
accent: 'blue' | 'emerald' | 'amber';
|
||||
accent: 'blue' | 'emerald' | 'amber' | 'violet';
|
||||
// icon 是触发类型对应的图标组件。
|
||||
icon: ElementType;
|
||||
}
|
||||
|
||||
@@ -4,11 +4,14 @@ import {
|
||||
accentClasses,
|
||||
accountLabel,
|
||||
actionSummary,
|
||||
adjustPriceTarget,
|
||||
boolFlag,
|
||||
buildAdjustPriceConfig,
|
||||
buildReviewConfig,
|
||||
cardActionsForTrigger,
|
||||
defaultRuleName,
|
||||
emptyVariant,
|
||||
isValidAdjustPrice,
|
||||
parseJSONObject,
|
||||
shouldReplaceGeneratedName,
|
||||
statusPill,
|
||||
@@ -49,10 +52,13 @@ describe('规则工具函数', /* 当前回调处理规则配置和展示状态
|
||||
expect(shouldReplaceGeneratedName('我自定义的规则')).toBe(false);
|
||||
});
|
||||
|
||||
test('创建三类触发器的默认动作链', /* 当前回调处理规则配置和展示状态。 */ () => {
|
||||
test('创建各类触发器的默认动作链', /* 当前回调处理规则配置和展示状态。 */ () => {
|
||||
expect(cardActionsForTrigger('review_missing_timeout')).toEqual([{
|
||||
action_type: 'send_text', message_template: '亲,商品使用满意的话,麻烦给个评价,谢谢~', enabled: true, sort_order: 1,
|
||||
}]);
|
||||
expect(cardActionsForTrigger('order_created')).toEqual([{
|
||||
action_type: 'adjust_price', config_json: '{"target_price":""}', enabled: true, sort_order: 1,
|
||||
}]);
|
||||
expect(cardActionsForTrigger('order_paid', 7)).toEqual([
|
||||
{ action_type: 'send_card', card_id: 7, delivery_count: 1, enabled: true, sort_order: 1 },
|
||||
{ action_type: 'confirm_shipment', enabled: true, sort_order: 2 },
|
||||
@@ -62,6 +68,23 @@ describe('规则工具函数', /* 当前回调处理规则配置和展示状态
|
||||
]);
|
||||
});
|
||||
|
||||
test('校验并读取拍下改价的目标价格', /* 当前回调处理规则配置和展示状态。 */ () => {
|
||||
expect(buildAdjustPriceConfig(' 9.9 ')).toBe('{"target_price":"9.9"}');
|
||||
expect(adjustPriceTarget([{ action_type: 'adjust_price', config_json: '{"target_price":"12.5"}', enabled: true }])).toBe('12.5');
|
||||
expect(adjustPriceTarget([{ action_type: 'send_text', message_template: 'x', enabled: true }])).toBe('');
|
||||
expect(adjustPriceTarget()).toBe('');
|
||||
expect(isValidAdjustPrice('9.9')).toBe(true);
|
||||
expect(isValidAdjustPrice('0.01')).toBe(true);
|
||||
expect(isValidAdjustPrice('1000000')).toBe(true);
|
||||
expect(isValidAdjustPrice('0')).toBe(false);
|
||||
expect(isValidAdjustPrice('')).toBe(false);
|
||||
expect(isValidAdjustPrice('1.234')).toBe(false);
|
||||
expect(isValidAdjustPrice('abc')).toBe(false);
|
||||
expect(isValidAdjustPrice('1000000.01')).toBe(false);
|
||||
expect(actionSummary(rule({ trigger_type: 'order_created', actions: [{ action_type: 'adjust_price', config_json: '{"target_price":"9.9"}', enabled: true }] }))).toBe('拍下后改价为 ¥9.9');
|
||||
expect(actionSummary(rule({ trigger_type: 'order_created', actions: [] }))).toBe('未配置目标价格');
|
||||
});
|
||||
|
||||
test('汇总动作、主题样式和布尔标志', /* 当前回调处理规则配置和展示状态。 */ () => {
|
||||
expect(actionSummary(rule({ trigger_type: 'review_missing_timeout', actions: [{ action_type: 'send_text', message_template: '请评价', enabled: true }] }))).toBe('请评价');
|
||||
expect(actionSummary(rule({ trigger_type: 'review_missing_timeout', actions: [] }))).toBe('发送求评价文案');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Clock3,Gift,PackageCheck } from 'lucide-react';
|
||||
import { CircleDollarSign,Clock3,Gift,PackageCheck } from 'lucide-react';
|
||||
import type {
|
||||
AccountDetail,
|
||||
AutomationAction,
|
||||
@@ -10,6 +10,14 @@ import type { TriggerMeta } from './types';
|
||||
|
||||
// triggerMeta 是自动化触发类型的统一展示元数据,供列表和编辑器复用。
|
||||
export const triggerMeta: Record<AutomationTriggerType, TriggerMeta> = {
|
||||
order_created: {
|
||||
label: '拍下未付款自动改价',
|
||||
shortLabel: '拍下改价',
|
||||
description: '买家拍下商品未付款时,把该笔待付款订单的价格自动修改为目标价格。',
|
||||
flow: ['拍下待付款卡片', '匹配商品规则', '修改订单价格', '可选提醒买家'],
|
||||
accent: 'violet',
|
||||
icon: CircleDollarSign,
|
||||
},
|
||||
order_paid: {
|
||||
label: '付款后自动发货',
|
||||
shortLabel: '自动发货',
|
||||
@@ -37,7 +45,7 @@ export const triggerMeta: Record<AutomationTriggerType, TriggerMeta> = {
|
||||
};
|
||||
|
||||
// triggerOrder 固定自动化类型在创建面板和筛选器中的排序。
|
||||
export const triggerOrder: AutomationTriggerType[] = ['order_paid', 'buyer_reviewed', 'review_missing_timeout'];
|
||||
export const triggerOrder: AutomationTriggerType[] = ['order_paid', 'order_created', 'buyer_reviewed', 'review_missing_timeout'];
|
||||
|
||||
// reviewRequestText 是超时未评价规则的默认提醒文案。
|
||||
export const reviewRequestText = '亲,商品使用满意的话,麻烦给个评价,谢谢~';
|
||||
@@ -95,8 +103,39 @@ export const shouldReplaceGeneratedName = (name?: string) => {
|
||||
);
|
||||
};
|
||||
|
||||
// buildAdjustPriceConfig 把目标价格文本序列化为改价动作配置 JSON。
|
||||
export const buildAdjustPriceConfig = (targetPrice: string) => JSON.stringify({ target_price: targetPrice.trim() });
|
||||
|
||||
// adjustPriceTarget 从动作列表读取改价动作的目标价格文本。
|
||||
export const adjustPriceTarget = (actions?: AutomationAction[]): string => {
|
||||
// action 是动作列表中的改价动作。
|
||||
const action = (actions || []).find(
|
||||
// 改价动作匹配器只查找订单改价类型。
|
||||
candidate => candidate.action_type === 'adjust_price',
|
||||
);
|
||||
return String(parseJSONObject(action?.config_json).target_price ?? '');
|
||||
};
|
||||
|
||||
// isValidAdjustPrice 校验目标价格是否为 0.01 到 1000000 元、最多两位小数的金额。
|
||||
export const isValidAdjustPrice = (raw: string): boolean => {
|
||||
// trimmed 是去掉首尾空白后的金额文本。
|
||||
const trimmed = raw.trim();
|
||||
if (!/^\d+(\.\d{1,2})?$/.test(trimmed)) return false;
|
||||
// cents 是金额折算出的整数分。
|
||||
const cents = Math.round(Number(trimmed) * 100);
|
||||
return cents >= 1 && cents <= 100000000;
|
||||
};
|
||||
|
||||
// cardActionsForTrigger 根据触发类型创建默认动作链。
|
||||
export const cardActionsForTrigger = (trigger: AutomationTriggerType, cardID = 0): AutomationAction[] => {
|
||||
if (trigger === 'order_created') {
|
||||
return [{
|
||||
action_type: 'adjust_price',
|
||||
config_json: buildAdjustPriceConfig(''),
|
||||
enabled: true,
|
||||
sort_order: 1,
|
||||
}];
|
||||
}
|
||||
if (trigger === 'review_missing_timeout') {
|
||||
return [{
|
||||
action_type: 'send_text',
|
||||
@@ -123,6 +162,11 @@ export const cardActionsForTrigger = (trigger: AutomationTriggerType, cardID = 0
|
||||
|
||||
// actionSummary 将规则动作转换成列表中的简短摘要。
|
||||
export const actionSummary = (rule: ShippingRule) => {
|
||||
if (rule.trigger_type === 'order_created') {
|
||||
// target 是规则配置的改价目标价格。
|
||||
const target = adjustPriceTarget(rule.actions);
|
||||
return target ? `拍下后改价为 ¥${target}` : '未配置目标价格';
|
||||
}
|
||||
if (rule.trigger_type === 'review_missing_timeout') {
|
||||
return rule.actions?.find(
|
||||
// 文案动作匹配器只查找发送文字动作。
|
||||
@@ -148,6 +192,7 @@ export const accentClasses = (accent: TriggerMeta['accent'], selected = false) =
|
||||
blue: selected ? 'border-blue-500 bg-blue-50 text-blue-700' : 'border-blue-100 bg-blue-50/60 text-blue-700 hover:border-blue-300',
|
||||
emerald: selected ? 'border-emerald-500 bg-emerald-50 text-emerald-700' : 'border-emerald-100 bg-emerald-50/60 text-emerald-700 hover:border-emerald-300',
|
||||
amber: selected ? 'border-amber-500 bg-amber-50 text-amber-700' : 'border-amber-100 bg-amber-50/60 text-amber-700 hover:border-amber-300',
|
||||
violet: selected ? 'border-violet-500 bg-violet-50 text-violet-700' : 'border-violet-100 bg-violet-50/60 text-violet-700 hover:border-violet-300',
|
||||
};
|
||||
return map[accent] || map.blue;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user