迁移订单详情应用服务
This commit is contained in:
1 parent
10371fa74a
commit
67a12ff53a
5 files changed
+172
-20
No files matched your search
@@ -749,3 +749,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P1 订单运行时 Port 基础设施类型收口切片 | 已完成 | `orderRuntimePort` 的完整自动化发货参数改为应用层 `orders.Order`,Cookie 会话持久化参数改为 `orders.PlatformRuntimeData`;`db.Order`/`db.CookieDetail` 转换下沉到 Server 装配适配器,业务服务不再通过运行时 Port 接触数据库模型;补充订单与平台视图边界转换回归,架构门禁、中文注释门禁和订单定向测试通过。订单应用服务实现仍位于 `internal/server`,Port 仍含平台协议接口,其他应用服务和基础设施适配器继续按后续切片迁移,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 调度器与通知 worker 关闭上下文切片 | 已完成 | 自动化调度器、续期调度器和通知 outbox worker 新增 `WaitContext`;`cmd/server` 在同一个 10 秒关闭 Context 内等待 HTTP、后台 worker、调度器、账号和通知,并将 `stopCancel` 延后到全部等待结束,避免使用已取消 Context;补充三个 worker 超时/完成回归测试,相关包测试和中文注释门禁通过。Engine 内部任务等待、平台浏览器关闭和生命周期统一清单仍待继续治理,阶段 5 保持“进行中” |
|
||||
| 2026-08-15 | P1 订单列表应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.ListService`、纯业务 `ListQuery/ListResult` 和窄 `ListRepository`;分页规范化、账号所有权校验和列表查询从 `internal/server/order_service.go` 迁入应用层,Server 仅负责 DTO 转换和兼容错误映射;新增应用层分页/跨用户/存储错误测试,Server 列表与订单服务回归、架构门禁和中文注释门禁通过。订单详情、更新、导入、手动发货、刷新等用例仍在 Server,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 订单详情应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.DetailService`、`DetailResult` 和窄 `DetailRepository`;订单详情读取、账号所有权校验和商品补全从 `internal/server/order_service.go` 迁入应用层,Server 仅负责兼容错误映射与 DTO 转换;新增跨用户、不存在订单、商品读取失败保留订单主体测试,订单/Server 回归、架构门禁和中文注释门禁通过。订单更新、导入、手动发货、刷新等用例仍在 Server,阶段 4/8/9 保持“进行中” |
|
||||
@@ -0,0 +1,81 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrNotFound 表示订单不存在或无法读取。
|
||||
var ErrNotFound = errors.New("订单不存在")
|
||||
|
||||
// DetailResult 是订单详情用例返回的订单和关联商品信息。
|
||||
type DetailResult struct {
|
||||
// Order 是经过当前用户所有权校验的订单实体。
|
||||
Order *Order
|
||||
// Item 是关联商品信息,读取失败时允许为空。
|
||||
Item *ItemInfo
|
||||
}
|
||||
|
||||
// DetailRepository 定义订单详情用例所需的最小数据访问能力。
|
||||
type DetailRepository interface {
|
||||
// ExistsOwned 判断账号是否归属于指定用户。
|
||||
ExistsOwned(ctx context.Context, userID int64, cookieID string) (bool, error)
|
||||
// GetOrder 读取订单实体。
|
||||
GetOrder(ctx context.Context, orderID string) (*Order, error)
|
||||
// GetItem 读取订单关联商品信息。
|
||||
GetItem(ctx context.Context, cookieID, itemID string) (*ItemInfo, error)
|
||||
}
|
||||
|
||||
// DetailService 承载订单详情读取和所有权规则,不依赖 HTTP 或数据库实现。
|
||||
type DetailService struct {
|
||||
// repository 保存订单详情用例所需的窄数据访问 Port。
|
||||
repository DetailRepository
|
||||
}
|
||||
|
||||
// NewDetailService 创建订单详情应用服务。
|
||||
func NewDetailService(repository DetailRepository) *DetailService {
|
||||
return &DetailService{repository: repository}
|
||||
}
|
||||
|
||||
// Get 读取订单并校验订单绑定账号属于当前用户。
|
||||
func (s *DetailService) Get(ctx context.Context, userID int64, orderID string) (*Order, error) {
|
||||
if s == nil || s.repository == nil {
|
||||
return nil, errors.New("订单详情 repository 未初始化")
|
||||
}
|
||||
// order、err 保存订单读取结果及错误。
|
||||
order, err := s.repository.GetOrder(ctx, orderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if order == nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if strings.TrimSpace(order.CookieID) == "" {
|
||||
return nil, ErrForbidden
|
||||
}
|
||||
// owned、ownershipErr 保存订单账号归属结果及错误。
|
||||
owned, ownershipErr := s.repository.ExistsOwned(ctx, userID, order.CookieID)
|
||||
if ownershipErr != nil {
|
||||
return nil, ownershipErr
|
||||
}
|
||||
if !owned {
|
||||
return nil, ErrForbidden
|
||||
}
|
||||
return order, nil
|
||||
}
|
||||
|
||||
// GetView 读取订单并补全关联商品信息,商品读取失败时保留订单主体。
|
||||
func (s *DetailService) GetView(ctx context.Context, userID int64, orderID string) (DetailResult, error) {
|
||||
// order、err 保存经过所有权校验的订单及错误。
|
||||
order, err := s.Get(ctx, userID, orderID)
|
||||
if err != nil {
|
||||
return DetailResult{}, err
|
||||
}
|
||||
// item、itemErr 保存关联商品及读取错误。
|
||||
item, itemErr := s.repository.GetItem(ctx, order.CookieID, order.ItemID)
|
||||
if itemErr != nil {
|
||||
item = nil
|
||||
}
|
||||
return DetailResult{Order: order, Item: item}, nil
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// detailRepositoryStub 是订单详情服务测试使用的最小数据访问替身。
|
||||
type detailRepositoryStub struct {
|
||||
// order 保存待返回的订单实体。
|
||||
order *Order
|
||||
// orderErr 保存订单读取错误。
|
||||
orderErr error
|
||||
// owned 表示账号是否归属于当前用户。
|
||||
owned bool
|
||||
// ownershipErr 保存账号归属查询错误。
|
||||
ownershipErr error
|
||||
// item 保存待返回的商品信息。
|
||||
item *ItemInfo
|
||||
// itemErr 保存商品读取错误。
|
||||
itemErr error
|
||||
}
|
||||
|
||||
// ExistsOwned 返回测试替身预设的账号归属结果。
|
||||
func (s *detailRepositoryStub) ExistsOwned(context.Context, int64, string) (bool, error) {
|
||||
return s.owned, s.ownershipErr
|
||||
}
|
||||
|
||||
// GetOrder 返回测试替身预设的订单结果。
|
||||
func (s *detailRepositoryStub) GetOrder(context.Context, string) (*Order, error) {
|
||||
return s.order, s.orderErr
|
||||
}
|
||||
|
||||
// GetItem 返回测试替身预设的商品结果。
|
||||
func (s *detailRepositoryStub) GetItem(context.Context, string, string) (*ItemInfo, error) {
|
||||
return s.item, s.itemErr
|
||||
}
|
||||
|
||||
// TestDetailServiceChecksOwnership 验证订单详情服务阻止跨用户读取。
|
||||
func TestDetailServiceChecksOwnership(t *testing.T) {
|
||||
// service 保存不拥有目标账号的详情服务。
|
||||
service := NewDetailService(&detailRepositoryStub{order: &Order{CookieID: "other"}, owned: false})
|
||||
// err 保存详情读取返回的错误。
|
||||
_, err := service.Get(context.Background(), 7, "order-1")
|
||||
if !errors.Is(err, ErrForbidden) {
|
||||
t.Fatalf("error=%v, want ErrForbidden", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDetailServiceReturnsNotFound 验证订单为空时返回稳定的不存在错误。
|
||||
func TestDetailServiceReturnsNotFound(t *testing.T) {
|
||||
// service 保存返回空订单的详情服务。
|
||||
service := NewDetailService(&detailRepositoryStub{owned: true})
|
||||
// err 保存详情读取返回的错误。
|
||||
_, err := service.Get(context.Background(), 7, "missing")
|
||||
if !errors.Is(err, ErrNotFound) {
|
||||
t.Fatalf("error=%v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDetailServiceKeepsOrderWhenItemLookupFails 验证商品读取失败不丢失订单主体。
|
||||
func TestDetailServiceKeepsOrderWhenItemLookupFails(t *testing.T) {
|
||||
// expected 保存详情服务应返回的订单主体。
|
||||
expected := &Order{OrderID: "order-1", CookieID: "cookie-1", ItemID: "item-1"}
|
||||
// service 保存商品读取失败的详情服务。
|
||||
service := NewDetailService(&detailRepositoryStub{order: expected, owned: true, itemErr: errors.New("商品暂不可用")})
|
||||
// result、err 保存详情查询结果及错误。
|
||||
result, err := service.GetView(context.Background(), 7, expected.OrderID)
|
||||
if err != nil || result.Order != expected || result.Item != nil {
|
||||
t.Fatalf("result=%+v err=%v", result, err)
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func newApplicationServices(server *Server) *applicationServices {
|
||||
// orderRepository 保存订单应用服务共享的基础设施适配器。
|
||||
orderRepository := newStoreOrderRepository(server.Store)
|
||||
return &applicationServices{
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: orderRepository, list: orderapp.NewListService(orderRepository), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: orderRepository, list: orderapp.NewListService(orderRepository), detail: orderapp.NewDetailService(orderRepository), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
itemPublish: &itemPublishService{server: server, repository: newStoreItemPublishRepository(server.Store)},
|
||||
accountLogin: &accountLoginService{server: server, repository: newStoreAccountLoginRepository(server.Store)},
|
||||
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
|
||||
|
||||
@@ -148,6 +148,8 @@ type orderApplicationService struct {
|
||||
repository orderapp.Repository
|
||||
// list 负责订单列表分页和账号所有权规则。
|
||||
list *orderapp.ListService
|
||||
// detail 负责订单详情读取、商品补全和账号所有权规则。
|
||||
detail *orderapp.DetailService
|
||||
// refreshJobs 提供订单刷新后台任务的持久化 Port。
|
||||
refreshJobs orderapp.RefreshJobRepository
|
||||
}
|
||||
@@ -349,36 +351,31 @@ func (a *orderApplicationService) List(ctx context.Context, query orderListQuery
|
||||
|
||||
// Get 查询订单并校验订单绑定账号属于当前用户。
|
||||
func (a *orderApplicationService) Get(ctx context.Context, userID int64, orderID string) (*orderapp.Order, error) {
|
||||
// order、err 保存order、err,供当前处理流程使用
|
||||
order, err := a.repository.GetOrder(ctx, orderID)
|
||||
if err != nil || order == nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// order、err 保存应用层订单详情结果及错误。
|
||||
order, err := a.detail.Get(ctx, userID, orderID)
|
||||
if errors.Is(err, orderapp.ErrNotFound) {
|
||||
return nil, db.ErrNotFound
|
||||
}
|
||||
if strings.TrimSpace(order.CookieID) == "" {
|
||||
if errors.Is(err, orderapp.ErrForbidden) {
|
||||
return nil, db.ErrForbidden
|
||||
}
|
||||
if !a.orderOwnedByUser(ctx, userID, order.CookieID) {
|
||||
return nil, db.ErrForbidden
|
||||
}
|
||||
return order, nil
|
||||
return order, err
|
||||
}
|
||||
|
||||
// GetView 查询订单并补全商品标题和主图,供详情 handler 直接编码。
|
||||
func (a *orderApplicationService) GetView(ctx context.Context, userID int64, orderID string) (orderDetailResult, error) {
|
||||
// order、err 保存order、err,供当前处理流程使用
|
||||
order, err := a.Get(ctx, userID, orderID)
|
||||
// result、err 保存应用层详情结果及错误。
|
||||
result, err := a.detail.GetView(ctx, userID, orderID)
|
||||
if errors.Is(err, orderapp.ErrNotFound) {
|
||||
return orderDetailResult{}, db.ErrNotFound
|
||||
}
|
||||
if errors.Is(err, orderapp.ErrForbidden) {
|
||||
return orderDetailResult{}, db.ErrForbidden
|
||||
}
|
||||
if err != nil {
|
||||
return orderDetailResult{}, err
|
||||
}
|
||||
// item、itemErr 保存item、itemErr,供当前处理流程使用
|
||||
item, itemErr := a.repository.GetItem(ctx, order.CookieID, order.ItemID)
|
||||
if itemErr != nil {
|
||||
item = nil
|
||||
}
|
||||
return orderDetailResult{Order: orderDTOFromOrder(order, item)}, nil
|
||||
return orderDetailResult{Order: orderDTOFromOrder(result.Order, result.Item)}, nil
|
||||
}
|
||||
|
||||
// Delete 逻辑删除订单,保留历史记录供审计使用。
|
||||
|
||||
Reference in new issue
Block a user