feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -21,6 +22,8 @@ func (r *Repository) List(ctx context.Context, query Query) (*PaginatedResult, e
|
||||
Where("admin_user_id = ?", query.AdminUserID)
|
||||
countDB := r.db.WithContext(ctx).Model(&model.AdminNotification{}).
|
||||
Where("admin_user_id = ?", query.AdminUserID)
|
||||
db = tenantFilter(ctx, db)
|
||||
countDB = tenantFilter(ctx, countDB)
|
||||
if query.OnlyUnread {
|
||||
db = db.Where("is_read = ?", false)
|
||||
countDB = countDB.Where("is_read = ?", false)
|
||||
@@ -47,6 +50,7 @@ func (r *Repository) UnreadCount(ctx context.Context, adminUserID uint64) (int64
|
||||
var total int64
|
||||
err := r.db.WithContext(ctx).Model(&model.AdminNotification{}).
|
||||
Where("admin_user_id = ? AND is_read = ?", adminUserID, false).
|
||||
Scopes(func(db *gorm.DB) *gorm.DB { return tenantFilter(ctx, db) }).
|
||||
Count(&total).Error
|
||||
return total, err
|
||||
}
|
||||
@@ -54,6 +58,7 @@ func (r *Repository) UnreadCount(ctx context.Context, adminUserID uint64) (int64
|
||||
func (r *Repository) MarkRead(ctx context.Context, adminUserID uint64, id uint64) error {
|
||||
result := r.db.WithContext(ctx).Model(&model.AdminNotification{}).
|
||||
Where("id = ? AND admin_user_id = ?", id, adminUserID).
|
||||
Scopes(func(db *gorm.DB) *gorm.DB { return tenantFilter(ctx, db) }).
|
||||
Update("is_read", true)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
@@ -62,6 +67,7 @@ func (r *Repository) MarkRead(ctx context.Context, adminUserID uint64, id uint64
|
||||
var total int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.AdminNotification{}).
|
||||
Where("id = ? AND admin_user_id = ?", id, adminUserID).
|
||||
Scopes(func(db *gorm.DB) *gorm.DB { return tenantFilter(ctx, db) }).
|
||||
Count(&total).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -75,12 +81,14 @@ func (r *Repository) MarkRead(ctx context.Context, adminUserID uint64, id uint64
|
||||
func (r *Repository) MarkAllRead(ctx context.Context, adminUserID uint64) (int64, error) {
|
||||
result := r.db.WithContext(ctx).Model(&model.AdminNotification{}).
|
||||
Where("admin_user_id = ? AND is_read = ?", adminUserID, false).
|
||||
Scopes(func(db *gorm.DB) *gorm.DB { return tenantFilter(ctx, db) }).
|
||||
Update("is_read", true)
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
// Entry 描述一条待写入的管理员通知。
|
||||
type Entry struct {
|
||||
TenantID uint64
|
||||
AdminUserID uint64
|
||||
Type string
|
||||
Title string
|
||||
@@ -95,7 +103,21 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
if entry.AdminUserID == 0 || entry.Title == "" {
|
||||
continue
|
||||
}
|
||||
tenantID := entry.TenantID
|
||||
if tenantID == 0 {
|
||||
var row struct {
|
||||
TenantID uint64
|
||||
}
|
||||
if err := tx.Table("admin_user_roles").
|
||||
Select("tenant_id").
|
||||
Where("admin_user_id = ? AND tenant_id > 0", entry.AdminUserID).
|
||||
Order("tenant_id ASC").
|
||||
Take(&row).Error; err == nil {
|
||||
tenantID = row.TenantID
|
||||
}
|
||||
}
|
||||
row := model.AdminNotification{
|
||||
TenantID: tenantID,
|
||||
AdminUserID: entry.AdminUserID,
|
||||
Type: entry.Type,
|
||||
Title: entry.Title,
|
||||
@@ -114,6 +136,7 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
func toDTO(row model.AdminNotification) NotificationDTO {
|
||||
return NotificationDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
AdminUserID: row.AdminUserID,
|
||||
Type: row.Type,
|
||||
Title: row.Title,
|
||||
@@ -123,3 +146,10 @@ func toDTO(row model.AdminNotification) NotificationDTO {
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user