fix: scope notifications by tenant

This commit is contained in:
2026-01-09 15:49:49 +08:00
parent c446dec5c6
commit 4d90f547e6
9 changed files with 62 additions and 44 deletions

View File

@@ -18,9 +18,12 @@ type notification struct {
job *job.Job
}
func (s *notification) List(ctx context.Context, userID int64, page int, typeArg string) (*requests.Pager, error) {
func (s *notification) List(ctx context.Context, tenantID, userID int64, page int, typeArg string) (*requests.Pager, error) {
tbl, q := models.NotificationQuery.QueryContext(ctx)
q = q.Where(tbl.UserID.Eq(userID))
if tenantID > 0 {
q = q.Where(tbl.TenantID.Eq(tenantID))
}
if typeArg != "" && typeArg != "all" {
q = q.Where(tbl.Type.Eq(typeArg))
@@ -58,41 +61,49 @@ func (s *notification) List(ctx context.Context, userID int64, page int, typeArg
}, nil
}
func (s *notification) MarkRead(ctx context.Context, userID, id int64) error {
_, err := models.NotificationQuery.WithContext(ctx).
Where(models.NotificationQuery.ID.Eq(id), models.NotificationQuery.UserID.Eq(userID)).
UpdateSimple(models.NotificationQuery.IsRead.Value(true))
func (s *notification) MarkRead(ctx context.Context, tenantID, userID, id int64) error {
tbl, q := models.NotificationQuery.QueryContext(ctx)
q = q.Where(tbl.ID.Eq(id), tbl.UserID.Eq(userID))
if tenantID > 0 {
q = q.Where(tbl.TenantID.Eq(tenantID))
}
_, err := q.UpdateSimple(tbl.IsRead.Value(true))
if err != nil {
return errorx.ErrDatabaseError.WithCause(err)
}
return nil
}
func (s *notification) MarkAllRead(ctx context.Context, userID int64) error {
_, err := models.NotificationQuery.WithContext(ctx).
Where(models.NotificationQuery.UserID.Eq(userID), models.NotificationQuery.IsRead.Is(false)).
UpdateSimple(models.NotificationQuery.IsRead.Value(true))
func (s *notification) MarkAllRead(ctx context.Context, tenantID, userID int64) error {
tbl, q := models.NotificationQuery.QueryContext(ctx)
q = q.Where(tbl.UserID.Eq(userID), tbl.IsRead.Is(false))
if tenantID > 0 {
q = q.Where(tbl.TenantID.Eq(tenantID))
}
_, err := q.UpdateSimple(tbl.IsRead.Value(true))
if err != nil {
return errorx.ErrDatabaseError.WithCause(err)
}
return nil
}
func (s *notification) Send(ctx context.Context, userID int64, typ, title, content string) error {
func (s *notification) Send(ctx context.Context, tenantID, userID int64, typ, title, content string) error {
arg := args.NotificationArgs{
UserID: userID,
Type: typ,
Title: title,
Content: content,
TenantID: tenantID,
UserID: userID,
Type: typ,
Title: title,
Content: content,
}
// 测试环境下同步写入,避免异步任务未启动导致结果不确定。
if os.Getenv("JOB_INLINE") == "1" {
n := &models.Notification{
UserID: userID,
Type: typ,
Title: title,
Content: content,
IsRead: false,
TenantID: tenantID,
UserID: userID,
Type: typ,
Title: title,
Content: content,
IsRead: false,
}
if err := models.NotificationQuery.WithContext(ctx).Create(n); err != nil {
return errorx.ErrDatabaseError.WithCause(err)