86896cd6b1
HIF-22 把申请提现扣 commission 的逻辑移到审批时执行,但 cancel 路径漏改, 撤销 pending 提现仍调 UpdateCommission(+amount) 凭空给用户加佣金,并写出 一条 338 退佣金日志污染对账。 参照 rejectWithdrawal 的处理方式(withdrawalCommon.go:80-87),事务体只保留 withdrawal.status -> Cancelled 的更新,删除 UpdateCommission 与误导性的 WriteCommissionLog 调用。补单测覆盖 happy path、非 pending 状态短路、跨用户 撤销拒绝、与审批并发时 FOR UPDATE 行锁让 cancel 输掉 race 这四类场景。 Co-authored-by: multica-agent <github@multica.ai>
86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
logicCommon "github.com/perfect-panel/server/internal/logic/common"
|
|
"github.com/perfect-panel/server/internal/model/user"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/constant"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type CancelWithdrawalLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewCancelWithdrawalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CancelWithdrawalLogic {
|
|
return &CancelWithdrawalLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CancelWithdrawalLogic) CancelWithdrawal(req *types.CancelWithdrawalRequest) (resp *types.WithdrawalLog, err error) {
|
|
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
|
if !ok {
|
|
l.Error("current user is not found in context")
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
|
|
}
|
|
|
|
var withdrawal *user.Withdrawal
|
|
err = l.svcCtx.DB.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
|
|
var txErr error
|
|
withdrawal, txErr = logicCommon.LoadPendingWithdrawalForUpdate(l.ctx, tx, req.WithdrawalId)
|
|
if txErr != nil {
|
|
if txErr.Error() == "withdrawal status invalid" {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.WithdrawalStatusInvalid), "withdrawal %d is not in pending state", req.WithdrawalId)
|
|
}
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "load withdrawal failed: %v", txErr)
|
|
}
|
|
|
|
if withdrawal.UserId != u.Id {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.PermissionDenied), "user %d cannot cancel withdrawal belonging to user %d", u.Id, withdrawal.UserId)
|
|
}
|
|
|
|
if txErr = tx.Model(&user.Withdrawal{}).
|
|
Where("id = ? AND status = ?", req.WithdrawalId, user.WithdrawalStatusPending).
|
|
Update("status", user.WithdrawalStatusCancelled).Error; txErr != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "cancel withdrawal failed: %v", txErr)
|
|
}
|
|
|
|
// Commission was NOT deducted at application time under the HIF-22
|
|
// approval flow, so cancellation requires no refund — only a status
|
|
// update. Refunding here would mint phantom commission and pollute
|
|
// reconciliation (mirrors rejectWithdrawal in admin/user/withdrawalCommon.go).
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_ = l.svcCtx.UserModel.ClearUserCache(l.ctx, u)
|
|
|
|
return &types.WithdrawalLog{
|
|
Id: withdrawal.Id,
|
|
UserId: withdrawal.UserId,
|
|
Amount: withdrawal.Amount,
|
|
Content: withdrawal.Content,
|
|
Status: user.WithdrawalStatusCancelled,
|
|
Reason: "",
|
|
Method: withdrawal.Method,
|
|
Account: withdrawal.Account,
|
|
QrCodeUrl: withdrawal.QrCodeUrl,
|
|
CreatedAt: withdrawal.CreatedAt.UnixMilli(),
|
|
UpdatedAt: withdrawal.UpdatedAt.UnixMilli(),
|
|
}, nil
|
|
}
|