This commit is contained in:
talksik
2026-04-14 14:55:53 -07:00
parent 5b8a702c4c
commit 9d6d3bfb3c
+5 -27
View File
@@ -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)