Seats: inviting past the seats paid for offers the owner to add one (Stripe prorates it); admins are told to ask the owner; Free has two. Moving down: past the plan's seats, the owner and one other (the owner's pick, or the first to join) edit and the rest read; storage is measured hourly, and past the plan's, photo uploads and the SEO crawl stop. Nothing is deleted. TEST.md section 18. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
311 lines
8.9 KiB
Go
311 lines
8.9 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"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]
|
|
if name == "seo" && s.storageFull() != nil {
|
|
return false // a crawl keeps its pages
|
|
}
|
|
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()
|
|
}
|
|
|
|
// seatRoom says why the team has no room for another person ("" if it
|
|
// has), and whether a seat could be bought for them: a subscription paid
|
|
// per person, not lapsed. On the trial there's no limit: Checkout counts
|
|
// the people.
|
|
func (s *Server) seatRoom() (problem string, canAdd bool, a *billing.Account) {
|
|
if !s.billingOn() || s.team() == 0 {
|
|
return "", false, nil
|
|
}
|
|
a, err := billing.Get(s.platform(), s.team())
|
|
if err != nil {
|
|
return "", false, nil
|
|
}
|
|
plan, ro := a.Effective(s.Plans, time.Now())
|
|
people, _ := s.people()
|
|
seats := plan.Seats
|
|
if seats == 0 {
|
|
if a.Subscription == "" || a.Seats == 0 {
|
|
return "", false, a
|
|
}
|
|
seats = a.Seats
|
|
}
|
|
if people < seats {
|
|
return "", false, a
|
|
}
|
|
if plan.Seats != 0 {
|
|
return fmt.Sprintf("The %s plan has %d people, and the team has them all. To invite more, subscribe under Settings → Billing.", plan.Name, plan.Seats), false, a
|
|
}
|
|
return fmt.Sprintf("All %d seats paid for are taken.", seats), !ro, a
|
|
}
|
|
|
|
// addSeat pays for one more person: Stripe charges for the rest of the
|
|
// period now (prorated).
|
|
func (s *Server) addSeat(ctx context.Context, a *billing.Account) error {
|
|
people, _ := s.people()
|
|
n := a.Seats
|
|
if people > n {
|
|
n = people
|
|
}
|
|
n++
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
if err := s.billingStripe().SetQuantity(ctx, a.Subscription, n); err != nil {
|
|
return err
|
|
}
|
|
a.Seats = n
|
|
return billing.Save(s.platform(), a, time.Now())
|
|
}
|
|
|
|
// seatCheck is the invite's and turning-on's look at the seats: "" to go
|
|
// ahead (a seat added first if the owner said so), or what to say, and
|
|
// whether to offer the owner a seat (only where the form can take it).
|
|
func (s *Server) seatCheck(r *http.Request, v *viewer, offerable bool) (problem string, offer bool) {
|
|
problem, canAdd, a := s.seatRoom()
|
|
if problem == "" {
|
|
return "", false
|
|
}
|
|
owner := v.Member.Role == users.Owner && !v.Viewing
|
|
switch {
|
|
case canAdd && owner && !offerable:
|
|
return problem + " Turn someone else off first, or add seats on Stripe's billing page (Settings → Billing).", false
|
|
case canAdd && owner && r.PostFormValue("add_seat") == "1":
|
|
if err := s.addSeat(r.Context(), a); err != nil {
|
|
return "Stripe didn't add the seat: " + err.Error() + ".", true
|
|
}
|
|
s.audit(r, "billing-seat", "", fmt.Sprintf("%d seats", a.Seats))
|
|
return "", false
|
|
case canAdd && owner:
|
|
return problem + " Add one: Stripe charges for it from today, for the rest of the period.", true
|
|
case canAdd:
|
|
return problem + " Ask the team's owner to add one.", false
|
|
case !owner:
|
|
return problem + " Ask the team's owner.", false
|
|
}
|
|
return problem, false
|
|
}
|
|
|
|
// kept says who keeps their access when a team has more people than its
|
|
// plan's seats (after moving down to Free): the owner, the one the owner
|
|
// chose (billing.kept_member), then whoever joined first. The rest can
|
|
// read, not change. nil: everyone keeps theirs.
|
|
func (s *Server) kept() (map[int64]bool, *billing.Plan) {
|
|
if !s.billingOn() || s.team() == 0 {
|
|
return nil, nil
|
|
}
|
|
a, err := billing.Get(s.platform(), s.team())
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
plan, _ := a.Effective(s.Plans, time.Now())
|
|
if plan.Seats == 0 {
|
|
return nil, plan
|
|
}
|
|
list, err := users.Members(s.platform(), s.team())
|
|
if err != nil {
|
|
return nil, plan
|
|
}
|
|
var people []users.TeamUser
|
|
for _, m := range list {
|
|
if m.Kind == users.Person && m.Status != users.Disabled {
|
|
people = append(people, m)
|
|
}
|
|
}
|
|
if len(people) <= plan.Seats {
|
|
return nil, plan
|
|
}
|
|
rank := func(m users.TeamUser) int {
|
|
switch {
|
|
case m.Role == users.Owner:
|
|
return 0
|
|
case m.ID == a.KeptMember:
|
|
return 1
|
|
}
|
|
return 2
|
|
}
|
|
sort.SliceStable(people, func(i, j int) bool {
|
|
if ri, rj := rank(people[i]), rank(people[j]); ri != rj {
|
|
return ri < rj
|
|
}
|
|
return people[i].Joined.Before(people[j].Joined)
|
|
})
|
|
out := map[int64]bool{}
|
|
for _, m := range people[:plan.Seats] {
|
|
out[m.ID] = true
|
|
}
|
|
return out, plan
|
|
}
|
|
|
|
// viewOnly makes a membership read-only: an admin reads every module, a
|
|
// member theirs, and neither changes anything.
|
|
func viewOnly(m users.Membership) users.Membership {
|
|
if m.Admin() {
|
|
m.Modules = map[string]users.Access{}
|
|
for _, g := range grants() {
|
|
m.Modules[g.ID] = users.View
|
|
}
|
|
m.Role = users.Member
|
|
}
|
|
m.Cap = map[string]users.Access{}
|
|
for id := range m.Modules {
|
|
m.Cap[id] = users.View
|
|
}
|
|
return m
|
|
}
|
|
|
|
// storageFull says why the team can't store another file, or nil.
|
|
func (s *Server) storageFull() error {
|
|
if !s.billingOn() || s.team() == 0 {
|
|
return nil
|
|
}
|
|
a, err := billing.Get(s.platform(), s.team())
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
plan, _ := a.Effective(s.Plans, time.Now())
|
|
seats := a.Seats
|
|
if seats == 0 {
|
|
seats, _ = s.people()
|
|
}
|
|
if limit := plan.StorageBytes(seats, a.StorageBlocks); limit > 0 && a.StorageUsed >= limit {
|
|
return fmt.Errorf("the team's storage is full (%s of the %s plan's %s): nothing's deleted, but nothing new is kept until there's room, or the owner moves up a plan or adds storage",
|
|
humanBytes(a.StorageUsed), plan.Name, humanBytes(limit))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// measureStorage notes how much a team stores: its files and its tables.
|
|
func (s *Server) measureStorage(ctx context.Context) (int64, error) {
|
|
var n int64
|
|
if s.Files != nil {
|
|
list, err := s.Files.List(ctx, "")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
for _, f := range list {
|
|
n += f.Size
|
|
}
|
|
}
|
|
if s.Teams != nil {
|
|
size, err := s.Teams.Size(s.team())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n += size
|
|
}
|
|
return n, nil
|
|
}
|