推进P1后台任务与通知补偿收口
This commit is contained in:
1 parent
3fdd0fe267
commit
25531c05cb
15 files changed
+985
-59
No files matched your search
@@ -0,0 +1,90 @@
|
||||
// Package notifications 提供通知运维查询等用例的应用层编排,不依赖 HTTP 或数据库模型。
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ErrInvalidInput 表示通知运维查询缺少有效的用户归属或持久化端口。
|
||||
var ErrInvalidInput = errors.New("通知运维查询参数无效")
|
||||
|
||||
// UncertainSummary 是外部通知已发送但本地确认失败时的非敏感摘要。
|
||||
// 该模型不携带通知正文、渠道配置或错误原文,允许安全地传递到 HTTP 层。
|
||||
type UncertainSummary struct {
|
||||
// ID 是通知 outbox 记录的稳定标识,仅用于运维定位。
|
||||
ID int64
|
||||
// ChannelID 是关联通知渠道标识,用于展示归属线索。
|
||||
ChannelID int64
|
||||
// OwnerUserID 是通知渠道所属用户;普通用户结果由应用服务保证只包含请求用户。
|
||||
OwnerUserID int64
|
||||
// EventType 是通知事件分类,不包含通知正文。
|
||||
EventType string
|
||||
// AttemptCount 是进入不确定状态前的发送尝试次数。
|
||||
AttemptCount int
|
||||
// UncertainAt 是进入不确定状态的 Unix 秒时间戳。
|
||||
UncertainAt int64
|
||||
// HasError 表示是否记录过本地确认错误,但不暴露错误原文。
|
||||
HasError bool
|
||||
}
|
||||
|
||||
// Repository 定义通知不确定状态查询所需的最小持久化端口。
|
||||
type Repository interface {
|
||||
// ListUncertainForUser 查询指定用户渠道的不确定通知摘要。
|
||||
ListUncertainForUser(ctx context.Context, userID int64, limit int) ([]UncertainSummary, error)
|
||||
// CountUncertainForUser 统计指定用户渠道的不确定通知数量。
|
||||
CountUncertainForUser(ctx context.Context, userID int64) (int, error)
|
||||
// ListUncertainForAdmin 查询所有用户渠道的不确定通知摘要。
|
||||
ListUncertainForAdmin(ctx context.Context, limit int) ([]UncertainSummary, error)
|
||||
// CountUncertainForAdmin 统计所有用户渠道的不确定通知数量。
|
||||
CountUncertainForAdmin(ctx context.Context) (int, error)
|
||||
}
|
||||
|
||||
// Service 编排通知不确定状态查询,不持有 HTTP 请求或数据库连接。
|
||||
type Service struct {
|
||||
// repository 保存调用方注入的最小通知查询端口。
|
||||
repository Repository
|
||||
}
|
||||
|
||||
// New 创建通知不确定状态应用服务;空端口会导致构造结果不可用。
|
||||
func New(repository Repository) *Service {
|
||||
return &Service{repository: repository}
|
||||
}
|
||||
|
||||
// ListForUser 查询当前用户有权查看的不确定通知摘要及总数。
|
||||
// userID 用于归属隔离,limit 控制列表上限;底层端口错误原样返回且不会泄露正文。
|
||||
func (s *Service) ListForUser(ctx context.Context, userID int64, limit int) ([]UncertainSummary, int, error) {
|
||||
if s == nil || s.repository == nil || userID <= 0 {
|
||||
return nil, 0, ErrInvalidInput
|
||||
}
|
||||
// items 保存按用户归属过滤后的非敏感通知摘要。
|
||||
items, err := s.repository.ListUncertainForUser(ctx, userID, limit)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
// total 保存当前用户所有不确定通知的数量。
|
||||
total, err := s.repository.CountUncertainForUser(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
// ListForAdmin 查询管理员可见的全局不确定通知摘要及总数。
|
||||
// limit 控制列表上限;结果仍只包含脱敏元数据,底层端口错误原样返回。
|
||||
func (s *Service) ListForAdmin(ctx context.Context, limit int) ([]UncertainSummary, int, error) {
|
||||
if s == nil || s.repository == nil {
|
||||
return nil, 0, ErrInvalidInput
|
||||
}
|
||||
// items 保存全局非敏感通知摘要列表。
|
||||
items, err := s.repository.ListUncertainForAdmin(ctx, limit)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
// total 保存全局不确定通知的数量。
|
||||
total, err := s.repository.CountUncertainForAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// fakeRepository 是通知不确定状态应用服务测试使用的端口替身。
|
||||
type fakeRepository struct {
|
||||
// userItems 保存普通用户查询应返回的脱敏摘要。
|
||||
userItems []UncertainSummary
|
||||
// adminItems 保存管理员查询应返回的脱敏摘要。
|
||||
adminItems []UncertainSummary
|
||||
// userTotal 保存普通用户查询应返回的总数。
|
||||
userTotal int
|
||||
// adminTotal 保存管理员查询应返回的总数。
|
||||
adminTotal int
|
||||
// listErr 保存列表查询错误。
|
||||
listErr error
|
||||
// countErr 保存统计查询错误。
|
||||
countErr error
|
||||
// userID 保存普通用户查询收到的归属标识。
|
||||
userID int64
|
||||
// userLimit 保存普通用户查询收到的上限。
|
||||
userLimit int
|
||||
// adminLimit 保存管理员查询收到的上限。
|
||||
adminLimit int
|
||||
}
|
||||
|
||||
// ListUncertainForUser 返回普通用户预置摘要并记录归属参数。
|
||||
func (r *fakeRepository) ListUncertainForUser(_ context.Context, userID int64, limit int) ([]UncertainSummary, error) {
|
||||
r.userID = userID
|
||||
r.userLimit = limit
|
||||
return r.userItems, r.listErr
|
||||
}
|
||||
|
||||
// CountUncertainForUser 返回普通用户预置总数。
|
||||
func (r *fakeRepository) CountUncertainForUser(context.Context, int64) (int, error) {
|
||||
return r.userTotal, r.countErr
|
||||
}
|
||||
|
||||
// ListUncertainForAdmin 返回管理员预置摘要并记录查询上限。
|
||||
func (r *fakeRepository) ListUncertainForAdmin(_ context.Context, limit int) ([]UncertainSummary, error) {
|
||||
r.adminLimit = limit
|
||||
return r.adminItems, r.listErr
|
||||
}
|
||||
|
||||
// CountUncertainForAdmin 返回管理员预置总数。
|
||||
func (r *fakeRepository) CountUncertainForAdmin(context.Context) (int, error) {
|
||||
return r.adminTotal, r.countErr
|
||||
}
|
||||
|
||||
// TestListForUserReturnsScopedSummary 验证普通用户查询传递归属并返回列表与总数。
|
||||
func TestListForUserReturnsScopedSummary(t *testing.T) {
|
||||
// repository 是带有非敏感摘要夹具的持久化端口替身。
|
||||
repository := &fakeRepository{userItems: []UncertainSummary{{ID: 7, EventType: "order"}}, userTotal: 3}
|
||||
// service 是待验证的通知应用服务。
|
||||
service := New(repository)
|
||||
// items、total、err 保存普通用户查询结果。
|
||||
items, total, err := service.ListForUser(context.Background(), 9, 17)
|
||||
if err != nil || total != 3 || len(items) != 1 || repository.userID != 9 || repository.userLimit != 17 {
|
||||
t.Fatalf("普通用户查询结果异常: items=%+v total=%d err=%v repository=%+v", items, total, err, repository)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListForAdminReturnsGlobalSummary 验证管理员查询使用全局端口并保留运维上限。
|
||||
func TestListForAdminReturnsGlobalSummary(t *testing.T) {
|
||||
// repository 是带有管理员摘要夹具的持久化端口替身。
|
||||
repository := &fakeRepository{adminItems: []UncertainSummary{{ID: 8, OwnerUserID: 2}}, adminTotal: 4}
|
||||
// service 是待验证的通知应用服务。
|
||||
service := New(repository)
|
||||
// items、total、err 保存管理员查询结果。
|
||||
items, total, err := service.ListForAdmin(context.Background(), 50)
|
||||
if err != nil || total != 4 || len(items) != 1 || repository.adminLimit != 50 || items[0].OwnerUserID != 2 {
|
||||
t.Fatalf("管理员查询结果异常: items=%+v total=%d err=%v repository=%+v", items, total, err, repository)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListRejectsInvalidServiceAndUser 验证缺少端口或无效用户归属时拒绝执行查询。
|
||||
func TestListRejectsInvalidServiceAndUser(t *testing.T) {
|
||||
// repository 是满足接口但不应被无效输入调用的端口替身。
|
||||
repository := &fakeRepository{}
|
||||
// cases 保存需要返回参数错误的服务与用户组合。
|
||||
cases := []struct {
|
||||
// name 是当前无效输入场景名称。
|
||||
name string
|
||||
// service 是当前场景的应用服务。
|
||||
service *Service
|
||||
// userID 是当前场景的用户归属标识。
|
||||
userID int64
|
||||
}{
|
||||
{name: "空服务", service: nil, userID: 1},
|
||||
{name: "空端口", service: New(nil), userID: 1},
|
||||
{name: "无效用户", service: New(repository), userID: 0},
|
||||
}
|
||||
// testCase 表示当前遍历的无效输入场景。
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
// _, err 保存当前场景的普通用户查询结果。
|
||||
_, _, err := testCase.service.ListForUser(context.Background(), testCase.userID, 10)
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("无效输入未被拒绝: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
// _, adminErr 保存空端口管理员查询结果。
|
||||
_, _, adminErr := New(nil).ListForAdmin(context.Background(), 10)
|
||||
if !errors.Is(adminErr, ErrInvalidInput) {
|
||||
t.Fatalf("空端口管理员查询未被拒绝: %v", adminErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListPropagatesRepositoryErrors 验证列表或统计失败都会原样返回且不伪造结果。
|
||||
func TestListPropagatesRepositoryErrors(t *testing.T) {
|
||||
// listErr 是持久化列表查询失败原因。
|
||||
listErr := errors.New("列表端口失败")
|
||||
// listRepository 是模拟列表失败的端口替身。
|
||||
listRepository := &fakeRepository{listErr: listErr}
|
||||
// _, _, gotListErr 保存列表失败的应用服务结果。
|
||||
_, _, gotListErr := New(listRepository).ListForUser(context.Background(), 1, 10)
|
||||
if !errors.Is(gotListErr, listErr) {
|
||||
t.Fatalf("列表错误未透传: %v", gotListErr)
|
||||
}
|
||||
// countErr 是持久化统计查询失败原因。
|
||||
countErr := errors.New("统计端口失败")
|
||||
// countRepository 是模拟统计失败的端口替身。
|
||||
countRepository := &fakeRepository{countErr: countErr}
|
||||
// _, _, gotCountErr 保存统计失败的应用服务结果。
|
||||
_, _, gotCountErr := New(countRepository).ListForAdmin(context.Background(), 10)
|
||||
if !errors.Is(gotCountErr, countErr) {
|
||||
t.Fatalf("统计错误未透传: %v", gotCountErr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user