fix: close business flow regressions

This commit is contained in:
david
2026-07-31 11:33:28 +08:00
parent 42a2322c8e
commit a47e9025d0
18 changed files with 403 additions and 30 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// AccountPasswordMinLength 是账号密码允许的最低字符数。
@@ -79,7 +80,10 @@ func IsGenericRecordStatus(status int) bool {
// ActiveRecords excludes logically archived records from operational queries.
func ActiveRecords(query *gorm.DB) *gorm.DB {
return query.Where("status <> ?", StatusArchived)
return query.Where(clause.Neq{
Column: clause.Column{Table: clause.CurrentTable, Name: "status"},
Value: StatusArchived,
})
}
func ListPage[T any](ctx *gin.Context) {

View File

@@ -31,11 +31,34 @@ func TestOperationalQueriesExcludeArchivedRecords(t *testing.T) {
statement := database.ToSQL(func(tx *gorm.DB) *gorm.DB {
return ActiveRecords(tx.Model(&models.EcProduct{})).Find(&[]models.EcProduct{})
})
if !strings.Contains(statement, `status <> 3`) {
if !strings.Contains(statement, `"ec_product"."status" <> 3`) {
t.Fatalf("archive filter missing from operational query: %s", statement)
}
}
func TestOperationalQueriesQualifyStatusAcrossJoins(t *testing.T) {
sqlDatabase, _, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer sqlDatabase.Close()
database, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDatabase}), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
statement := database.ToSQL(func(tx *gorm.DB) *gorm.DB {
return ActiveRecords(tx.Model(&models.StaffCredential{})).
Joins("JOIN staff_account ON staff_account.id = staff_credential.staff_account_id").
Find(&[]models.StaffCredential{})
})
if !strings.Contains(statement, `"staff_credential"."status" <> 3`) {
t.Fatalf("joined archive filter is not table-qualified: %s", statement)
}
if strings.Contains(statement, `WHERE status <>`) {
t.Fatalf("joined archive filter is ambiguous: %s", statement)
}
}
func TestResourceResponseStripsInternalIDsRecursively(t *testing.T) {
got := ResourceResponse(map[string]any{
"id": uint64(1), "identity": "root",

View File

@@ -59,6 +59,17 @@ func ListBank(ctx *gin.Context) { listWalletChild(ctx, &models.WalletBank{},
func ListPayment(ctx *gin.Context) { listWalletChild(ctx, &models.WalletPayment{}, "wallet_payment") }
func ListRecord(ctx *gin.Context) { listWalletChild(ctx, &models.WalletRecord{}, "wallet_record") }
func ListRefund(ctx *gin.Context) { listWalletChild(ctx, &models.WalletRefund{}, "wallet_refund") }
func ListRecharge(ctx *gin.Context) {
point, _, ok := currentScope(ctx)
if !ok {
return
}
query := common.ActiveRecords(db().Model(&models.WalletRecord{})).
Joins("JOIN wallet_basic ON wallet_basic.id = wallet_record.wallet_basic_id").
Where("wallet_basic.owner_type = ? AND wallet_basic.owner_id = ? AND wallet_record.trade_type = ?",
"delivery", point.ID, "delivery_admin_recharge")
listScoped(ctx, &models.WalletRecord{}, query, "wallet_record.created_at desc")
}
func ListApplyCash(ctx *gin.Context) {
listWalletChild(ctx, &models.WalletApplyCash{}, "wallet_apply_cash")
}
@@ -79,6 +90,18 @@ func GetBank(ctx *gin.Context) { getWalletChild(ctx, &models.WalletBank{}, "w
func GetPayment(ctx *gin.Context) { getWalletChild(ctx, &models.WalletPayment{}, "wallet_payment") }
func GetRecord(ctx *gin.Context) { getWalletChild(ctx, &models.WalletRecord{}, "wallet_record") }
func GetRefund(ctx *gin.Context) { getWalletChild(ctx, &models.WalletRefund{}, "wallet_refund") }
func GetRecharge(ctx *gin.Context) {
point, _, ok := currentScope(ctx)
if !ok {
return
}
var record models.WalletRecord
query := common.ActiveRecords(db().Model(&models.WalletRecord{})).
Joins("JOIN wallet_basic ON wallet_basic.id = wallet_record.wallet_basic_id").
Where("wallet_record.identity = ? AND wallet_basic.owner_type = ? AND wallet_basic.owner_id = ? AND wallet_record.trade_type = ?",
ctx.Param("identity"), "delivery", point.ID, "delivery_admin_recharge")
respondRecord(ctx, query, &record)
}
func GetApplyCash(ctx *gin.Context) {
getWalletChild(ctx, &models.WalletApplyCash{}, "wallet_apply_cash")
}

View File

@@ -7,8 +7,15 @@ import (
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func scopedDeliveryAccounts(databaseService *gorm.DB, gasBasicID uint64) *gorm.DB {
return databaseService.Model(&models.DeliveryAccount{}).
Joins("JOIN delivery_basic ON delivery_basic.id = delivery_account.delivery_basic_id").
Where("delivery_account.status <> ? AND delivery_basic.gas_basic_id = ?", common.StatusArchived, gasBasicID)
}
func ListDeliveryAccount(ctx *gin.Context) {
station, ok := currentGas(ctx)
if !ok {
@@ -18,9 +25,7 @@ func ListDeliveryAccount(ctx *gin.Context) {
var list []models.DeliveryAccount
var total int64
query := common.ApplyKeywordFilter(ctx,
common.ActiveRecords(impl.DBService.Model(&models.DeliveryAccount{})).
Joins("JOIN delivery_basic ON delivery_basic.id = delivery_account.delivery_basic_id").
Where("delivery_basic.gas_basic_id = ?", station.ID), &models.DeliveryAccount{})
scopedDeliveryAccounts(impl.DBService, station.ID), &models.DeliveryAccount{})
if err := query.Count(&total).Error; err != nil {
infra.Response.Error(ctx, err)
return
@@ -37,9 +42,8 @@ func GetDeliveryAccount(ctx *gin.Context) {
if !ok {
return
}
query := common.ActiveRecords(impl.DBService.Model(&models.DeliveryAccount{})).
Joins("JOIN delivery_basic ON delivery_basic.id = delivery_account.delivery_basic_id").
Where("delivery_account.identity = ? AND delivery_basic.gas_basic_id = ?", ctx.Param("identity"), station.ID)
query := scopedDeliveryAccounts(impl.DBService, station.ID).
Where("delivery_account.identity = ?", ctx.Param("identity"))
respondScopedRecord(ctx, query, &models.DeliveryAccount{})
}
@@ -84,10 +88,9 @@ func UpdateDeliveryAccount(ctx *gin.Context) {
return
}
var account models.DeliveryAccount
if err := common.ActiveRecords(impl.DBService.Model(&models.DeliveryAccount{})).
if err := scopedDeliveryAccounts(impl.DBService, station.ID).
Select("delivery_account.*").
Joins("JOIN delivery_basic ON delivery_basic.id = delivery_account.delivery_basic_id").
Where("delivery_account.identity = ? AND delivery_basic.gas_basic_id = ?", ctx.Param("identity"), station.ID).
Where("delivery_account.identity = ?", ctx.Param("identity")).
First(&account).Error; err != nil {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return
@@ -108,10 +111,9 @@ func UpdateDeliveryAccountStatus(ctx *gin.Context) {
return
}
var account models.DeliveryAccount
if err := common.ActiveRecords(impl.DBService.Model(&models.DeliveryAccount{})).
if err := scopedDeliveryAccounts(impl.DBService, station.ID).
Select("delivery_account.*").
Joins("JOIN delivery_basic ON delivery_basic.id = delivery_account.delivery_basic_id").
Where("delivery_account.identity = ? AND delivery_basic.gas_basic_id = ?", ctx.Param("identity"), station.ID).
Where("delivery_account.identity = ?", ctx.Param("identity")).
First(&account).Error; err != nil {
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
return

View File

@@ -0,0 +1,31 @@
package gas
import (
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func TestScopedDeliveryAccountsQualifiesJoinedStatusColumn(t *testing.T) {
connection, _, err := sqlmock.New()
if err != nil {
t.Fatalf("create SQL mock: %v", err)
}
t.Cleanup(func() { _ = connection.Close() })
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
if err != nil {
t.Fatalf("open GORM database: %v", err)
}
statement := scopedDeliveryAccounts(databaseService, 42).
Find(&[]struct{}{}).Statement.SQL.String()
if !strings.Contains(statement, "delivery_account.status <>") {
t.Fatalf("joined query must qualify delivery account status, got: %s", statement)
}
if strings.Contains(statement, "WHERE status <>") {
t.Fatalf("joined query contains ambiguous status column: %s", statement)
}
}