Phase 14, parts 5 and 6: seats, and moving down to Free
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>
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/billing"
|
||||
@@ -69,6 +72,9 @@ func (s *Server) jobAllowed(name string) bool {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -115,3 +121,190 @@ func (s *Server) crawlLimits() (int, time.Duration) {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user