File size: 870 Bytes
f86003d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import psycopg2
import os
import json

def check_margin(price, cost, threshold=0.20):
    """Checks if margin >= threshold."""
    margin = (price - cost) / price if price > 0 else 0
    return json.dumps({"is_compliant": margin >= threshold, "margin": margin})

def log_audit_event(session_id, agent_name, tool_name, is_compliant):
    """Logs audit event to Supabase."""
    try:
        conn = psycopg2.connect(os.getenv("DATABASE_URL"))
        cursor = conn.cursor()
        cursor.execute(
            "INSERT INTO transaction_logs (session_id, agent_name, tool_name, is_compliant) VALUES (%s, %s, %s, %s)",
            (session_id, agent_name, tool_name, is_compliant)
        )
        conn.commit()
        conn.close()
        return json.dumps({"status": "logged"})
    except Exception as e:
        return json.dumps({"status": "error", "message": str(e)})