248 lines
8.9 KiB
Go
248 lines
8.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// ErrNoRaise means no raise has that id.
|
|
var ErrNoRaise = errors.New("no such raise")
|
|
|
|
// Raise is a proposal to loosen a class by one level.
|
|
type Raise struct {
|
|
ID uuid.UUID `db:"id"`
|
|
OrgID string `db:"org_id"`
|
|
ClassID uuid.UUID `db:"class_id"`
|
|
FromLevel string `db:"from_level"`
|
|
ToLevel string `db:"to_level"`
|
|
Evidence []byte `db:"evidence"`
|
|
State string `db:"state"`
|
|
Deadline time.Time `db:"deadline"`
|
|
ProposedAt time.Time `db:"proposed_at"`
|
|
AnsweredBy sql.NullString `db:"answered_by"`
|
|
AnsweredAt sql.NullTime `db:"answered_at"`
|
|
}
|
|
|
|
const raiseColumns = `id, org_id, class_id, from_level, to_level, evidence, state, deadline, proposed_at, answered_by, answered_at`
|
|
|
|
// ClassOfDecision returns the class a decision belongs to.
|
|
func (s *Store) ClassOfDecision(ctx context.Context, orgID string, decisionID uuid.UUID) (Class, error) {
|
|
var out Class
|
|
err := s.db.GetContext(ctx, &out, `
|
|
SELECT c.id, c.org_id, c.connector, c.action, c.target_kind, c.environment, c.read_only, c.level,
|
|
c.created_at, c.reset_at, c.tightened_at, c.tightened_because
|
|
FROM judgement.decisions d
|
|
JOIN judgement.classes c ON c.org_id = d.org_id AND c.connector = d.connector AND c.action = d.action
|
|
AND c.target_kind = d.target_kind AND c.environment = d.environment
|
|
WHERE d.org_id = $1 AND d.id = $2`, orgID, decisionID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Class{}, ErrNoClass
|
|
}
|
|
if err != nil {
|
|
return Class{}, fmt.Errorf("read class of decision: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ClassByKey returns the class named by its four names.
|
|
func (s *Store) ClassByKey(ctx context.Context, c Class) (Class, error) {
|
|
var out Class
|
|
err := s.db.GetContext(ctx, &out, `
|
|
SELECT `+classColumns+` FROM judgement.classes
|
|
WHERE org_id = $1 AND connector = $2 AND action = $3 AND target_kind = $4 AND environment = $5`,
|
|
c.OrgID, c.Connector, c.Action, c.TargetKind, c.Environment)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Class{}, ErrNoClass
|
|
}
|
|
if err != nil {
|
|
return Class{}, fmt.Errorf("read class: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// RaiseByID returns one raise.
|
|
func (s *Store) RaiseByID(ctx context.Context, orgID string, id uuid.UUID) (Raise, error) {
|
|
var out Raise
|
|
err := s.db.GetContext(ctx, &out, `SELECT `+raiseColumns+` FROM judgement.class_raises WHERE org_id = $1 AND id = $2`, orgID, id)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Raise{}, ErrNoRaise
|
|
}
|
|
if err != nil {
|
|
return Raise{}, fmt.Errorf("read raise: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ClassRaises returns a class's raises, newest first.
|
|
func (s *Store) ClassRaises(ctx context.Context, id uuid.UUID) ([]Raise, error) {
|
|
var out []Raise
|
|
if err := s.db.SelectContext(ctx, &out, `
|
|
SELECT `+raiseColumns+` FROM judgement.class_raises WHERE class_id = $1 ORDER BY proposed_at DESC, id`, id); err != nil {
|
|
return nil, fmt.Errorf("list class raises: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ClassCount returns how many approvals a class has counted, for the class
|
|
// view.
|
|
func (s *Store) ClassCount(ctx context.Context, id uuid.UUID) (int, error) {
|
|
var n int
|
|
if err := s.db.GetContext(ctx, &n, `SELECT count(*) FROM judgement.class_approvals WHERE class_id = $1 AND counted`, id); err != nil {
|
|
return 0, fmt.Errorf("count approvals: %w", err)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// ClassTx is one class held for update. Everything done through it commits
|
|
// together, so two answers arriving at once see each other's count.
|
|
type ClassTx struct {
|
|
tx *sqlx.Tx
|
|
now time.Time
|
|
Class Class
|
|
}
|
|
|
|
// InClass runs fn with the class locked. A raise nobody answered by its
|
|
// deadline is expired first, and its count restarts, so a class that wasn't
|
|
// raised is asked again only after earning it again.
|
|
func (s *Store) InClass(ctx context.Context, orgID string, id uuid.UUID, now time.Time, fn func(*ClassTx) error) error {
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("class: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
t := &ClassTx{tx: tx, now: now}
|
|
err = tx.GetContext(ctx, &t.Class, `SELECT `+classColumns+` FROM judgement.classes WHERE org_id = $1 AND id = $2 FOR UPDATE`, orgID, id)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNoClass
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("lock class: %w", err)
|
|
}
|
|
res, err := tx.ExecContext(ctx, `
|
|
UPDATE judgement.class_raises SET state = 'expired', answered_at = $2
|
|
WHERE class_id = $1 AND state = 'pending' AND deadline <= $2`, id, now)
|
|
if err != nil {
|
|
return fmt.Errorf("expire raise: %w", err)
|
|
}
|
|
if n, _ := res.RowsAffected(); n > 0 {
|
|
if err := t.ClearCount(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := fn(t); err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return fmt.Errorf("class: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Count returns how many approvals count toward a raise, and the raise waiting
|
|
// for an answer, if one is.
|
|
func (t *ClassTx) Count(ctx context.Context) (int, *Raise, error) {
|
|
var n int
|
|
if err := t.tx.GetContext(ctx, &n, `SELECT count(*) FROM judgement.class_approvals WHERE class_id = $1 AND counted`, t.Class.ID); err != nil {
|
|
return 0, nil, fmt.Errorf("count approvals: %w", err)
|
|
}
|
|
var r Raise
|
|
err := t.tx.GetContext(ctx, &r, `SELECT `+raiseColumns+` FROM judgement.class_raises WHERE class_id = $1 AND state = 'pending'`, t.Class.ID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return n, nil, nil
|
|
}
|
|
if err != nil {
|
|
return 0, nil, fmt.Errorf("read pending raise: %w", err)
|
|
}
|
|
return n, &r, nil
|
|
}
|
|
|
|
// AddApproval counts one approved decision. A decision counts once.
|
|
func (t *ClassTx) AddApproval(ctx context.Context, decisionID uuid.UUID, by string) error {
|
|
if _, err := t.tx.ExecContext(ctx, `
|
|
INSERT INTO judgement.class_approvals (id, org_id, class_id, decision_id, by, at)
|
|
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (decision_id) DO NOTHING`,
|
|
uuid.New(), t.Class.OrgID, t.Class.ID, decisionID, by, t.now); err != nil {
|
|
return fmt.Errorf("record approval: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Approval is one counted approval, the evidence for a raise.
|
|
type Approval struct {
|
|
DecisionID uuid.UUID `db:"decision_id" json:"decision_id"`
|
|
By string `db:"by" json:"by"`
|
|
At time.Time `db:"at" json:"at"`
|
|
}
|
|
|
|
// Approvals returns the approvals that count, oldest first.
|
|
func (t *ClassTx) Approvals(ctx context.Context) ([]Approval, error) {
|
|
var out []Approval
|
|
if err := t.tx.SelectContext(ctx, &out, `
|
|
SELECT decision_id, by, at FROM judgement.class_approvals WHERE class_id = $1 AND counted ORDER BY at, id`, t.Class.ID); err != nil {
|
|
return nil, fmt.Errorf("list approvals: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ClearCount stops every approval counting.
|
|
func (t *ClassTx) ClearCount(ctx context.Context) error {
|
|
if _, err := t.tx.ExecContext(ctx, `UPDATE judgement.class_approvals SET counted = false WHERE class_id = $1 AND counted`, t.Class.ID); err != nil {
|
|
return fmt.Errorf("clear approvals: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetLevel sets the class's level.
|
|
func (t *ClassTx) SetLevel(ctx context.Context, level string) error {
|
|
if _, err := t.tx.ExecContext(ctx, `UPDATE judgement.classes SET level = $2 WHERE id = $1`, t.Class.ID, level); err != nil {
|
|
return fmt.Errorf("set level: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Tighten records that the class lost autonomy, and why.
|
|
func (t *ClassTx) Tighten(ctx context.Context, level, because string) error {
|
|
if _, err := t.tx.ExecContext(ctx, `
|
|
UPDATE judgement.classes SET level = $2, tightened_at = $3, tightened_because = $4 WHERE id = $1`,
|
|
t.Class.ID, level, t.now, because); err != nil {
|
|
return fmt.Errorf("tighten class: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CloseRaise settles the class's pending raise.
|
|
func (t *ClassTx) CloseRaise(ctx context.Context, state, by string) error {
|
|
if _, err := t.tx.ExecContext(ctx, `
|
|
UPDATE judgement.class_raises SET state = $2, answered_by = nullif($3, ''), answered_at = $4
|
|
WHERE class_id = $1 AND state = 'pending'`, t.Class.ID, state, by, t.now); err != nil {
|
|
return fmt.Errorf("close raise: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// InsertRaise records a proposal.
|
|
func (t *ClassTx) InsertRaise(ctx context.Context, r Raise) error {
|
|
if _, err := t.tx.ExecContext(ctx, `
|
|
INSERT INTO judgement.class_raises (id, org_id, class_id, from_level, to_level, evidence, state, deadline, proposed_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, 'pending', $7, $8)`,
|
|
r.ID, t.Class.OrgID, t.Class.ID, r.FromLevel, r.ToLevel, r.Evidence, r.Deadline, t.now); err != nil {
|
|
return fmt.Errorf("record raise: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CancelRaise withdraws a pending raise whose request couldn't be asked.
|
|
func (s *Store) CancelRaise(ctx context.Context, orgID string, id uuid.UUID) error {
|
|
if _, err := s.db.ExecContext(ctx, `
|
|
UPDATE judgement.class_raises SET state = 'superseded' WHERE org_id = $1 AND id = $2 AND state = 'pending'`, orgID, id); err != nil {
|
|
return fmt.Errorf("cancel raise: %w", err)
|
|
}
|
|
return nil
|
|
}
|