refactor
This commit is contained in:
@@ -27,9 +27,6 @@ type Service interface {
|
|||||||
// GetUsage reports today's freemium quota state for a network.
|
// GetUsage reports today's freemium quota state for a network.
|
||||||
// Pro networks get Limit=nil (unlimited); free networks get Limit=&FreemiumDailyLimit.
|
// Pro networks get Limit=nil (unlimited); free networks get Limit=&FreemiumDailyLimit.
|
||||||
GetUsage(ctx context.Context, networkID string) (*Usage, error)
|
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
|
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)
|
return nil, fmt.Errorf("read daily usage: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
plan, err := s.resolvePlan(ctx, networkID)
|
sub, err := s.GetStatus(ctx, networkID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("unable to get network billing status: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
u := &Usage{
|
u := &Usage{
|
||||||
Plan: plan,
|
Plan: sub.Plan,
|
||||||
Used: used,
|
Used: used,
|
||||||
ResetAt: nextUTCMidnight(now),
|
ResetAt: nextUTCMidnight(now),
|
||||||
|
Limit: nil,
|
||||||
}
|
}
|
||||||
if plan == PlanFree {
|
if sub.Plan == PlanFree {
|
||||||
limit := FreemiumDailyLimit
|
limit := FreemiumDailyLimit
|
||||||
u.Limit = &limit
|
u.Limit = &limit
|
||||||
}
|
}
|
||||||
return u, nil
|
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 {
|
func nextUTCMidnight(now time.Time) time.Time {
|
||||||
utc := now.UTC()
|
utc := now.UTC()
|
||||||
return time.Date(utc.Year(), utc.Month(), utc.Day(), 0, 0, 0, 0, time.UTC).Add(24 * time.Hour)
|
return time.Date(utc.Year(), utc.Month(), utc.Day(), 0, 0, 0, 0, time.UTC).Add(24 * time.Hour)
|
||||||
|
|||||||
Reference in New Issue
Block a user