feat(32.2-01): per-item rows and item actions on My account contributions

- pact: AccountContribution.Items/ItemActions/ItemsEmpty, AccountItem,
  AccountItemAction, AccountItemActionInput, ErrAccountItemNotFound
- cabana: the list sends items (null without Items), permitted item actions
  and the empty line; boot refuses item actions without Items
- cabana: POST {api}/account/contribution-items/{name}/{item}/actions/{action}
  runs only for an item the administrator's own Items listed, in a transaction
  (path outside /account/contributions/ to avoid a ServeMux overlap)
- swag views; admin.json and schema.d.ts regenerated; route inventories
This commit is contained in:
Jakub Zych
2026-11-21 00:22:13 +01:00
parent 97d7ee0586
commit 3f7ce6020d
16 changed files with 706 additions and 30 deletions

View File

@@ -3,6 +3,7 @@ package pact
import (
"context"
"encoding/json"
"errors"
"io"
"io/fs"
"net/http"
@@ -607,18 +608,70 @@ type HasAccountContributions interface {
// signed-in administrator (an empty string shows none). Actions are the
// section's buttons.
//
// Items, when set, adds a list of rows under the status line: a pure read
// scoped to the signed-in administrator that answers that administrator's
// rows (connections, devices, keys). ItemActions are the buttons of each row;
// the framework runs one only for an item ID the administrator's own Items
// listed. ItemsEmpty (a phrase key or text) is the line shown when Items
// answers no rows; empty means the framework's own empty line. A nil Items
// keeps the section exactly as it is without these fields.
//
// The admin compiles contributions at boot, only when it is active. A name
// that is not an identifier, a name another plugin already contributed, an
// empty Label, an action whose name is not an identifier or repeats within
// the section, a nil Run or an empty action Label stops the boot.
// empty Label, an action or item action whose name is not an identifier or
// repeats within the section, a nil Run, an empty action Label or item
// actions without Items stops the boot.
type AccountContribution struct {
Name string
Label string
Description string
Status func(ctx context.Context, adminID uint) (string, error) `json:"-"`
Actions []AccountAction
Items func(ctx context.Context, adminID uint) ([]AccountItem, error) `json:"-"`
ItemActions []AccountItemAction
ItemsEmpty string
}
// AccountItem is one row of an AccountContribution's Items: an ID unique
// within the section (the item action route names it), a Title, an optional
// Subtitle and optional Meta entries (short lines such as dates). All are
// shown as text, never as markup.
type AccountItem struct {
ID string
Title string
Subtitle string
Meta []string
}
// AccountItemAction is one button of every row of an AccountContribution.
// Label and Confirm are phrase keys or text; Confirm may contain :title,
// which the admin fills with the item's Title, and Danger selects the
// confirm dialog's danger variant. Permissions are extra backend permission
// codes the administrator needs. Run acts for the signed-in administrator on
// one item, inside a transaction (cabana.TxFromContext reads it), and only
// after the framework found the item among the administrator's own Items;
// it answers ErrAccountItemNotFound when the item is gone meanwhile.
type AccountItemAction struct {
Name string
Label string
Confirm string
Danger bool
Permissions []string
Run func(ctx context.Context, in AccountItemActionInput) (AccountActionResult, error) `json:"-"`
}
// AccountItemActionInput is what the framework hands an AccountItemAction:
// the id of the signed-in administrator and the ID of the item, which that
// administrator's Items listed.
type AccountItemActionInput struct {
AdminID uint
ItemID string
}
// ErrAccountItemNotFound is what an AccountItemAction's Run answers when its
// item no longer exists; the admin API answers 404.
var ErrAccountItemNotFound = errors.New("pact: account item not found")
// AccountAction is one button of an AccountContribution. Confirm, when set,
// is the confirmation text the admin shows first (Danger selects its danger
// variant). Permissions are extra backend permission codes the administrator