修复:完善上传契约与自动化闭环
统一 multipart 请求透传并补齐接口契约和边界校验;新增 AI 自动改价、暂时性失败重试、擦亮与确认发货幂等处理;修复聊天已读回执跨端同步误报警,并更新前端静态资源与回归测试。
This commit is contained in:
1 parent
44f0b5c559
commit
50eca2fca6
77 files changed
+2138
-274
No files matched your search
@@ -15,6 +15,9 @@ var ErrRuleNotFound = errors.New("自动化规则不存在")
|
||||
// ErrRuleActive 表示规则仍有待处理运行,不能直接删除。
|
||||
var ErrRuleActive = errors.New("规则仍有待处理的自动化运行")
|
||||
|
||||
// ErrPricingModeConflict 表示启用的 AI 议价账号不能再启用固定规则改价。
|
||||
var ErrPricingModeConflict = errors.New("该账号已启用 AI 议价,不能同时启用自动化规则改价")
|
||||
|
||||
// TriggerOrderCreated 表示买家拍下未付款触发器。
|
||||
const TriggerOrderCreated = "order_created"
|
||||
|
||||
@@ -221,6 +224,8 @@ type RuleOwnership interface {
|
||||
OwnsItem(ctx context.Context, userID int64, accountID, itemID string) (bool, error)
|
||||
// GetCard 返回用户拥有的卡密组类型。
|
||||
GetCard(ctx context.Context, userID, cardID int64) (CardInfo, error)
|
||||
// AIReplyEnabled 判断账号是否启用了 AI 议价模式。
|
||||
AIReplyEnabled(ctx context.Context, accountID string) (bool, error)
|
||||
}
|
||||
|
||||
// RuleService 编排自动化规则校验、分页和持久化。
|
||||
@@ -319,6 +324,16 @@ func (s *RuleService) Normalize(ctx context.Context, userID int64, draft RuleDra
|
||||
if combinationErr := validateTriggerActionCombination(draft.TriggerType, flags); combinationErr != nil {
|
||||
return RuleInput{}, combinationErr
|
||||
}
|
||||
if draft.Enabled && flags.hasAdjustPrice {
|
||||
// aiEnabled 表示账号是否已经采用 AI 议价;aiErr 是非敏感设置读取错误。
|
||||
aiEnabled, aiErr := s.ownership.AIReplyEnabled(ctx, draft.CookieID)
|
||||
if aiErr != nil {
|
||||
return RuleInput{}, aiErr
|
||||
}
|
||||
if aiEnabled {
|
||||
return RuleInput{}, ErrPricingModeConflict
|
||||
}
|
||||
}
|
||||
return RuleInput{UserID: userID, CookieID: draft.CookieID, ItemID: draft.ItemID, Name: draft.Name,
|
||||
TriggerType: draft.TriggerType, Enabled: draft.Enabled, Priority: draft.Priority,
|
||||
ConfigJSON: draft.ConfigJSON, Actions: actions}, nil
|
||||
|
||||
@@ -51,6 +51,8 @@ type ruleOwnershipFake struct {
|
||||
itemErr error
|
||||
// cardErr 是卡密组查询需要返回的基础设施错误。
|
||||
cardErr error
|
||||
// aiEnabled 表示测试账号是否启用了 AI 议价。
|
||||
aiEnabled bool
|
||||
}
|
||||
|
||||
// OwnsAccount 返回账号归属通过。
|
||||
@@ -80,6 +82,28 @@ func (r *ruleOwnershipFake) GetCard(context.Context, int64, int64) (CardInfo, er
|
||||
return CardInfo{Type: r.cardType}, nil
|
||||
}
|
||||
|
||||
// AIReplyEnabled 返回测试账号的 AI 议价开关。
|
||||
func (r *ruleOwnershipFake) AIReplyEnabled(context.Context, string) (bool, error) {
|
||||
return r.aiEnabled, nil
|
||||
}
|
||||
|
||||
// TestRuleServiceRejectsAdjustPriceWhenAIEnabled 验证启用 AI 议价的账号不能再启用固定规则改价。
|
||||
func TestRuleServiceRejectsAdjustPriceWhenAIEnabled(t *testing.T) {
|
||||
// service 是注入 AI 议价开启状态的规则应用服务。
|
||||
service := NewRuleService(&ruleRepositoryFake{}, &ruleOwnershipFake{aiEnabled: true})
|
||||
// draft 是准备启用的拍下固定价格规则。
|
||||
draft := RuleDraft{CookieID: "account-1", TriggerType: TriggerOrderCreated, Enabled: true, Actions: []ActionDraft{{ActionType: ActionAdjustPrice, ConfigJSON: `{"target_price":"9.90"}`}}}
|
||||
// err 是启用冲突规则时返回的互斥错误。
|
||||
if _, err := service.Normalize(context.Background(), 7, draft); !errors.Is(err, ErrPricingModeConflict) {
|
||||
t.Fatalf("AI 议价与固定改价规则冲突应被拒绝: %v", err)
|
||||
}
|
||||
draft.Enabled = false
|
||||
// err 是保留停用规则时不应出现的校验错误。
|
||||
if _, err := service.Normalize(context.Background(), 7, draft); err != nil {
|
||||
t.Fatalf("停用的固定规则应允许保留: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuleServiceNormalizePropagatesOwnershipErrors 验证归属端口的基础设施错误不会伪装成用户输入错误。
|
||||
func TestRuleServiceNormalizePropagatesOwnershipErrors(t *testing.T) {
|
||||
// backendErr 是归属查询模拟的底层数据库故障。
|
||||
|
||||
@@ -21,6 +21,9 @@ var ErrForbidden = errors.New("无权操作该账号设置")
|
||||
// ErrConfigNotFound 表示账号存在但尚未保存 AI 回复设置。
|
||||
var ErrConfigNotFound = errors.New("AI 回复设置不存在")
|
||||
|
||||
// ErrPricingModeConflict 表示 AI 议价与固定自动改价规则不能同时启用。
|
||||
var ErrPricingModeConflict = errors.New("AI 议价与自动化规则改价不能同时启用,请先关闭另一种改价方式")
|
||||
|
||||
// SecretChange 描述敏感系统设置的显式三态变更命令。
|
||||
type SecretChange struct {
|
||||
// Action 是 retain、replace 或 clear 之一。
|
||||
@@ -35,6 +38,8 @@ type AIReplySettings struct {
|
||||
CookieID string
|
||||
// AIEnabled 表示账号 AI 回复是否启用。
|
||||
AIEnabled bool
|
||||
// AutoAdjustPriceEnabled 表示是否把有效 AI 报价自动应用到买家新拍订单。
|
||||
AutoAdjustPriceEnabled bool
|
||||
// MaxDiscountPercent 是允许的最大折扣比例。
|
||||
MaxDiscountPercent int
|
||||
// MaxDiscountAmount 是允许的最大折扣金额。
|
||||
@@ -93,6 +98,8 @@ type Repository interface {
|
||||
GetAIReply(ctx context.Context, userID int64, cookieID string) (AIReplySettings, error)
|
||||
// UpsertAIReply 保存指定账号的 AI 设置摘要。
|
||||
UpsertAIReply(ctx context.Context, cookieID string, settings AIReplySettings) error
|
||||
// HasEnabledAdjustPriceRule 判断账号是否已有启用的固定自动改价规则。
|
||||
HasEnabledAdjustPriceRule(ctx context.Context, cookieID string) (bool, error)
|
||||
}
|
||||
|
||||
// ModelClient 定义读取远端 AI 模型列表所需的最小网络能力。
|
||||
@@ -268,6 +275,19 @@ func (s *Service) UpsertAIReply(ctx context.Context, userID int64, cookieID stri
|
||||
if settings.MaxBargainRounds < 1 || settings.MaxBargainRounds > 10 {
|
||||
return errors.New("最大砍价轮次必须在 1 到 10 之间")
|
||||
}
|
||||
if settings.AutoAdjustPriceEnabled && !settings.AIEnabled {
|
||||
return errors.New("开启 AI 自动改价前必须先启用 AI 议价")
|
||||
}
|
||||
if settings.AIEnabled {
|
||||
// conflict 表示当前账号是否已有启用的固定价格规则;conflictErr 是查询错误。
|
||||
conflict, conflictErr := s.repository.HasEnabledAdjustPriceRule(ctx, strings.TrimSpace(cookieID))
|
||||
if conflictErr != nil {
|
||||
return conflictErr
|
||||
}
|
||||
if conflict {
|
||||
return ErrPricingModeConflict
|
||||
}
|
||||
}
|
||||
settings.CookieID = strings.TrimSpace(cookieID)
|
||||
return s.repository.UpsertAIReply(ctx, settings.CookieID, settings)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ type settingsRepositoryFake struct {
|
||||
aiSettings map[string]AIReplySettings
|
||||
// systemValues 保存系统设置读取值。
|
||||
systemValues map[string]string
|
||||
// adjustRuleEnabled 表示测试账号是否已有启用的固定自动改价规则。
|
||||
adjustRuleEnabled bool
|
||||
}
|
||||
|
||||
// IsSensitiveSettingKey 判断测试设置键是否属于敏感集合。
|
||||
@@ -139,6 +141,11 @@ func (r *settingsRepositoryFake) UpsertAIReply(_ context.Context, cookieID strin
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasEnabledAdjustPriceRule 返回测试配置的固定自动改价规则状态。
|
||||
func (r *settingsRepositoryFake) HasEnabledAdjustPriceRule(context.Context, string) (bool, error) {
|
||||
return r.adjustRuleEnabled, nil
|
||||
}
|
||||
|
||||
// modelClientFake 是 AI 模型客户端测试替身。
|
||||
type modelClientFake struct {
|
||||
// calls 保存收到的端点和密钥,测试只比较是否传递,不输出秘密。
|
||||
@@ -235,4 +242,28 @@ func TestServiceListAIModelsFailsClosedWhenAuditUnavailable(t *testing.T) {
|
||||
}
|
||||
|
||||
var _ Repository = (*settingsRepositoryFake)(nil)
|
||||
|
||||
// TestServiceRejectsConflictingPricingModes 验证自动改价依赖 AI 议价,且 AI 议价不能与固定改价规则同时启用。
|
||||
func TestServiceRejectsConflictingPricingModes(t *testing.T) {
|
||||
// ctx 是 AI 设置互斥校验测试上下文。
|
||||
ctx := context.Background()
|
||||
// repository 模拟账号已存在启用的固定自动改价规则。
|
||||
repository := &settingsRepositoryFake{ownerID: 7, adjustRuleEnabled: true}
|
||||
// service 是待验证的设置应用服务。
|
||||
service := NewService(repository, nil)
|
||||
// settings 是开启 AI 议价的有效数值配置。
|
||||
settings := AIReplySettings{AIEnabled: true, MaxDiscountPercent: 10, MaxDiscountAmount: 100, MaxBargainRounds: 3}
|
||||
// err 是启用冲突 AI 模式时返回的互斥错误。
|
||||
if err := service.UpsertAIReply(ctx, 7, "account-1", settings); !errors.Is(err, ErrPricingModeConflict) {
|
||||
t.Fatalf("固定规则冲突应被拒绝: %v", err)
|
||||
}
|
||||
repository.adjustRuleEnabled = false
|
||||
settings.AIEnabled = false
|
||||
settings.AutoAdjustPriceEnabled = true
|
||||
// err 是脱离 AI 议价单独启用真实改价时返回的依赖错误。
|
||||
if err := service.UpsertAIReply(ctx, 7, "account-1", settings); err == nil || !strings.Contains(err.Error(), "必须先启用 AI 议价") {
|
||||
t.Fatalf("独立开启 AI 自动改价应被拒绝: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var _ ModelClient = (*modelClientFake)(nil)
|
||||
Reference in new issue
Block a user