CheckUserLimit checks a user-level limit (not workspace-scoped), such as total workspace count or total API tokens.
(userID, feature string)
| 112 | // CheckUserLimit checks a user-level limit (not workspace-scoped), such as |
| 113 | // total workspace count or total API tokens. |
| 114 | func (s *Store) CheckUserLimit(userID, feature string) (*LimitResult, error) { |
| 115 | user, err := s.GetUser(userID) |
| 116 | if err != nil { |
| 117 | return nil, fmt.Errorf("check user limit: get user: %w", err) |
| 118 | } |
| 119 | if user == nil { |
| 120 | return nil, fmt.Errorf("check user limit: user not found") |
| 121 | } |
| 122 | |
| 123 | plan := user.Plan |
| 124 | if plan == "" { |
| 125 | plan = "free" |
| 126 | } |
| 127 | if plan == "self-hosted" || plan == "pro" { |
| 128 | return &LimitResult{Allowed: true, Feature: feature, Limit: -1, Current: 0, Plan: plan}, nil |
| 129 | } |
| 130 | |
| 131 | limit := s.resolveLimit(plan, feature, user.PlanOverrides) |
| 132 | if limit < 0 { |
| 133 | return &LimitResult{Allowed: true, Feature: feature, Limit: -1, Current: 0, Plan: plan}, nil |
| 134 | } |
| 135 | |
| 136 | current, err := s.userFeatureCount(userID, feature) |
| 137 | if err != nil { |
| 138 | return nil, fmt.Errorf("check user limit: count %s: %w", feature, err) |
| 139 | } |
| 140 | |
| 141 | return &LimitResult{ |
| 142 | Allowed: current < limit, |
| 143 | Feature: feature, |
| 144 | Limit: limit, |
| 145 | Current: current, |
| 146 | Plan: plan, |
| 147 | }, nil |
| 148 | } |
| 149 | |
| 150 | // resolveLimit resolves the limit for a feature using the three-tier resolution: |
| 151 | // user overrides → DB-stored plan defaults → hardcoded fallback. |