fix(search): 修复跨租户重复手机号查询报错
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package dispute
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func TestAdminDisputePhoneFilterWithDuplicateAccounts(t *testing.T) {
|
||||
db := setupDisputeTestDB(t)
|
||||
const phone = "13812345678"
|
||||
wantByTenant := map[uint64]map[uint64]bool{101: {}, 202: {}}
|
||||
for i, tenantID := range []uint64{101, 101, 202, 101} {
|
||||
owner, renter, order := createDisputeOrderFixture(t, db, model.RentalOrder{
|
||||
OrderNo: fmt.Sprintf("RO-PHONE-%d", i), BuyerTenantID: tenantID,
|
||||
SourceTenantID: 303, Status: "renting",
|
||||
})
|
||||
if i < 3 {
|
||||
userID := owner.ID
|
||||
if i == 1 {
|
||||
userID = renter.ID
|
||||
}
|
||||
// 匹配账号可来自货源租户,不能把手机号子查询限定到买方租户。
|
||||
if err := db.Model(&model.User{}).Where("id = ?", userID).
|
||||
Updates(map[string]any{"tenant_id": 303 + i, "phone": phone}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
dispute := model.Dispute{OrderID: order.ID, InitiatorID: renter.ID,
|
||||
InitiatorType: "user", TargetUserID: owner.ID, Type: "cannot_login", Status: "open"}
|
||||
if err := db.Create(&dispute).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i < 3 {
|
||||
wantByTenant[tenantID][dispute.ID] = true
|
||||
}
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
for tenantID, want := range wantByTenant {
|
||||
t.Run(fmt.Sprint(tenantID), func(t *testing.T) {
|
||||
ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenantID})
|
||||
got, err := repo.ListAdmin(ctx, AdminListQuery{Keyword: phone, Page: 1, PageSize: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Total != int64(len(want)) || len(got.Items) != 1 || !want[got.Items[0].ID] {
|
||||
t.Fatalf("phone filter returned unexpected disputes: %+v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -151,9 +151,9 @@ func applyAdminListFilters(db *gorm.DB, query AdminListQuery) *gorm.DB {
|
||||
if keyword := strings.TrimSpace(query.Keyword); keyword != "" {
|
||||
upper := strings.ToUpper(keyword)
|
||||
if isPhoneKeyword(keyword) {
|
||||
// 先由 users(phone) 唯一索引解析用户,再走订单号主/租客索引。
|
||||
// 手机号可对应多个租户或站点账号,匹配用户集合后再走订单号主/租客索引。
|
||||
db = db.Where(
|
||||
"(o.owner_id = (SELECT id FROM users WHERE phone = ?) OR o.renter_id = (SELECT id FROM users WHERE phone = ?))",
|
||||
"(o.owner_id IN (SELECT id FROM users WHERE phone = ?) OR o.renter_id IN (SELECT id FROM users WHERE phone = ?))",
|
||||
keyword,
|
||||
keyword,
|
||||
)
|
||||
|
||||
@@ -781,7 +781,7 @@ func TestApplyAdminListFiltersSmartRouting(t *testing.T) {
|
||||
{
|
||||
name: "11位手机号先解析用户再走订单角色索引",
|
||||
query: AdminListQuery{Keyword: "13800138000"},
|
||||
wantSQLFrag: "o.owner_id = (SELECT id FROM users WHERE phone = ?)",
|
||||
wantSQLFrag: "o.owner_id IN (SELECT id FROM users WHERE phone = ?)",
|
||||
},
|
||||
{
|
||||
name: "RO前缀订单号路由至rental_orders(order_no)",
|
||||
|
||||
Reference in New Issue
Block a user