feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -4,6 +4,7 @@ import "time"
|
||||
|
||||
type NotificationDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -16,6 +17,7 @@ type Repository struct {
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
TenantID uint64
|
||||
UserID uint64
|
||||
Type string
|
||||
Title string
|
||||
@@ -62,6 +64,7 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
var total int64
|
||||
@@ -71,6 +74,7 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []model.Notification
|
||||
dataQuery := r.db.WithContext(ctx).Where("user_id = ?", userID)
|
||||
dataQuery = tenantFilter(ctx, dataQuery)
|
||||
dataQuery = r.applySiteFilter(dataQuery, siteID, isSite)
|
||||
if err := dataQuery.Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -94,6 +98,7 @@ func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64, sit
|
||||
|
||||
now := time.Now()
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
result := query.Update("read_at", now)
|
||||
@@ -102,6 +107,7 @@ func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64, sit
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
checkQuery := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
||||
checkQuery = tenantFilter(ctx, checkQuery)
|
||||
checkQuery = r.applySiteFilter(checkQuery, siteID, isSite)
|
||||
var total int64
|
||||
if err := checkQuery.Count(&total).Error; err != nil {
|
||||
@@ -125,6 +131,7 @@ func (r *Repository) UnreadCount(ctx context.Context, userID uint64, siteCode ..
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
var total int64
|
||||
@@ -144,6 +151,7 @@ func (r *Repository) MarkAllRead(ctx context.Context, userID uint64, siteCode ..
|
||||
|
||||
now := time.Now()
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
result := query.Update("read_at", now)
|
||||
@@ -155,6 +163,13 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
if entry.UserID == 0 || entry.Title == "" {
|
||||
continue
|
||||
}
|
||||
tenantID := entry.TenantID
|
||||
if tenantID == 0 {
|
||||
var user model.User
|
||||
if err := tx.Select("tenant_id").First(&user, entry.UserID).Error; err == nil {
|
||||
tenantID = user.TenantID
|
||||
}
|
||||
}
|
||||
siteID := entry.DistributionSiteID
|
||||
if siteID == nil {
|
||||
if (entry.BizType == "order" || entry.BizType == "order_handoff") && entry.BizID != nil && *entry.BizID > 0 {
|
||||
@@ -174,6 +189,7 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
}
|
||||
}
|
||||
row := model.Notification{
|
||||
TenantID: tenantID,
|
||||
UserID: entry.UserID,
|
||||
Type: entry.Type,
|
||||
Title: entry.Title,
|
||||
@@ -195,6 +211,7 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
func toDTO(row model.Notification) NotificationDTO {
|
||||
return NotificationDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
UserID: row.UserID,
|
||||
Type: row.Type,
|
||||
Title: row.Title,
|
||||
@@ -206,3 +223,10 @@ func toDTO(row model.Notification) NotificationDTO {
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func tenantFilter(ctx context.Context, db *gorm.DB) *gorm.DB {
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
return db.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func TestRepositoryUnreadCountAndMarkRead(t *testing.T) {
|
||||
@@ -191,3 +192,53 @@ func TestNotificationTenantIsolation(t *testing.T) {
|
||||
t.Fatalf("autoNotif.DistributionSiteID = %v, want %d", autoNotif.DistributionSiteID, siteID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationTenantScopeAndAppendDerivation(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Tenant{}, &model.User{}, &model.Notification{}); err != nil {
|
||||
t.Fatalf("AutoMigrate() error = %v", err)
|
||||
}
|
||||
tenantA := model.Tenant{Code: "notify-a", Name: "通知 A", Status: "active"}
|
||||
tenantB := model.Tenant{Code: "notify-b", Name: "通知 B", Status: "active"}
|
||||
if err := db.Create(&tenantA).Error; err != nil {
|
||||
t.Fatalf("创建租户 A: %v", err)
|
||||
}
|
||||
if err := db.Create(&tenantB).Error; err != nil {
|
||||
t.Fatalf("创建租户 B: %v", err)
|
||||
}
|
||||
userA := model.User{TenantID: tenantA.ID, Phone: "13700000001"}
|
||||
userB := model.User{TenantID: tenantB.ID, Phone: "13700000002"}
|
||||
if err := db.Create(&userA).Error; err != nil {
|
||||
t.Fatalf("创建用户 A: %v", err)
|
||||
}
|
||||
if err := db.Create(&userB).Error; err != nil {
|
||||
t.Fatalf("创建用户 B: %v", err)
|
||||
}
|
||||
if err := Append(db,
|
||||
Entry{UserID: userA.ID, Title: "A 通知"},
|
||||
Entry{UserID: userB.ID, Title: "B 通知"},
|
||||
); err != nil {
|
||||
t.Fatalf("Append(): %v", err)
|
||||
}
|
||||
var rows []model.Notification
|
||||
if err := db.Order("id ASC").Find(&rows).Error; err != nil {
|
||||
t.Fatalf("查询通知: %v", err)
|
||||
}
|
||||
if len(rows) != 2 || rows[0].TenantID != tenantA.ID || rows[1].TenantID != tenantB.ID {
|
||||
t.Fatalf("通知租户回填异常: %+v", rows)
|
||||
}
|
||||
|
||||
repo := NewRepository(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantA.ID})
|
||||
listA, err := repo.List(ctxA, userA.ID, 1, 20)
|
||||
if err != nil {
|
||||
t.Fatalf("查询租户 A 通知: %v", err)
|
||||
}
|
||||
if listA.Total != 1 {
|
||||
t.Fatalf("租户 A 通知数 = %d, want 1", listA.Total)
|
||||
}
|
||||
ctxWrong := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantB.ID})
|
||||
if err := repo.MarkRead(ctxWrong, userA.ID, rows[0].ID); !errors.Is(err, ErrNotificationNotFound) {
|
||||
t.Fatalf("跨租户标记已读 error = %v, want not found", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user