From 31cde6c5558c52af714b3473569a96e40899edf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E9=A2=9C?= Date: Mon, 19 Jan 2026 19:35:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(subscriptions):=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=AE=A2=E9=98=85=E4=B8=8D=E8=BF=94=E5=9B=9E=E5=88=86=E9=85=8D?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用户侧 UserSubscription DTO 移除 assigned_by/assigned_at/notes/assigned_by_user 等管理员字段\n- 新增 AdminUserSubscription,并调整管理员订阅接口与批量分配结果使用\n- 增加 /api/v1/subscriptions 契约测试,确保用户侧响应不包含上述字段 --- .../handler/admin/subscription_handler.go | 18 +++--- backend/internal/handler/dto/mappers.go | 30 +++++++--- backend/internal/handler/dto/types.go | 21 ++++--- backend/internal/server/api_contract_test.go | 57 +++++++++++++++++++ 4 files changed, 103 insertions(+), 23 deletions(-) diff --git a/backend/internal/handler/admin/subscription_handler.go b/backend/internal/handler/admin/subscription_handler.go index 08db999a..3cdc8368 100644 --- a/backend/internal/handler/admin/subscription_handler.go +++ b/backend/internal/handler/admin/subscription_handler.go @@ -83,9 +83,9 @@ func (h *SubscriptionHandler) List(c *gin.Context) { return } - out := make([]dto.UserSubscription, 0, len(subscriptions)) + out := make([]dto.AdminUserSubscription, 0, len(subscriptions)) for i := range subscriptions { - out = append(out, *dto.UserSubscriptionFromService(&subscriptions[i])) + out = append(out, *dto.UserSubscriptionFromServiceAdmin(&subscriptions[i])) } response.PaginatedWithResult(c, out, toResponsePagination(pagination)) } @@ -105,7 +105,7 @@ func (h *SubscriptionHandler) GetByID(c *gin.Context) { return } - response.Success(c, dto.UserSubscriptionFromService(subscription)) + response.Success(c, dto.UserSubscriptionFromServiceAdmin(subscription)) } // GetProgress handles getting subscription usage progress @@ -150,7 +150,7 @@ func (h *SubscriptionHandler) Assign(c *gin.Context) { return } - response.Success(c, dto.UserSubscriptionFromService(subscription)) + response.Success(c, dto.UserSubscriptionFromServiceAdmin(subscription)) } // BulkAssign handles bulk assigning subscriptions to multiple users @@ -201,7 +201,7 @@ func (h *SubscriptionHandler) Extend(c *gin.Context) { return } - response.Success(c, dto.UserSubscriptionFromService(subscription)) + response.Success(c, dto.UserSubscriptionFromServiceAdmin(subscription)) } // Revoke handles revoking a subscription @@ -239,9 +239,9 @@ func (h *SubscriptionHandler) ListByGroup(c *gin.Context) { return } - out := make([]dto.UserSubscription, 0, len(subscriptions)) + out := make([]dto.AdminUserSubscription, 0, len(subscriptions)) for i := range subscriptions { - out = append(out, *dto.UserSubscriptionFromService(&subscriptions[i])) + out = append(out, *dto.UserSubscriptionFromServiceAdmin(&subscriptions[i])) } response.PaginatedWithResult(c, out, toResponsePagination(pagination)) } @@ -261,9 +261,9 @@ func (h *SubscriptionHandler) ListByUser(c *gin.Context) { return } - out := make([]dto.UserSubscription, 0, len(subscriptions)) + out := make([]dto.AdminUserSubscription, 0, len(subscriptions)) for i := range subscriptions { - out = append(out, *dto.UserSubscriptionFromService(&subscriptions[i])) + out = append(out, *dto.UserSubscriptionFromServiceAdmin(&subscriptions[i])) } response.Success(c, out) } diff --git a/backend/internal/handler/dto/mappers.go b/backend/internal/handler/dto/mappers.go index e768b188..34cc26d4 100644 --- a/backend/internal/handler/dto/mappers.go +++ b/backend/internal/handler/dto/mappers.go @@ -442,7 +442,27 @@ func UserSubscriptionFromService(sub *service.UserSubscription) *UserSubscriptio if sub == nil { return nil } - return &UserSubscription{ + out := userSubscriptionFromServiceBase(sub) + return &out +} + +// UserSubscriptionFromServiceAdmin converts a service UserSubscription to DTO for admin users. +// It includes assignment metadata and notes. +func UserSubscriptionFromServiceAdmin(sub *service.UserSubscription) *AdminUserSubscription { + if sub == nil { + return nil + } + return &AdminUserSubscription{ + UserSubscription: userSubscriptionFromServiceBase(sub), + AssignedBy: sub.AssignedBy, + AssignedAt: sub.AssignedAt, + Notes: sub.Notes, + AssignedByUser: UserFromServiceShallow(sub.AssignedByUser), + } +} + +func userSubscriptionFromServiceBase(sub *service.UserSubscription) UserSubscription { + return UserSubscription{ ID: sub.ID, UserID: sub.UserID, GroupID: sub.GroupID, @@ -455,14 +475,10 @@ func UserSubscriptionFromService(sub *service.UserSubscription) *UserSubscriptio DailyUsageUSD: sub.DailyUsageUSD, WeeklyUsageUSD: sub.WeeklyUsageUSD, MonthlyUsageUSD: sub.MonthlyUsageUSD, - AssignedBy: sub.AssignedBy, - AssignedAt: sub.AssignedAt, - Notes: sub.Notes, CreatedAt: sub.CreatedAt, UpdatedAt: sub.UpdatedAt, User: UserFromServiceShallow(sub.User), Group: GroupFromServiceShallow(sub.Group), - AssignedByUser: UserFromServiceShallow(sub.AssignedByUser), } } @@ -470,9 +486,9 @@ func BulkAssignResultFromService(r *service.BulkAssignResult) *BulkAssignResult if r == nil { return nil } - subs := make([]UserSubscription, 0, len(r.Subscriptions)) + subs := make([]AdminUserSubscription, 0, len(r.Subscriptions)) for i := range r.Subscriptions { - subs = append(subs, *UserSubscriptionFromService(&r.Subscriptions[i])) + subs = append(subs, *UserSubscriptionFromServiceAdmin(&r.Subscriptions[i])) } return &BulkAssignResult{ SuccessCount: r.SuccessCount, diff --git a/backend/internal/handler/dto/types.go b/backend/internal/handler/dto/types.go index 649cc036..c55c8e74 100644 --- a/backend/internal/handler/dto/types.go +++ b/backend/internal/handler/dto/types.go @@ -323,22 +323,29 @@ type UserSubscription struct { WeeklyUsageUSD float64 `json:"weekly_usage_usd"` MonthlyUsageUSD float64 `json:"monthly_usage_usd"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + + User *User `json:"user,omitempty"` + Group *Group `json:"group,omitempty"` +} + +// AdminUserSubscription 是管理员接口使用的订阅 DTO(包含分配信息/备注等字段)。 +// 注意:普通用户接口不得返回 assigned_by/assigned_at/notes/assigned_by_user 等管理员字段。 +type AdminUserSubscription struct { + UserSubscription + AssignedBy *int64 `json:"assigned_by"` AssignedAt time.Time `json:"assigned_at"` Notes string `json:"notes"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - - User *User `json:"user,omitempty"` - Group *Group `json:"group,omitempty"` - AssignedByUser *User `json:"assigned_by_user,omitempty"` + AssignedByUser *User `json:"assigned_by_user,omitempty"` } type BulkAssignResult struct { SuccessCount int `json:"success_count"` FailedCount int `json:"failed_count"` - Subscriptions []UserSubscription `json:"subscriptions"` + Subscriptions []AdminUserSubscription `json:"subscriptions"` Errors []string `json:"errors"` } diff --git a/backend/internal/server/api_contract_test.go b/backend/internal/server/api_contract_test.go index c4cbc038..9a2333d9 100644 --- a/backend/internal/server/api_contract_test.go +++ b/backend/internal/server/api_contract_test.go @@ -186,6 +186,56 @@ func TestAPIContracts(t *testing.T) { ] }`, }, + { + name: "GET /api/v1/subscriptions", + setup: func(t *testing.T, deps *contractDeps) { + t.Helper() + // 普通用户订阅接口不应包含 assigned_* / notes 等管理员字段。 + deps.userSubRepo.SetByUserID(1, []service.UserSubscription{ + { + ID: 501, + UserID: 1, + GroupID: 10, + StartsAt: deps.now, + ExpiresAt: deps.now.Add(24 * time.Hour), + Status: service.SubscriptionStatusActive, + DailyUsageUSD: 1.23, + WeeklyUsageUSD: 2.34, + MonthlyUsageUSD: 3.45, + AssignedBy: ptr(int64(999)), + AssignedAt: deps.now, + Notes: "admin-note", + CreatedAt: deps.now, + UpdatedAt: deps.now, + }, + }) + }, + method: http.MethodGet, + path: "/api/v1/subscriptions", + wantStatus: http.StatusOK, + wantJSON: `{ + "code": 0, + "message": "success", + "data": [ + { + "id": 501, + "user_id": 1, + "group_id": 10, + "starts_at": "2025-01-02T03:04:05Z", + "expires_at": "2025-01-03T03:04:05Z", + "status": "active", + "daily_window_start": null, + "weekly_window_start": null, + "monthly_window_start": null, + "daily_usage_usd": 1.23, + "weekly_usage_usd": 2.34, + "monthly_usage_usd": 3.45, + "created_at": "2025-01-02T03:04:05Z", + "updated_at": "2025-01-02T03:04:05Z" + } + ] + }`, + }, { name: "GET /api/v1/usage/stats", setup: func(t *testing.T, deps *contractDeps) { @@ -490,6 +540,9 @@ func newContractDeps(t *testing.T) *contractDeps { usageRepo := newStubUsageLogRepo() usageService := service.NewUsageService(usageRepo, userRepo, nil, nil) + subscriptionService := service.NewSubscriptionService(groupRepo, userSubRepo, nil) + subscriptionHandler := handler.NewSubscriptionHandler(subscriptionService) + settingRepo := newStubSettingRepo() settingService := service.NewSettingService(settingRepo, cfg) @@ -536,6 +589,10 @@ func newContractDeps(t *testing.T) *contractDeps { v1Usage.GET("/usage", usageHandler.List) v1Usage.GET("/usage/stats", usageHandler.Stats) + v1Subs := v1.Group("") + v1Subs.Use(jwtAuth) + v1Subs.GET("/subscriptions", subscriptionHandler.List) + v1Admin := v1.Group("/admin") v1Admin.Use(adminAuth) v1Admin.GET("/settings", adminSettingHandler.GetSettings)