Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
AUG26-008。
- 迁移 000214–000217:tb_shop 全局唯一且不可修改的随机分销码(含存量回填)、
tb_agent_distribution_registration 待审批注册记录、tb_withdrawal_qualification 资料版本、
tb_commission_withdrawal_request_attempt 审批尝试记录,以及提现申请的 latest_*/异常标记列;
不修改既有迁移,down 在存在本 Change 业务事实或新类型场景行时拒绝破坏性回滚。
- 公开接口 POST /api/c/v1/agent-distribution-registrations:无认证,复用既有短信验证码校验、
消费与限流;无效分销码、停用上级、验证码无效或已消费统一返回「分销码不可用」且不落库,
审批通过前不创建店铺、账号或钱包。
- 审批通过才在同一事务内建启用店铺、代理主账号、钱包、上级层级与业务员快照,驳回不建实体,
重复回调不重复建实体,提交后清理上级下级缓存。
- 提现资料资格按不可变版本保存,替换合同或法人身份证即新增版本并同事务失效旧有效版本;
超管作废原因必填;代理停用与店铺删除联动失效。
- 提现每次提交或重提新增不可变审批尝试记录并冻结金额;企业微信通过仅一次从冻结扣减、
保持状态 2 并写 paid_at(不使用状态 4),驳回/cancelled/deleted 仅一次释放,
通过后撤销不回滚、不重新冻结、只写正交异常标记;加锁顺序统一为申请→尝试→钱包。
- 本地人工终审对已关联审批实例的申请返回状态冲突,approval_instance_id 为空的存量申请保持既有行为,
不新增任何配置开关。
- 补齐审批业务类型注册点全集:业务类型与场景字段常量、场景 DTO 两处枚举与中文描述、
场景字段白名单/合法类型/中文名、数据库 CHECK、Worker 决策消费者与装配、审批审计资源映射,
以及三个新审计资源与 13 个审计动作;失败/拒绝审计改为必达。
- 新增后台路由与 OpenAPI:资格提交/查询/作废、提现申请/重提/详情、店铺详情返回只读分销码。
- 归档本 Change:主 Spec 新增 agent-distribution-withdrawal 能力(5 个 Requirement)。
验证(junhong_cmp_test + Redis DB 6,显式 DB_*,未重置整库):
- 迁移 up → version 217 且 dirty=false → down 3 → up 回 217,fixture 复核残留为 0。
- 受控状态机脚手架 227 项通过 / 0 项失败,覆盖 18 组场景(幂等与乱序回调、资金冻结/释放/重提、
退款回扣 × 在途提现并发、负向场景拒绝审计与 14 个动作码审计真实落库)。
- gofmt 空、go build/go vet 通过、gendocs 与工作区逐字节一致、context-health 通过、
openspec validate --strict 通过、doctor healthy;自动化测试按项目决策为 N/A。
运行期前置(未完成,非代码交付物):由超管经 PUT /api/admin/wecom/scenes/{business_type} 为
agent_distribution_approval、withdrawal_qualification_approval、commission_withdrawal_approval
配置启用场景与模板控件映射;未配置时相应提交失败关闭。
156 lines
5.9 KiB
Go
156 lines
5.9 KiB
Go
// Package distributionwithdrawal 收口代理分销注册、提现资料资格与提现企业微信终审的用例。
|
||
// 三者都以审批尝试/资料版本/注册记录主键作为通用审批业务标识,终态消费幂等且可重放。
|
||
package distributionwithdrawal
|
||
|
||
import (
|
||
"context"
|
||
stderrors "errors"
|
||
"strconv"
|
||
|
||
"github.com/bytedance/sonic"
|
||
"gorm.io/gorm"
|
||
|
||
approvalapp "github.com/break/junhong_cmp_fiber/internal/application/approval"
|
||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||
"github.com/break/junhong_cmp_fiber/pkg/auditfailure"
|
||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||
)
|
||
|
||
// VerificationCodeVerifier 是公开扫码注册复用的短信验证码校验接缝。
|
||
// 校验成功即消费验证码,同一验证码不可二次使用。
|
||
type VerificationCodeVerifier interface {
|
||
VerifyCode(ctx context.Context, phone string, code string) error
|
||
}
|
||
|
||
// AuditChange 描述分销注册、提现资格与提现审批事实的实际变化。
|
||
// 日志与审计不得记录密码、完整证件号、完整手机号或附件内容。
|
||
type AuditChange struct {
|
||
// EventID 是审计事件稳定标识,同一业务事实重复重放时保持相同值。
|
||
EventID string
|
||
// ActionCode 是已注册的审计动作码。
|
||
ActionCode string
|
||
// Summary 是给人工阅读的中文摘要。
|
||
Summary string
|
||
// CorrelationID 是来源业务链路标识。
|
||
CorrelationID string
|
||
// Registration 是本次动作后的扫码注册记录事实。
|
||
Registration *model.AgentDistributionRegistration
|
||
// ParentShop 是扫码注册使用的上级店铺。
|
||
ParentShop *model.Shop
|
||
// Shop 是本次动作所属或引用的店铺。
|
||
Shop *model.Shop
|
||
// CreatedShop 是注册审批通过时新建的店铺。
|
||
// CreatedShop.DistributionCode 是本次为新店铺生成的随机码;
|
||
// AppliedDistributionCode 是注册时使用的上级店铺码快照,二者必须区分,不得混用。
|
||
CreatedShop *model.Shop
|
||
// AppliedDistributionCode 是注册提交时使用的上级店铺分销码快照。
|
||
AppliedDistributionCode string
|
||
// Qualification 是本次动作后的提现资料资格版本。
|
||
Qualification *model.WithdrawalQualification
|
||
// Withdrawal 是本次动作后的提现申请事实。
|
||
Withdrawal *model.CommissionWithdrawalRequest
|
||
// Attempt 是本次动作对应的提现审批尝试记录。
|
||
Attempt *model.CommissionWithdrawalRequestAttempt
|
||
// Wallet 是本次动作影响的佣金钱包。
|
||
Wallet *model.AgentWallet
|
||
// Transaction 是本次动作产生的钱包流水。
|
||
Transaction *model.AgentWalletTransaction
|
||
// BeforeData 与 AfterData 是脱敏前后的字段快照。
|
||
BeforeData map[string]any
|
||
AfterData map[string]any
|
||
// Result 是审计结果,空值按成功处理。
|
||
Result string
|
||
// ErrorCode 与 ErrorSummary 是失败或拒绝审计的稳定错误信息。
|
||
ErrorCode string
|
||
ErrorSummary string
|
||
}
|
||
|
||
// AuditWriter 在业务事务内追加统一 Audit Event。
|
||
type AuditWriter interface {
|
||
WriteDistributionWithdrawal(ctx context.Context, tx *gorm.DB, change AuditChange) error
|
||
}
|
||
|
||
// RecordFailure 在业务回滚后使用独立短事务记录失败或拒绝事实。
|
||
func RecordFailure(ctx context.Context, db *gorm.DB, writer AuditWriter, change AuditChange, businessErr error) {
|
||
if writer == nil || db == nil || businessErr == nil {
|
||
return
|
||
}
|
||
appErr := changeError(businessErr)
|
||
change.Result = constants.AuditResultFailed
|
||
switch appErr.Code {
|
||
case errors.CodeForbidden, errors.CodeNotFound, errors.CodeInvalidParam, errors.CodeConflict,
|
||
errors.CodeInvalidStatus, errors.CodeInsufficientBalance, errors.CodeShopLevelExceeded:
|
||
change.Result = constants.AuditResultDenied
|
||
}
|
||
if change.ErrorCode == "" {
|
||
change.ErrorCode = strconv.Itoa(appErr.Code)
|
||
}
|
||
if change.ErrorSummary == "" {
|
||
change.ErrorSummary = appErr.Message
|
||
}
|
||
if err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||
return writer.WriteDistributionWithdrawal(ctx, tx, change)
|
||
}); err != nil {
|
||
auditfailure.RecordSecondaryWriteFailure(
|
||
change.ActionCode, "", "", change.CorrelationID, change.ErrorCode, err,
|
||
)
|
||
}
|
||
}
|
||
|
||
// changeError 归一化底层错误为稳定 AppError,避免失败审计泄露底层文本。
|
||
func changeError(err error) *errors.AppError {
|
||
var appErr *errors.AppError
|
||
if stderrors.As(err, &appErr) {
|
||
return appErr
|
||
}
|
||
return errors.New(errors.CodeInternalError, "分销注册或提现审批操作失败")
|
||
}
|
||
|
||
// approvalSnapshots 生成通用审批的提交人快照与业务表单快照。
|
||
func approvalSnapshots(accountID uint, accountName string, business map[string]any) ([]byte, []byte, error) {
|
||
submitter, err := marshalJSON(map[string]any{
|
||
"account_id": accountID, "account_name": accountName,
|
||
})
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
request, err := marshalJSON(business)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return submitter, request, nil
|
||
}
|
||
|
||
// marshalJSON 使用 sonic 序列化业务快照,禁止写入密码、完整证件号或附件内容。
|
||
func marshalJSON(value any) ([]byte, error) {
|
||
payload, err := sonic.Marshal(value)
|
||
if err != nil {
|
||
return nil, errors.Wrap(errors.CodeInternalError, err, "序列化审批业务快照失败")
|
||
}
|
||
return payload, nil
|
||
}
|
||
|
||
// createApprovalInTx 在业务事务内创建通用审批实例并返回引用。
|
||
func createApprovalInTx(
|
||
ctx context.Context,
|
||
tx *gorm.DB,
|
||
port approvalapp.Port,
|
||
preparation approvalapp.Preparation,
|
||
businessType string,
|
||
businessID uint,
|
||
submitterAccountID uint,
|
||
submitterSnapshot []byte,
|
||
requestSnapshot []byte,
|
||
correlationID string,
|
||
) (approvalapp.Reference, error) {
|
||
if port == nil {
|
||
return approvalapp.Reference{}, errors.New(errors.CodeServiceUnavailable, "审批能力尚未配置")
|
||
}
|
||
return port.CreateInTx(ctx, tx, approvalapp.CreateRequest{
|
||
Preparation: preparation, BusinessType: businessType, BusinessID: businessID,
|
||
SubmitterAccountID: submitterAccountID, SubmitterSnapshot: submitterSnapshot,
|
||
RequestSnapshot: requestSnapshot, CorrelationID: correlationID,
|
||
})
|
||
}
|