From 9d6d3bfb3c8a73e75f2f65ace4b2909a09e4ea56 Mon Sep 17 00:00:00 2001 From: talksik Date: Tue, 14 Apr 2026 14:55:53 -0700 Subject: [PATCH] refactor --- go/internal/billing/service.go | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/go/internal/billing/service.go b/go/internal/billing/service.go index ca5a588..0e51a85 100644 --- a/go/internal/billing/service.go +++ b/go/internal/billing/service.go @@ -27,9 +27,6 @@ type Service interface { // GetUsage reports today's freemium quota state for a network. // Pro networks get Limit=nil (unlimited); free networks get Limit=&FreemiumDailyLimit. GetUsage(ctx context.Context, networkID string) (*Usage, error) - // IncrementDailyUsage is called by the particle processor worker for each - // qualifying particle (non-container). Idempotency is the caller's concern - // — the worker guards this via processed_particles. IncrementDailyUsage(ctx context.Context, networkID string, at time.Time) error } @@ -240,43 +237,24 @@ func (s *serviceImpl) GetUsage(ctx context.Context, networkID string) (*Usage, e return nil, fmt.Errorf("read daily usage: %w", err) } - plan, err := s.resolvePlan(ctx, networkID) + sub, err := s.GetStatus(ctx, networkID) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to get network billing status: %w", err) } u := &Usage{ - Plan: plan, + Plan: sub.Plan, Used: used, ResetAt: nextUTCMidnight(now), + Limit: nil, } - if plan == PlanFree { + if sub.Plan == PlanFree { limit := FreemiumDailyLimit u.Limit = &limit } return u, nil } -// resolvePlan is a lightweight read: it infers free/pro from the local -// subscription row without hitting Stripe, so it is safe to call from the -// particle processor worker (no Stripe client required). -func (s *serviceImpl) resolvePlan(ctx context.Context, networkID string) (Plan, error) { - sub, err := s.repo.getSubscriptionByNetworkID(ctx, networkID) - if errors.Is(err, errNotFound) { - return PlanFree, nil - } - if err != nil { - return "", err - } - switch stripe.SubscriptionStatus(sub.Status) { - case stripe.SubscriptionStatusActive, - stripe.SubscriptionStatusTrialing, - stripe.SubscriptionStatusPastDue: - return PlanPro, nil - } - return PlanFree, nil -} - func nextUTCMidnight(now time.Time) time.Time { utc := now.UTC() return time.Date(utc.Year(), utc.Month(), utc.Day(), 0, 0, 0, 0, time.UTC).Add(24 * time.Hour)