Phase 14, part 1: plans, each team's account, and their limits
The multi-tenant edition's plans (Free and Paid as decided, or a plans file) and each team's billing account, with a 14-day trial of Paid. A team's plan caps what its people and agents may change: modules outside it stay readable, a lapsed team changes nothing, and agents, Market entries, crawls and background jobs keep to it. Without a Stripe key, no billing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/billing"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
// What a team's plan allows (the multi-tenant edition, with billing on):
|
||||
// its modules (the others keep their data, read-only), its agents, its
|
||||
// sources, how much it crawls; and, when the subscription has lapsed too
|
||||
// long, nothing that changes anything.
|
||||
|
||||
// billingOn says whether teams are billed.
|
||||
func (s *Server) billingOn() bool { return s.multi() && s.Plans != nil }
|
||||
|
||||
// teamPlan is the team's plan now and whether it's read-only; nil when
|
||||
// there's no billing (everything allowed).
|
||||
func (s *Server) teamPlan() (*billing.Plan, bool) {
|
||||
if !s.billingOn() || s.team() == 0 {
|
||||
return nil, false
|
||||
}
|
||||
a, err := billing.Get(s.platform(), s.team())
|
||||
if err != nil {
|
||||
s.Log.Error("billing: reading the team's", "team", s.team(), "err", err)
|
||||
return nil, false
|
||||
}
|
||||
return a.Effective(s.Plans, time.Now())
|
||||
}
|
||||
|
||||
// errLapsed is what's said when a lapsed team tries to change something.
|
||||
var errLapsed = errors.New("the team's subscription has lapsed: its owner can renew it under Settings → Billing. Until then, nothing is fetched, sent or published")
|
||||
|
||||
// planned limits a membership to the plan: modules outside it, and every
|
||||
// module when the team is read-only, can only be viewed. Admins keep
|
||||
// Settings (but changes there are refused while read-only: allow).
|
||||
func planned(m users.Membership, plan *billing.Plan, readOnly bool) users.Membership {
|
||||
if plan == nil {
|
||||
return m
|
||||
}
|
||||
m.Cap = map[string]users.Access{}
|
||||
for _, g := range grants() {
|
||||
if plan.Includes(g.ID) && !readOnly {
|
||||
m.Cap[g.ID] = users.Edit
|
||||
} else {
|
||||
m.Cap[g.ID] = users.View
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// jobModule is the module a background job works for, if any.
|
||||
var jobModule = map[string]string{
|
||||
"scheduler": "content", "outreach": "prospects", "imports": "prospects", "discovery": "prospects",
|
||||
"tracking": "prospects", "seo": "seo", "metrics": "metrics",
|
||||
}
|
||||
|
||||
// jobAllowed says whether a team's background job may run: its module in
|
||||
// the plan, and the team not lapsed.
|
||||
func (s *Server) jobAllowed(name string) bool {
|
||||
plan, ro := s.teamPlan()
|
||||
if plan == nil {
|
||||
return true
|
||||
}
|
||||
if ro {
|
||||
return name == "accounts"
|
||||
}
|
||||
m, ok := jobModule[name]
|
||||
return !ok || plan.Includes(m)
|
||||
}
|
||||
|
||||
// limitAgents says why the team may not have another agent, or nil.
|
||||
func (s *Server) limitAgents() error {
|
||||
plan, ro := s.teamPlan()
|
||||
if plan == nil {
|
||||
return nil
|
||||
}
|
||||
if ro {
|
||||
return errLapsed
|
||||
}
|
||||
list, err := users.Members(s.platform(), s.team())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n := 0
|
||||
for _, m := range list {
|
||||
if m.Kind == users.Agent && m.Status == users.Active {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if plan.Agents > 0 && n >= plan.Agents {
|
||||
return fmt.Errorf("the %s plan has %d agents, and the team has them all: turn one off, or move up a plan", plan.Name, plan.Agents)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// limitSources says why the Market may not take another entry, or nil.
|
||||
func (s *Server) limitSources(existing int) error {
|
||||
plan, _ := s.teamPlan()
|
||||
if plan == nil || plan.Sources == 0 || existing < plan.Sources {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("the %s plan watches %d entries in the Market: remove one, or move up a plan", plan.Name, plan.Sources)
|
||||
}
|
||||
|
||||
// crawlLimits are how many pages an audit reads and how often (0: the
|
||||
// edition's own).
|
||||
func (s *Server) crawlLimits() (int, time.Duration) {
|
||||
plan, _ := s.teamPlan()
|
||||
if plan == nil {
|
||||
return 0, 7 * 24 * time.Hour
|
||||
}
|
||||
return plan.CrawlPages, plan.CrawlInterval()
|
||||
}
|
||||
Reference in New Issue
Block a user