feat: batches with dry run, undo, and CSV import and export

- POST /_seo/api/v1/batch runs changes in one transaction: dry_run
  answers the diff and the conflicts (loops, chains, noindex on a
  published record, canonicals to redirected or noindex addresses) and
  rolls back; a refused change refuses the batch (D-12)
- undo of a change or a whole batch from History and through MCP, with
  the undo_stale, undo_loop and undo_gone refusals (D-13)
- CSV export and import of redirects, rules and records by URL: BOM,
  comma or semicolon, formula cells escaped, a dry-run preview, all or
  nothing with via import; the screen's apply token is HMAC-SHA256 over
  the file and a fingerprint of the data (transfer.stale)
- Import and export singleton with the seo_transfer widget action,
  History record actions undo and undo_batch, navigation entry
- MCP tools undo_change, undo_batch, apply_batch, export_csv, import_csv
- TestBatch, TestUndo, TestCSVTransfer, TestLoopRefused mcp and import
This commit is contained in:
2026-10-27 12:35:26 +01:00
parent aa0c3bcacf
commit ab3b219fec
27 changed files with 3319 additions and 96 deletions

View File

@@ -2,10 +2,12 @@ package api
import (
"context"
"errors"
"net/http"
"git.golem15.com/golem15/sm-seo-plugin/classes"
"git.golem15.com/golem15/sm-seo-plugin/models"
"gorm.io/gorm"
)
// ListHistoryInput is the query of GET /_seo/api/v1/history.
@@ -93,3 +95,64 @@ func ShowHistory(service func() *classes.Service) http.HandlerFunc {
return http.StatusOK, map[string]any{"data": RevisionJSON(rows[0])}, nil
})
}
// HistoryBatchInput names one batch.
type HistoryBatchInput struct {
Batch string `json:"batch" path:"batch" jsonschema:"the batch id"`
}
// undoAnswer maps an undo refusal to 409 with its code and message.
func undoAnswer(ctx context.Context, svc *classes.Service, err error) error {
var refusal *classes.UndoRefusal
if errors.As(err, &refusal) {
return &apiError{Status: http.StatusConflict, Body: map[string]any{
"error": refusal.Code,
"message": svc.PhraseCtx(ctx, refusal.Key, refusal.Params),
}}
}
return err
}
// UndoChange serves POST /_seo/api/v1/history/{id}/undo (D-13): the values
// before the change are restored and the undo is recorded (via undo). It
// answers 409 undo_stale when a later change touched the same values,
// undo_loop when the restore would create a redirect loop, undo_gone when
// the record is gone and already_undone; nothing is undone then.
func UndoChange(service func() *classes.Service) http.HandlerFunc {
return manage(service, func(ctx context.Context, svc *classes.Service, r *http.Request) (int, any, error) {
id, err := pathID(r, "id")
if err != nil {
return 0, nil, err
}
var ids []uint
err = write(ctx, svc, func(ctx context.Context, tx *gorm.DB) error {
var err error
ids, err = svc.Undo(ctx, tx, id)
return err
})
if err != nil {
return 0, nil, undoAnswer(ctx, svc, err)
}
return http.StatusOK, map[string]any{"undone": ids, "count": len(ids)}, nil
})
}
// UndoBatch serves POST /_seo/api/v1/history/batches/{batch}/undo: every
// change of the batch not undone yet is undone in one transaction, newest
// first, or none is (the same refusals as one change). 404 when nothing of
// the batch is left to undo.
func UndoBatch(service func() *classes.Service) http.HandlerFunc {
return manage(service, func(ctx context.Context, svc *classes.Service, r *http.Request) (int, any, error) {
batch := r.PathValue("batch")
var ids []uint
err := write(ctx, svc, func(ctx context.Context, tx *gorm.DB) error {
var err error
ids, err = svc.UndoBatch(ctx, tx, batch)
return err
})
if err != nil {
return 0, nil, undoAnswer(ctx, svc, err)
}
return http.StatusOK, map[string]any{"undone": ids, "count": len(ids)}, nil
})
}