fix(invitation): 按经营租户隔离邀请与返利数据
This commit is contained in:
@@ -2,7 +2,9 @@ package invitation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -51,3 +53,102 @@ func TestRebateConfigIsScopedByTenant(t *testing.T) {
|
||||
t.Fatalf("tenant B config = %+v", cfgB)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminInvitationAndRebateQueriesAreScopedByTenant(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
repo := NewRepository(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101, TenantCode: "dachui"})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202, TenantCode: "jieyou"})
|
||||
now := time.Now()
|
||||
|
||||
codeA1 := "tenant-a-inviter"
|
||||
codeB1 := "tenant-b-inviter"
|
||||
users := []model.User{
|
||||
{TenantID: 101, Phone: "13800000101", Nickname: "大锤邀请人", Status: "active", RealnameStatus: "verified", InviteCode: &codeA1},
|
||||
{TenantID: 101, Phone: "13900000101", Nickname: "大锤被邀请人", Status: "active", RealnameStatus: "verified"},
|
||||
{TenantID: 202, Phone: "13800000202", Nickname: "解忧邀请人", Status: "active", RealnameStatus: "verified", InviteCode: &codeB1},
|
||||
{TenantID: 202, Phone: "13900000202", Nickname: "解忧被邀请人", Status: "active", RealnameStatus: "verified"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("create tenant users: %v", err)
|
||||
}
|
||||
|
||||
invitations := []model.UserInvitation{
|
||||
{InviterID: users[0].ID, InviteeID: users[1].ID, InviteCode: codeA1, Status: StatusActive, VerifiedAt: &now},
|
||||
{InviterID: users[2].ID, InviteeID: users[3].ID, InviteCode: codeB1, Status: StatusActive, VerifiedAt: &now},
|
||||
}
|
||||
if err := db.Create(&invitations).Error; err != nil {
|
||||
t.Fatalf("create invitations: %v", err)
|
||||
}
|
||||
|
||||
ledgers := []model.InvitationRebateLedger{
|
||||
{
|
||||
OrderID: 9001, OrderNo: "ORDER-TENANT-A", Source: "rental_order",
|
||||
InviterID: users[0].ID, InviteeID: users[1].ID, TradeRole: "buyer",
|
||||
RateApplied: 0.6, CycleStage: CycleFirstMonth, RebateAmountCent: 100,
|
||||
Status: "settled", SettledAt: now,
|
||||
},
|
||||
{
|
||||
OrderID: 9002, OrderNo: "ORDER-TENANT-B", Source: "rental_order",
|
||||
InviterID: users[2].ID, InviteeID: users[3].ID, TradeRole: "buyer",
|
||||
RateApplied: 0.6, CycleStage: CycleFirstMonth, RebateAmountCent: 200,
|
||||
Status: "settled", SettledAt: now,
|
||||
},
|
||||
}
|
||||
if err := db.Create(&ledgers).Error; err != nil {
|
||||
t.Fatalf("create rebate ledgers: %v", err)
|
||||
}
|
||||
|
||||
itemsA, totalA, err := repo.ListAdminInvitations(ctxA, AdminInvitationQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("list tenant A invitations: %v", err)
|
||||
}
|
||||
if totalA != 1 || len(itemsA) != 1 {
|
||||
t.Fatalf("tenant A invitations total/items = %d/%d, want 1/1", totalA, len(itemsA))
|
||||
}
|
||||
if got := itemsA[0]["inviter_phone"]; got != "13800000101" {
|
||||
t.Fatalf("tenant A inviter phone = %v", got)
|
||||
}
|
||||
if got := itemsA[0]["inviter_total_invited"]; got != int64(1) {
|
||||
t.Fatalf("tenant A inviter total invited = %v, want 1", got)
|
||||
}
|
||||
|
||||
itemsB, totalB, err := repo.ListAdminInvitations(ctxB, AdminInvitationQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("list tenant B invitations: %v", err)
|
||||
}
|
||||
if totalB != 1 || len(itemsB) != 1 || itemsB[0]["inviter_phone"] != "13800000202" {
|
||||
t.Fatalf("tenant B invitations leaked: total=%d items=%+v", totalB, itemsB)
|
||||
}
|
||||
|
||||
ledgersA, totalLedgersA, err := repo.ListAdminRebateLedgers(ctxA, AdminRebateLedgerQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("list tenant A rebate ledgers: %v", err)
|
||||
}
|
||||
if totalLedgersA != 1 || len(ledgersA) != 1 || ledgersA[0]["order_no"] != "ORDER-TENANT-A" {
|
||||
t.Fatalf("tenant A rebate ledgers leaked: total=%d items=%+v", totalLedgersA, ledgersA)
|
||||
}
|
||||
|
||||
summaryA, err := repo.AdminRebateSummary(ctxA, AdminRebateLedgerQuery{})
|
||||
if err != nil {
|
||||
t.Fatalf("tenant A rebate summary: %v", err)
|
||||
}
|
||||
if summaryA.LedgerCount != 1 || summaryA.TotalRebateCent != 100 {
|
||||
t.Fatalf("tenant A rebate summary leaked: %+v", summaryA)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvitationTenantScopeSQL(t *testing.T) {
|
||||
db := database.NewDryRunDB()
|
||||
ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202, TenantCode: "jieyou"})
|
||||
|
||||
stmt := applyInvitationUserTenantScope(ctx, db.Table("user_invitations AS ui"), "inviter", "invitee").
|
||||
Find(&[]struct{}{}).Statement
|
||||
sql := stmt.SQL.String()
|
||||
if !strings.Contains(sql, "inviter.tenant_id = ? AND invitee.tenant_id = ?") {
|
||||
t.Fatalf("tenant scope SQL missing: %s", sql)
|
||||
}
|
||||
if len(stmt.Vars) < 2 || stmt.Vars[0] != uint64(202) || stmt.Vars[1] != uint64(202) {
|
||||
t.Fatalf("tenant scope args = %#v", stmt.Vars)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user