File size: 1,902 Bytes
8075297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Approval Queue — formal interface for content drafts awaiting approval.
"""

from .db import (
    get_conn,
    upsert_content_variant,
    set_variant_status,
    get_variant_history,
    get_active_variant,
    now_iso,
)


def submit_draft(variant_id: str, kind: str, headline: str, description: str,
                 title: str = "", body: str = "", hypothesis: str = "") -> str:
    """Submit a draft to the approval queue."""
    upsert_content_variant(variant_id, kind, headline, description, title, body,
                         status="pending_approval", hypothesis=hypothesis)
    return variant_id


def approve_draft(variant_id: str) -> bool:
    """Mark a draft as approved."""
    set_variant_status(variant_id, "approved")
    return True


def reject_draft(variant_id: str) -> bool:
    """Reject a draft."""
    set_variant_status(variant_id, "rejected")
    return True


def list_pending(kind: str = None) -> list:
    """List pending drafts."""
    conn = get_conn()
    if kind:
        rows = conn.execute(
            "SELECT * FROM content_variants WHERE kind=? AND status='pending_approval' ORDER BY created_at DESC",
            (kind,)
        ).fetchall()
    else:
        rows = conn.execute(
            "SELECT * FROM content_variants WHERE status='pending_approval' ORDER BY created_at DESC"
        ).fetchall()
    conn.close()
    return [dict(r) for r in rows]


def list_approved(kind: str = None) -> list:
    """List approved drafts."""
    conn = get_conn()
    if kind:
        rows = conn.execute(
            "SELECT * FROM content_variants WHERE kind=? AND status='approved' ORDER BY created_at DESC",
            (kind,)
        ).fetchall()
    else:
        rows = conn.execute(
            "SELECT * FROM content_variants WHERE status='approved' ORDER BY created_at DESC"
        ).fetchall()
    conn.close()
    return [dict(r) for r in rows]