mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-17 14:57:27 +00:00
Merge branch 'Wei-Shaw:main' into main
This commit is contained in:
36 files changed
+460
-92
No files matched your search
@@ -575,7 +575,11 @@ func (r *accountRepository) ListWithFilters(ctx context.Context, params paginati
|
||||
}))
|
||||
}
|
||||
|
||||
total, err := q.Count(ctx)
|
||||
// Clone before Count so interceptor-appended predicates (SoftDeleteMixin's
|
||||
// deleted_at IS NULL) don't accumulate on the shared builder and pollute the
|
||||
// subsequent list query. Same pattern used in group_repo/promo_code_repo/user_repo
|
||||
// (P1-03 audit fix, commit 2588fa6a).
|
||||
total, err := q.Clone().Count(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -445,9 +445,14 @@ func (s *AccountRepoSuite) TestListWithFilters() {
|
||||
|
||||
tt.setup(client)
|
||||
|
||||
accounts, _, err := repo.ListWithFilters(ctx, pagination.PaginationParams{Page: 1, PageSize: 10}, tt.platform, tt.accType, tt.status, tt.search, tt.groupID, tt.privacyMode)
|
||||
accounts, page, err := repo.ListWithFilters(ctx, pagination.PaginationParams{Page: 1, PageSize: 10}, tt.platform, tt.accType, tt.status, tt.search, tt.groupID, tt.privacyMode)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(accounts, tt.wantCount)
|
||||
// Regression guard for issue #3601: when the whole result set fits on a single page,
|
||||
// pagination.Total must match len(items). A mismatch means the Count query was applied
|
||||
// against different predicates than the list query — the exact symptom reported.
|
||||
s.Require().NotNil(page)
|
||||
s.Require().Equal(int64(tt.wantCount), page.Total, "total must match items on single page")
|
||||
if tt.validate != nil {
|
||||
tt.validate(accounts)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
billingBalanceKeyPrefix = "billing:balance:"
|
||||
billingSubKeyPrefix = "billing:sub:"
|
||||
billingRateLimitKeyPrefix = "apikey:rate:"
|
||||
subCacheInvalidateChannel = "subscription:cache:invalidate"
|
||||
billingCacheTTL = 5 * time.Minute
|
||||
billingCacheJitter = 30 * time.Second
|
||||
rateLimitCacheTTL = 7 * 24 * time.Hour // 7 days matches the longest window
|
||||
@@ -256,6 +257,45 @@ func (c *billingCache) InvalidateSubscriptionCache(ctx context.Context, userID,
|
||||
return c.rdb.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (c *billingCache) PublishSubscriptionCacheInvalidation(ctx context.Context, cacheKey string) error {
|
||||
return c.rdb.Publish(ctx, subCacheInvalidateChannel, cacheKey).Err()
|
||||
}
|
||||
|
||||
func (c *billingCache) SubscribeSubscriptionCacheInvalidation(ctx context.Context, handler func(cacheKey string)) error {
|
||||
pubsub := c.rdb.Subscribe(ctx, subCacheInvalidateChannel)
|
||||
|
||||
_, err := pubsub.Receive(ctx)
|
||||
if err != nil {
|
||||
_ = pubsub.Close()
|
||||
return fmt.Errorf("subscribe to subscription cache invalidation: %w", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
if err := pubsub.Close(); err != nil {
|
||||
log.Printf("Warning: failed to close subscription cache invalidation pubsub: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
ch := pubsub.Channel()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case msg, ok := <-ch:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if msg != nil {
|
||||
handler(msg.Payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *billingCache) GetAPIKeyRateLimit(ctx context.Context, keyID int64) (*service.APIKeyRateLimitCacheData, error) {
|
||||
key := billingRateLimitKey(keyID)
|
||||
result, err := c.rdb.HGetAll(ctx, key).Result()
|
||||
|
||||
@@ -6,6 +6,9 @@ import (
|
||||
|
||||
dbent "github.com/Wei-Shaw/sub2api/ent"
|
||||
"github.com/Wei-Shaw/sub2api/ent/group"
|
||||
"github.com/Wei-Shaw/sub2api/ent/predicate"
|
||||
"github.com/Wei-Shaw/sub2api/ent/schema/mixins"
|
||||
"github.com/Wei-Shaw/sub2api/ent/user"
|
||||
"github.com/Wei-Shaw/sub2api/ent/usersubscription"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
@@ -194,6 +197,7 @@ func (r *userSubscriptionRepository) ListByGroupID(ctx context.Context, groupID
|
||||
func (r *userSubscriptionRepository) List(ctx context.Context, params pagination.PaginationParams, userID, groupID *int64, status, platform, sortBy, sortOrder string) ([]service.UserSubscription, *pagination.PaginationResult, error) {
|
||||
client := clientFromContext(ctx, r.client)
|
||||
q := client.UserSubscription.Query()
|
||||
includeSoftDeleted := status == "" || status == service.SubscriptionStatusRevoked
|
||||
if userID != nil {
|
||||
q = q.Where(usersubscription.UserIDEQ(*userID))
|
||||
}
|
||||
@@ -201,7 +205,11 @@ func (r *userSubscriptionRepository) List(ctx context.Context, params pagination
|
||||
q = q.Where(usersubscription.GroupIDEQ(*groupID))
|
||||
}
|
||||
if platform != "" {
|
||||
q = q.Where(usersubscription.HasGroupWith(group.PlatformEQ(platform)))
|
||||
groupPredicates := []predicate.Group{group.PlatformEQ(platform)}
|
||||
if includeSoftDeleted {
|
||||
groupPredicates = append(groupPredicates, group.DeletedAtIsNil())
|
||||
}
|
||||
q = q.Where(usersubscription.HasGroupWith(groupPredicates...))
|
||||
}
|
||||
|
||||
// Status filtering with real-time expiration check
|
||||
@@ -224,20 +232,29 @@ func (r *userSubscriptionRepository) List(ctx context.Context, params pagination
|
||||
),
|
||||
),
|
||||
)
|
||||
case service.SubscriptionStatusRevoked:
|
||||
// Revoked is a DTO/API display state backed by user_subscriptions.deleted_at.
|
||||
q = q.Where(usersubscription.DeletedAtNotNil())
|
||||
case "":
|
||||
// No filter
|
||||
// No filter. Use SkipSoftDelete below so admin "all status" includes revoked history.
|
||||
default:
|
||||
// Other status (e.g., revoked)
|
||||
// Other persisted status.
|
||||
q = q.Where(usersubscription.StatusEQ(status))
|
||||
}
|
||||
|
||||
total, err := q.Clone().Count(ctx)
|
||||
queryCtx := ctx
|
||||
if includeSoftDeleted {
|
||||
queryCtx = mixins.SkipSoftDelete(ctx)
|
||||
}
|
||||
|
||||
total, err := q.Clone().Count(queryCtx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Apply sorting
|
||||
q = q.WithUser().WithGroup().WithAssignedByUser()
|
||||
if !includeSoftDeleted {
|
||||
q = q.WithUser().WithGroup().WithAssignedByUser()
|
||||
}
|
||||
|
||||
// Determine sort field
|
||||
var field string
|
||||
@@ -260,12 +277,19 @@ func (r *userSubscriptionRepository) List(ctx context.Context, params pagination
|
||||
subs, err := q.
|
||||
Offset(params.Offset()).
|
||||
Limit(params.Limit()).
|
||||
All(ctx)
|
||||
All(queryCtx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return userSubscriptionEntitiesToService(subs), paginationResultFromTotal(int64(total), params), nil
|
||||
result := userSubscriptionEntitiesToService(subs)
|
||||
if includeSoftDeleted {
|
||||
if err := r.attachUserSubscriptionRelations(ctx, result); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return result, paginationResultFromTotal(int64(total), params), nil
|
||||
}
|
||||
|
||||
func (r *userSubscriptionRepository) ExistsByUserIDAndGroupID(ctx context.Context, userID, groupID int64) (bool, error) {
|
||||
@@ -425,17 +449,91 @@ func (r *userSubscriptionRepository) DeleteByGroupID(ctx context.Context, groupI
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func (r *userSubscriptionRepository) attachUserSubscriptionRelations(ctx context.Context, subs []service.UserSubscription) error {
|
||||
if len(subs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
userIDs := make([]int64, 0, len(subs))
|
||||
groupIDs := make([]int64, 0, len(subs))
|
||||
assignedByIDs := make([]int64, 0, len(subs))
|
||||
for i := range subs {
|
||||
userIDs = append(userIDs, subs[i].UserID)
|
||||
groupIDs = append(groupIDs, subs[i].GroupID)
|
||||
if subs[i].AssignedBy != nil {
|
||||
assignedByIDs = append(assignedByIDs, *subs[i].AssignedBy)
|
||||
}
|
||||
}
|
||||
|
||||
client := clientFromContext(ctx, r.client)
|
||||
users, err := client.User.Query().Where(user.IDIn(uniqueInt64s(userIDs)...)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userByID := make(map[int64]*service.User, len(users))
|
||||
for _, u := range users {
|
||||
userByID[u.ID] = userEntityToService(u)
|
||||
}
|
||||
|
||||
groups, err := client.Group.Query().Where(group.IDIn(uniqueInt64s(groupIDs)...)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
groupByID := make(map[int64]*service.Group, len(groups))
|
||||
for _, g := range groups {
|
||||
groupByID[g.ID] = groupEntityToService(g)
|
||||
}
|
||||
|
||||
assignedByID := map[int64]*service.User{}
|
||||
if len(assignedByIDs) > 0 {
|
||||
assignedUsers, err := client.User.Query().Where(user.IDIn(uniqueInt64s(assignedByIDs)...)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
assignedByID = make(map[int64]*service.User, len(assignedUsers))
|
||||
for _, u := range assignedUsers {
|
||||
assignedByID[u.ID] = userEntityToService(u)
|
||||
}
|
||||
}
|
||||
|
||||
for i := range subs {
|
||||
subs[i].User = userByID[subs[i].UserID]
|
||||
subs[i].Group = groupByID[subs[i].GroupID]
|
||||
if subs[i].AssignedBy != nil {
|
||||
subs[i].AssignedByUser = assignedByID[*subs[i].AssignedBy]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func uniqueInt64s(values []int64) []int64 {
|
||||
seen := make(map[int64]struct{}, len(values))
|
||||
out := make([]int64, 0, len(values))
|
||||
for _, v := range values {
|
||||
if _, ok := seen[v]; ok {
|
||||
continue
|
||||
}
|
||||
seen[v] = struct{}{}
|
||||
out = append(out, v)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func userSubscriptionEntityToService(m *dbent.UserSubscription) *service.UserSubscription {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
status := m.Status
|
||||
if m.DeletedAt != nil {
|
||||
status = service.SubscriptionStatusRevoked
|
||||
}
|
||||
out := &service.UserSubscription{
|
||||
ID: m.ID,
|
||||
UserID: m.UserID,
|
||||
GroupID: m.GroupID,
|
||||
StartsAt: m.StartsAt,
|
||||
ExpiresAt: m.ExpiresAt,
|
||||
Status: m.Status,
|
||||
Status: status,
|
||||
DailyWindowStart: m.DailyWindowStart,
|
||||
WeeklyWindowStart: m.WeeklyWindowStart,
|
||||
MonthlyWindowStart: m.MonthlyWindowStart,
|
||||
@@ -447,6 +545,7 @@ func userSubscriptionEntityToService(m *dbent.UserSubscription) *service.UserSub
|
||||
Notes: derefString(m.Notes),
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
DeletedAt: m.DeletedAt,
|
||||
}
|
||||
if m.Edges.User != nil {
|
||||
out.User = userEntityToService(m.Edges.User)
|
||||
|
||||
@@ -326,6 +326,61 @@ func (s *UserSubscriptionRepoSuite) TestList_FilterByStatus() {
|
||||
s.Require().Equal(service.SubscriptionStatusExpired, subs[0].Status)
|
||||
}
|
||||
|
||||
func (s *UserSubscriptionRepoSuite) TestList_IncludesRevokedWhenStatusEmpty() {
|
||||
user1 := s.mustCreateUser("allstatus1@test.com", service.RoleUser)
|
||||
user2 := s.mustCreateUser("allstatus2@test.com", service.RoleUser)
|
||||
user3 := s.mustCreateUser("allstatus3@test.com", service.RoleUser)
|
||||
group1 := s.mustCreateGroup("g-allstatus-1")
|
||||
group2 := s.mustCreateGroup("g-allstatus-2")
|
||||
group3 := s.mustCreateGroup("g-allstatus-3")
|
||||
|
||||
s.mustCreateSubscription(user1.ID, group1.ID, nil)
|
||||
s.mustCreateSubscription(user2.ID, group2.ID, func(c *dbent.UserSubscriptionCreate) {
|
||||
c.SetStatus(service.SubscriptionStatusExpired)
|
||||
c.SetExpiresAt(time.Now().Add(-24 * time.Hour))
|
||||
})
|
||||
revoked := s.mustCreateSubscription(user3.ID, group3.ID, nil)
|
||||
s.Require().NoError(s.repo.Delete(s.ctx, revoked.ID))
|
||||
|
||||
subs, pag, err := s.repo.List(s.ctx, pagination.PaginationParams{Page: 1, PageSize: 10}, nil, nil, "", "", "", "")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(subs, 3)
|
||||
s.Require().Equal(int64(3), pag.Total)
|
||||
|
||||
var gotRevoked *service.UserSubscription
|
||||
for i := range subs {
|
||||
if subs[i].ID == revoked.ID {
|
||||
gotRevoked = &subs[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
s.Require().NotNil(gotRevoked, "all status should include soft-deleted subscription")
|
||||
s.Require().Equal(service.SubscriptionStatusRevoked, gotRevoked.Status)
|
||||
s.Require().NotNil(gotRevoked.DeletedAt)
|
||||
s.Require().NotNil(gotRevoked.User)
|
||||
s.Require().NotNil(gotRevoked.Group)
|
||||
}
|
||||
|
||||
func (s *UserSubscriptionRepoSuite) TestList_FilterByRevokedStatus() {
|
||||
user1 := s.mustCreateUser("revokedfilter1@test.com", service.RoleUser)
|
||||
user2 := s.mustCreateUser("revokedfilter2@test.com", service.RoleUser)
|
||||
group1 := s.mustCreateGroup("g-revoked-1")
|
||||
group2 := s.mustCreateGroup("g-revoked-2")
|
||||
|
||||
active := s.mustCreateSubscription(user1.ID, group1.ID, nil)
|
||||
revoked := s.mustCreateSubscription(user2.ID, group2.ID, nil)
|
||||
s.Require().NoError(s.repo.Delete(s.ctx, revoked.ID))
|
||||
|
||||
subs, pag, err := s.repo.List(s.ctx, pagination.PaginationParams{Page: 1, PageSize: 10}, nil, nil, service.SubscriptionStatusRevoked, "", "", "")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(subs, 1)
|
||||
s.Require().Equal(int64(1), pag.Total)
|
||||
s.Require().Equal(revoked.ID, subs[0].ID)
|
||||
s.Require().NotEqual(active.ID, subs[0].ID)
|
||||
s.Require().Equal(service.SubscriptionStatusRevoked, subs[0].Status)
|
||||
s.Require().NotNil(subs[0].DeletedAt)
|
||||
}
|
||||
|
||||
// --- Usage tracking ---
|
||||
|
||||
func (s *UserSubscriptionRepoSuite) TestIncrementUsage() {
|
||||
|
||||
Reference in new issue
Block a user