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",