File size: 950 Bytes
35cdce3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import sqlite3  # or use psycopg2, mysql.connector, etc. depending on your DB

# --- Simple validator ---
def extract_cust_id(text: str):
    """Return cust_id in format C#### or None"""
    m = re.search(r"\b(C\d{4})\b", text, flags=re.I)
    return m.group(1).upper() if m else None

# --- Validate customer ID using direct SQL ---
def is_valid_customer(customer_id: str) -> bool:
    cust_id = extract_cust_id(customer_id)
    if not cust_id:
        return True

    try:
        # Connect to your database
        conn = sqlite3.connect("customer_orders.db")  # Replace with your actual DB connection
        cursor = conn.cursor()

        # Run a simple query to check existence
        cursor.execute("SELECT 1 FROM orders WHERE cust_id = ?", (cust_id,))
        result = cursor.fetchone()

        conn.close()
        return result is not None

    except Exception as e:
        print(f"Database error: {e}")
        return True