On the multi-tenant edition each team's data is in its own schema; the tables every team shares (people, sessions, memberships, keys, locks, and a new teams table) stay in the main one. A team's connection pool searches its own schema first, so the app's SQL runs unchanged and can't reach another team's tables. Each request is served by a copy of the Server for the viewer's team, with that team's database, secrets and caches; agents work on their own team's. The local and server editions are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
package users
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// Teams (the multi-tenant edition): each is a company, working on one
|
|
// website domain, with its data in its own schema (db.Teams). On the local
|
|
// and server editions there's one, Team, and no row for it.
|
|
|
|
// Team statuses.
|
|
const (
|
|
TeamActive = "active"
|
|
TeamSuspended = "suspended"
|
|
TeamDeleting = "deleting"
|
|
)
|
|
|
|
// TeamInfo is a team.
|
|
type TeamInfo struct {
|
|
ID int64
|
|
Name string
|
|
Domain string
|
|
Status string
|
|
Created time.Time
|
|
DeleteAt time.Time
|
|
}
|
|
|
|
const teamCols = `id, name, domain, status, created_at, delete_at`
|
|
|
|
func scanTeam(row interface{ Scan(...any) error }) (*TeamInfo, error) {
|
|
var t TeamInfo
|
|
var created, del string
|
|
if err := row.Scan(&t.ID, &t.Name, &t.Domain, &t.Status, &created, &del); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
t.Created, t.DeleteAt = parseTS(created), parseTS(del)
|
|
return &t, nil
|
|
}
|
|
|
|
// CreateTeam adds a team (its schema is the caller's to make).
|
|
func CreateTeam(d *db.DB, name, domain string, now time.Time) (*TeamInfo, error) {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
return nil, errors.New("give the company's name")
|
|
}
|
|
t := &TeamInfo{Name: name, Domain: strings.ToLower(strings.TrimSpace(domain)), Status: TeamActive, Created: now}
|
|
var err error
|
|
t.ID, err = d.Insert(`INSERT INTO teams (name, domain, status, created_at) VALUES (?, ?, ?, ?)`, t.Name, t.Domain, t.Status, ts(now))
|
|
return t, err
|
|
}
|
|
|
|
// GetTeam finds a team.
|
|
func GetTeam(d *db.DB, id int64) (*TeamInfo, error) {
|
|
return scanTeam(d.QueryRow(`SELECT `+teamCols+` FROM teams WHERE id = ?`, id))
|
|
}
|
|
|
|
// ListTeams is every team, newest first.
|
|
func ListTeams(d *db.DB) ([]TeamInfo, error) {
|
|
return queryTeams(d, `SELECT `+teamCols+` FROM teams ORDER BY id DESC`)
|
|
}
|
|
|
|
// TeamsOf are the teams a user belongs to, by name.
|
|
func TeamsOf(d *db.DB, user int64) ([]TeamInfo, error) {
|
|
return queryTeams(d, `SELECT t.id, t.name, t.domain, t.status, t.created_at, t.delete_at FROM teams t
|
|
JOIN memberships m ON m.team_id = t.id WHERE m.user_id = ? ORDER BY t.name COLLATE NOCASE, t.id`, user)
|
|
}
|
|
|
|
func queryTeams(d *db.DB, q string, args ...any) ([]TeamInfo, error) {
|
|
rows, err := d.Query(q, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []TeamInfo
|
|
for rows.Next() {
|
|
t, err := scanTeam(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *t)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// SetTeamStatus suspends, restores or marks a team for deletion.
|
|
func SetTeamStatus(d *db.DB, id int64, status string, deleteAt time.Time) error {
|
|
_, err := d.Exec(`UPDATE teams SET status = ?, delete_at = ? WHERE id = ?`, status, tsOrEmpty(deleteAt), id)
|
|
return err
|
|
}
|
|
|
|
// SetSessionTeam switches a session to another of its user's teams.
|
|
func SetSessionTeam(d *db.DB, sessionID string, team int64) error {
|
|
_, err := d.Exec(`UPDATE sessions SET team_id = ? WHERE id = ?`, team, sessionID)
|
|
return err
|
|
}
|
|
|
|
// AgentTeam is the team an agent works for: agents belong to one.
|
|
func AgentTeam(d *db.DB, agent int64) (int64, error) {
|
|
var t int64
|
|
err := d.QueryRow(`SELECT team_id FROM memberships WHERE user_id = ? ORDER BY team_id LIMIT 1`, agent).Scan(&t)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return 0, ErrNotFound
|
|
}
|
|
return t, err
|
|
}
|