fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -37,6 +37,7 @@ type UpdateRequest struct {
|
||||
// EntryDTO 录单记录。
|
||||
type EntryDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
EntryNo string `json:"entry_no"`
|
||||
EntryGroup string `json:"entry_group"`
|
||||
Platform string `json:"platform"`
|
||||
@@ -86,6 +87,7 @@ type CreateResult struct {
|
||||
// OptionDTO 可选项(平台 / 操作人 / 出款实际操作人)。
|
||||
type OptionDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
GroupTag string `json:"group_tag"` // 操作人组别标记:collect/sell/空
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -21,6 +22,10 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func manualEntryTenantScope(ctx context.Context, db *gorm.DB, column string) *gorm.DB {
|
||||
return db.Where(column+" = ?", tenancy.IDFromContext(ctx))
|
||||
}
|
||||
|
||||
// FindAdminSnapshot 读取客服的昵称与登录名,用于录单时绑定快照。
|
||||
func (r *Repository) FindAdminSnapshot(ctx context.Context, adminID uint64) (name, username string, err error) {
|
||||
var admin model.AdminUser
|
||||
@@ -36,6 +41,7 @@ func (r *Repository) FindAdminSnapshot(ctx context.Context, adminID uint64) (nam
|
||||
// Create 写入一条录单记录。
|
||||
func (r *Repository) Create(ctx context.Context, adminID uint64, adminName, adminUsername, entryNo, entryGroup, platform, operator, remark string) (*EntryDTO, error) {
|
||||
row := model.CSManualEntry{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
EntryNo: entryNo,
|
||||
EntryGroup: entryGroup,
|
||||
Platform: platform,
|
||||
@@ -58,7 +64,7 @@ func (r *Repository) Create(ctx context.Context, adminID uint64, adminName, admi
|
||||
// FindByID 按 ID 查询单条录单记录。
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64) (*EntryDTO, error) {
|
||||
var row model.CSManualEntry
|
||||
if err := r.db.WithContext(ctx).First(&row, id).Error; err != nil {
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").First(&row, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrEntryNotFound
|
||||
}
|
||||
@@ -70,7 +76,7 @@ func (r *Repository) FindByID(ctx context.Context, id uint64) (*EntryDTO, error)
|
||||
|
||||
// Update 修正录单的编号、组别、平台、操作人与备注。同组内编号唯一冲突返回 ErrEntryNoExists。
|
||||
func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup, platform, operator, remark string) (*EntryDTO, error) {
|
||||
result := r.db.WithContext(ctx).Model(&model.CSManualEntry{}).
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id").
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{
|
||||
"entry_no": entryNo,
|
||||
@@ -87,7 +93,8 @@ func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup,
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.CSManualEntry{}).Where("id = ?", id).Count(&count).Error; err != nil {
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id").
|
||||
Where("id = ?", id).Count(&count).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
@@ -99,7 +106,8 @@ func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup,
|
||||
|
||||
// Delete 删除一条录单记录。
|
||||
func (r *Repository) Delete(ctx context.Context, id uint64) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.CSManualEntry{}, id)
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").
|
||||
Delete(&model.CSManualEntry{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -111,8 +119,8 @@ func (r *Repository) Delete(ctx context.Context, id uint64) error {
|
||||
|
||||
// List 按条件分页查询录单记录。
|
||||
func (r *Repository) List(ctx context.Context, query ListQuery) (*PaginatedResult, error) {
|
||||
db := r.db.WithContext(ctx).Model(&model.CSManualEntry{})
|
||||
countDB := r.db.WithContext(ctx).Model(&model.CSManualEntry{})
|
||||
db := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id")
|
||||
countDB := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntry{}), "tenant_id")
|
||||
|
||||
// 编号查询:精确匹配该编号,并同时展示其带后缀的变体(如 202611060124-0、202611060124/1)。
|
||||
if query.EntryNo != "" {
|
||||
@@ -189,7 +197,7 @@ func (r *Repository) TodayAgentStats(ctx context.Context, day time.Time) ([]Agen
|
||||
SellCount int64
|
||||
}
|
||||
rows := make([]row, 0)
|
||||
if err := r.db.WithContext(ctx).Table("cs_manual_entries").
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Table("cs_manual_entries"), "tenant_id").
|
||||
Select(`admin_id, MAX(admin_name) AS admin_name, COUNT(*) AS count,
|
||||
SUM(CASE WHEN entry_group = 'collect' THEN 1 ELSE 0 END) AS collect_count,
|
||||
SUM(CASE WHEN entry_group = 'sell' THEN 1 ELSE 0 END) AS sell_count`).
|
||||
@@ -225,7 +233,7 @@ func (r *Repository) ListOptions(ctx context.Context, optionType string) ([]Opti
|
||||
return nil, ErrOptionTypeInvalid
|
||||
}
|
||||
rows := make([]model.CSManualEntryOption, 0)
|
||||
if err := r.db.WithContext(ctx).
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").
|
||||
Where("type = ?", optionType).
|
||||
Order("sort_order ASC, id ASC").
|
||||
Find(&rows).Error; err != nil {
|
||||
@@ -265,7 +273,12 @@ func (r *Repository) CreateOption(ctx context.Context, optionType, name, groupTa
|
||||
if !IsValidOptionType(optionType) {
|
||||
return nil, ErrOptionTypeInvalid
|
||||
}
|
||||
row := model.CSManualEntryOption{Type: optionType, Name: name, GroupTag: groupTag}
|
||||
row := model.CSManualEntryOption{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
Type: optionType,
|
||||
Name: name,
|
||||
GroupTag: groupTag,
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Create(&row).Error; err != nil {
|
||||
if isDuplicateEntry(err) {
|
||||
return nil, ErrOptionExists
|
||||
@@ -279,13 +292,13 @@ func (r *Repository) CreateOption(ctx context.Context, optionType, name, groupTa
|
||||
// UpdateOption 重命名一个选项并可更新组别标记,重名返回 ErrOptionExists。
|
||||
func (r *Repository) UpdateOption(ctx context.Context, id uint64, name, groupTag string) (*OptionDTO, error) {
|
||||
var row model.CSManualEntryOption
|
||||
if err := r.db.WithContext(ctx).First(&row, id).Error; err != nil {
|
||||
if err := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").First(&row, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrOptionNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
result := r.db.WithContext(ctx).Model(&model.CSManualEntryOption{}).
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx).Model(&model.CSManualEntryOption{}), "tenant_id").
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{
|
||||
"name": name,
|
||||
@@ -305,7 +318,8 @@ func (r *Repository) UpdateOption(ctx context.Context, id uint64, name, groupTag
|
||||
|
||||
// DeleteOption 删除一个选项(历史录单里的名称快照不受影响)。
|
||||
func (r *Repository) DeleteOption(ctx context.Context, id uint64) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.CSManualEntryOption{}, id)
|
||||
result := manualEntryTenantScope(ctx, r.db.WithContext(ctx), "tenant_id").
|
||||
Delete(&model.CSManualEntryOption{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -340,6 +354,7 @@ func isDuplicateEntry(err error) bool {
|
||||
func toDTO(row model.CSManualEntry) EntryDTO {
|
||||
return EntryDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
EntryNo: row.EntryNo,
|
||||
EntryGroup: row.EntryGroup,
|
||||
Platform: row.Platform,
|
||||
@@ -355,6 +370,7 @@ func toDTO(row model.CSManualEntry) EntryDTO {
|
||||
func toOptionDTO(row model.CSManualEntryOption) OptionDTO {
|
||||
return OptionDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
Type: row.Type,
|
||||
Name: row.Name,
|
||||
GroupTag: row.GroupTag,
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package csmanualentry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func manualEntryTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func TestManualEntriesAndOptionsAreScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
tenants := []model.Tenant{
|
||||
{Code: "manual_entry_a", Name: "录单A站", Status: "active"},
|
||||
{Code: "manual_entry_b", Name: "录单B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
admin := model.AdminUser{Username: "manual-cs", Nickname: "录单客服", PasswordHash: "x", Status: "active"}
|
||||
if err := db.Create(&admin).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
ctxA := manualEntryTenantContext(tenants[0].ID)
|
||||
ctxB := manualEntryTenantContext(tenants[1].ID)
|
||||
|
||||
entryA, err := repo.Create(ctxA, admin.ID, admin.Nickname, admin.Username, "SAME-ENTRY", EntryGroupCollect, "平台A", "客服A", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entryB, err := repo.Create(ctxB, admin.ID, admin.Nickname, admin.Username, "SAME-ENTRY", EntryGroupCollect, "平台B", "客服B", "")
|
||||
if err != nil {
|
||||
t.Fatalf("same entry number in another tenant: %v", err)
|
||||
}
|
||||
if entryA.ID == entryB.ID || entryA.TenantID != tenants[0].ID || entryB.TenantID != tenants[1].ID {
|
||||
t.Fatalf("tenant entries A=%+v B=%+v", entryA, entryB)
|
||||
}
|
||||
|
||||
listA, err := repo.List(ctxA, ListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listB, err := repo.List(ctxB, ListQuery{Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if listA.Total != 1 || listA.Items[0].ID != entryA.ID || listB.Total != 1 || listB.Items[0].ID != entryB.ID {
|
||||
t.Fatalf("tenant manual entry lists A=%+v B=%+v", listA, listB)
|
||||
}
|
||||
if _, err := repo.FindByID(ctxB, entryA.ID); !errors.Is(err, ErrEntryNotFound) {
|
||||
t.Fatalf("cross-tenant find error = %v", err)
|
||||
}
|
||||
if _, err := repo.Update(ctxB, entryA.ID, "HACK", EntryGroupSell, "", "", ""); !errors.Is(err, ErrEntryNotFound) {
|
||||
t.Fatalf("cross-tenant update error = %v", err)
|
||||
}
|
||||
if err := repo.Delete(ctxB, entryA.ID); !errors.Is(err, ErrEntryNotFound) {
|
||||
t.Fatalf("cross-tenant delete error = %v", err)
|
||||
}
|
||||
|
||||
optionA, err := repo.CreateOption(ctxA, OptionTypePlatform, "同名平台", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
optionB, err := repo.CreateOption(ctxB, OptionTypePlatform, "同名平台", "")
|
||||
if err != nil {
|
||||
t.Fatalf("same option name in another tenant: %v", err)
|
||||
}
|
||||
if optionA.ID == optionB.ID || optionA.TenantID != tenants[0].ID || optionB.TenantID != tenants[1].ID {
|
||||
t.Fatalf("tenant options A=%+v B=%+v", optionA, optionB)
|
||||
}
|
||||
optionsA, err := repo.ListAllOptions(ctxA)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
optionsB, err := repo.ListAllOptions(ctxB)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(optionsA.Platforms) != 1 || optionsA.Platforms[0].ID != optionA.ID ||
|
||||
len(optionsB.Platforms) != 1 || optionsB.Platforms[0].ID != optionB.ID {
|
||||
t.Fatalf("tenant options list A=%+v B=%+v", optionsA, optionsB)
|
||||
}
|
||||
if _, err := repo.UpdateOption(ctxB, optionA.ID, "越权", ""); !errors.Is(err, ErrOptionNotFound) {
|
||||
t.Fatalf("cross-tenant option update error = %v", err)
|
||||
}
|
||||
if err := repo.DeleteOption(ctxB, optionA.ID); !errors.Is(err, ErrOptionNotFound) {
|
||||
t.Fatalf("cross-tenant option delete error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user