Changeset 0.26.0 (#165)
This commit is contained in:
@@ -3,6 +3,7 @@ package postgres
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
)
|
||||
@@ -87,6 +88,21 @@ func (s *SessionStore) Delete(ctx context.Context, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteExpired removes sessions created before olderThan whose channel
|
||||
// has no messages. Returns the count of deleted rows.
|
||||
func (s *SessionStore) DeleteExpired(ctx context.Context, olderThan time.Time) (int64, error) {
|
||||
res, err := DB.ExecContext(ctx, `
|
||||
DELETE FROM session_participants sp
|
||||
WHERE sp.created_at < $1
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM messages m WHERE m.channel_id = sp.channel_id
|
||||
)`, olderThan)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// nullText returns nil for empty strings.
|
||||
func nullText(s string) interface{} {
|
||||
if s == "" {
|
||||
|
||||
@@ -41,5 +41,6 @@ func NewStores(db *sql.DB) store.Stores {
|
||||
RoutingPolicies: NewRoutingPolicyStore(db),
|
||||
Sessions: NewSessionStore(),
|
||||
Surfaces: NewSurfaceRegistryStore(),
|
||||
Workflows: NewWorkflowStore(),
|
||||
}
|
||||
}
|
||||
|
||||
316
server/store/postgres/workflows.go
Normal file
316
server/store/postgres/workflows.go
Normal file
@@ -0,0 +1,316 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
)
|
||||
|
||||
// WorkflowStore implements store.WorkflowStore for Postgres.
|
||||
type WorkflowStore struct{}
|
||||
|
||||
func NewWorkflowStore() *WorkflowStore { return &WorkflowStore{} }
|
||||
|
||||
// ── Workflow CRUD ───────────────────────────
|
||||
|
||||
func (s *WorkflowStore) Create(ctx context.Context, w *models.Workflow) error {
|
||||
branding := jsonOrEmpty(w.Branding)
|
||||
retention := jsonOrDefault(w.Retention, `{"mode":"archive"}`)
|
||||
return DB.QueryRowContext(ctx, `
|
||||
INSERT INTO workflows (team_id, name, slug, description, branding, entry_mode, is_active, on_complete, retention, created_by)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING id, version, created_at, updated_at`,
|
||||
w.TeamID, w.Name, w.Slug, w.Description, branding, w.EntryMode,
|
||||
w.IsActive, jsonOrNull(w.OnComplete), retention, w.CreatedBy,
|
||||
).Scan(&w.ID, &w.Version, &w.CreatedAt, &w.UpdatedAt)
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) GetByID(ctx context.Context, id string) (*models.Workflow, error) {
|
||||
w := &models.Workflow{}
|
||||
var branding, retention, onComplete []byte
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, team_id, name, slug, description, branding, entry_mode, is_active,
|
||||
version, on_complete, retention, created_by, created_at, updated_at
|
||||
FROM workflows WHERE id = $1`, id,
|
||||
).Scan(&w.ID, &w.TeamID, &w.Name, &w.Slug, &w.Description, &branding,
|
||||
&w.EntryMode, &w.IsActive, &w.Version, &onComplete, &retention,
|
||||
&w.CreatedBy, &w.CreatedAt, &w.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Branding = branding
|
||||
w.OnComplete = onComplete
|
||||
w.Retention = retention
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) GetBySlug(ctx context.Context, teamID *string, slug string) (*models.Workflow, error) {
|
||||
var q string
|
||||
var args []interface{}
|
||||
if teamID != nil {
|
||||
q = `SELECT id, team_id, name, slug, description, branding, entry_mode, is_active,
|
||||
version, on_complete, retention, created_by, created_at, updated_at
|
||||
FROM workflows WHERE team_id = $1 AND slug = $2`
|
||||
args = []interface{}{*teamID, slug}
|
||||
} else {
|
||||
q = `SELECT id, team_id, name, slug, description, branding, entry_mode, is_active,
|
||||
version, on_complete, retention, created_by, created_at, updated_at
|
||||
FROM workflows WHERE team_id IS NULL AND slug = $1`
|
||||
args = []interface{}{slug}
|
||||
}
|
||||
w := &models.Workflow{}
|
||||
var branding, retention, onComplete []byte
|
||||
err := DB.QueryRowContext(ctx, q, args...).Scan(
|
||||
&w.ID, &w.TeamID, &w.Name, &w.Slug, &w.Description, &branding,
|
||||
&w.EntryMode, &w.IsActive, &w.Version, &onComplete, &retention,
|
||||
&w.CreatedBy, &w.CreatedAt, &w.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Branding = branding
|
||||
w.OnComplete = onComplete
|
||||
w.Retention = retention
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) Update(ctx context.Context, id string, patch models.WorkflowPatch) error {
|
||||
// Build dynamic SET clause
|
||||
sets := []string{}
|
||||
args := []interface{}{}
|
||||
idx := 1
|
||||
|
||||
add := func(col string, val interface{}) {
|
||||
sets = append(sets, fmt.Sprintf("%s = $%d", col, idx))
|
||||
args = append(args, val)
|
||||
idx++
|
||||
}
|
||||
|
||||
if patch.Name != nil {
|
||||
add("name", *patch.Name)
|
||||
}
|
||||
if patch.Description != nil {
|
||||
add("description", *patch.Description)
|
||||
}
|
||||
if patch.Branding != nil {
|
||||
add("branding", string(*patch.Branding))
|
||||
}
|
||||
if patch.EntryMode != nil {
|
||||
add("entry_mode", *patch.EntryMode)
|
||||
}
|
||||
if patch.IsActive != nil {
|
||||
add("is_active", *patch.IsActive)
|
||||
}
|
||||
if patch.OnComplete != nil {
|
||||
add("on_complete", string(*patch.OnComplete))
|
||||
}
|
||||
if patch.Retention != nil {
|
||||
add("retention", string(*patch.Retention))
|
||||
}
|
||||
|
||||
if len(sets) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Increment version on any edit
|
||||
sets = append(sets, fmt.Sprintf("version = version + 1"))
|
||||
|
||||
q := "UPDATE workflows SET "
|
||||
for i, s := range sets {
|
||||
if i > 0 {
|
||||
q += ", "
|
||||
}
|
||||
q += s
|
||||
}
|
||||
q += fmt.Sprintf(" WHERE id = $%d", idx)
|
||||
args = append(args, id)
|
||||
|
||||
_, err := DB.ExecContext(ctx, q, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) Delete(ctx context.Context, id string) error {
|
||||
_, err := DB.ExecContext(ctx, `DELETE FROM workflows WHERE id = $1`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) ListForTeam(ctx context.Context, teamID string) ([]models.Workflow, error) {
|
||||
return s.queryWorkflows(ctx, `
|
||||
SELECT id, team_id, name, slug, description, branding, entry_mode, is_active,
|
||||
version, on_complete, retention, created_by, created_at, updated_at
|
||||
FROM workflows WHERE team_id = $1
|
||||
ORDER BY name ASC`, teamID)
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) ListGlobal(ctx context.Context) ([]models.Workflow, error) {
|
||||
return s.queryWorkflows(ctx, `
|
||||
SELECT id, team_id, name, slug, description, branding, entry_mode, is_active,
|
||||
version, on_complete, retention, created_by, created_at, updated_at
|
||||
FROM workflows WHERE team_id IS NULL
|
||||
ORDER BY name ASC`)
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) queryWorkflows(ctx context.Context, q string, args ...interface{}) ([]models.Workflow, error) {
|
||||
rows, err := DB.QueryContext(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var result []models.Workflow
|
||||
for rows.Next() {
|
||||
var w models.Workflow
|
||||
var branding, retention, onComplete []byte
|
||||
if err := rows.Scan(&w.ID, &w.TeamID, &w.Name, &w.Slug, &w.Description,
|
||||
&branding, &w.EntryMode, &w.IsActive, &w.Version, &onComplete,
|
||||
&retention, &w.CreatedBy, &w.CreatedAt, &w.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.Branding = branding
|
||||
w.OnComplete = onComplete
|
||||
w.Retention = retention
|
||||
result = append(result, w)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// ── Stages ──────────────────────────────────
|
||||
|
||||
func (s *WorkflowStore) CreateStage(ctx context.Context, st *models.WorkflowStage) error {
|
||||
formTpl := jsonOrEmpty(st.FormTemplate)
|
||||
transRules := jsonOrEmpty(st.TransitionRules)
|
||||
return DB.QueryRowContext(ctx, `
|
||||
INSERT INTO workflow_stages (workflow_id, ordinal, name, persona_id, assignment_team_id,
|
||||
form_template, history_mode, auto_transition, transition_rules)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id, created_at`,
|
||||
st.WorkflowID, st.Ordinal, st.Name, st.PersonaID, st.AssignmentTeamID,
|
||||
formTpl, st.HistoryMode, st.AutoTransition, transRules,
|
||||
).Scan(&st.ID, &st.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) ListStages(ctx context.Context, workflowID string) ([]models.WorkflowStage, error) {
|
||||
rows, err := DB.QueryContext(ctx, `
|
||||
SELECT id, workflow_id, ordinal, name, persona_id, assignment_team_id,
|
||||
form_template, history_mode, auto_transition, transition_rules, created_at
|
||||
FROM workflow_stages WHERE workflow_id = $1
|
||||
ORDER BY ordinal ASC`, workflowID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var result []models.WorkflowStage
|
||||
for rows.Next() {
|
||||
var st models.WorkflowStage
|
||||
var formTpl, transRules []byte
|
||||
if err := rows.Scan(&st.ID, &st.WorkflowID, &st.Ordinal, &st.Name,
|
||||
&st.PersonaID, &st.AssignmentTeamID, &formTpl, &st.HistoryMode,
|
||||
&st.AutoTransition, &transRules, &st.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
st.FormTemplate = formTpl
|
||||
st.TransitionRules = transRules
|
||||
result = append(result, st)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) UpdateStage(ctx context.Context, st *models.WorkflowStage) error {
|
||||
formTpl := jsonOrEmpty(st.FormTemplate)
|
||||
transRules := jsonOrEmpty(st.TransitionRules)
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
UPDATE workflow_stages
|
||||
SET ordinal = $2, name = $3, persona_id = $4, assignment_team_id = $5,
|
||||
form_template = $6, history_mode = $7, auto_transition = $8, transition_rules = $9
|
||||
WHERE id = $1`,
|
||||
st.ID, st.Ordinal, st.Name, st.PersonaID, st.AssignmentTeamID,
|
||||
formTpl, st.HistoryMode, st.AutoTransition, transRules)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) DeleteStage(ctx context.Context, id string) error {
|
||||
_, err := DB.ExecContext(ctx, `DELETE FROM workflow_stages WHERE id = $1`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) ReorderStages(ctx context.Context, workflowID string, orderedIDs []string) error {
|
||||
tx, err := DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
for i, id := range orderedIDs {
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE workflow_stages SET ordinal = $1 WHERE id = $2 AND workflow_id = $3`,
|
||||
i, id, workflowID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ── Versions ────────────────────────────────
|
||||
|
||||
func (s *WorkflowStore) Publish(ctx context.Context, v *models.WorkflowVersion) error {
|
||||
return DB.QueryRowContext(ctx, `
|
||||
INSERT INTO workflow_versions (workflow_id, version_number, snapshot)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, created_at`,
|
||||
v.WorkflowID, v.VersionNumber, string(v.Snapshot),
|
||||
).Scan(&v.ID, &v.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) GetVersion(ctx context.Context, workflowID string, versionNumber int) (*models.WorkflowVersion, error) {
|
||||
v := &models.WorkflowVersion{}
|
||||
var snapshot []byte
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, workflow_id, version_number, snapshot, created_at
|
||||
FROM workflow_versions WHERE workflow_id = $1 AND version_number = $2`,
|
||||
workflowID, versionNumber,
|
||||
).Scan(&v.ID, &v.WorkflowID, &v.VersionNumber, &snapshot, &v.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.Snapshot = snapshot
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowStore) GetLatestVersion(ctx context.Context, workflowID string) (*models.WorkflowVersion, error) {
|
||||
v := &models.WorkflowVersion{}
|
||||
var snapshot []byte
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, workflow_id, version_number, snapshot, created_at
|
||||
FROM workflow_versions WHERE workflow_id = $1
|
||||
ORDER BY version_number DESC LIMIT 1`,
|
||||
workflowID,
|
||||
).Scan(&v.ID, &v.WorkflowID, &v.VersionNumber, &snapshot, &v.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.Snapshot = snapshot
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// ── Helpers ─────────────────────────────────
|
||||
|
||||
func jsonOrEmpty(b json.RawMessage) string {
|
||||
if len(b) == 0 {
|
||||
return "{}"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func jsonOrDefault(b json.RawMessage, def string) string {
|
||||
if len(b) == 0 {
|
||||
return def
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func jsonOrNull(b json.RawMessage) interface{} {
|
||||
if len(b) == 0 || string(b) == "null" {
|
||||
return nil
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
Reference in New Issue
Block a user