[30388f42] Infrastructure Hardening: Repaired CE/Connector DB schema, fixed frontend styling build, implemented robust echo shield in worker v2.1.1, and integrated Lead Engine into gateway.
This commit is contained in:
@@ -27,12 +27,26 @@ def init_db():
|
||||
email TEXT,
|
||||
phone TEXT,
|
||||
raw_body TEXT,
|
||||
lead_metadata TEXT,
|
||||
enrichment_data TEXT,
|
||||
status TEXT DEFAULT 'new',
|
||||
response_draft TEXT,
|
||||
sent_at TIMESTAMP
|
||||
)
|
||||
''')
|
||||
|
||||
# Simple migration check: add 'lead_metadata' if not exists
|
||||
c.execute("PRAGMA table_info(leads)")
|
||||
columns = [row[1] for row in c.fetchall()]
|
||||
|
||||
if 'lead_metadata' not in columns:
|
||||
print("Migrating DB: Adding lead_metadata column...")
|
||||
c.execute('ALTER TABLE leads ADD COLUMN lead_metadata TEXT')
|
||||
|
||||
if 'source' not in columns:
|
||||
print("Migrating DB: Adding source column...")
|
||||
c.execute('ALTER TABLE leads ADD COLUMN source TEXT')
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -41,21 +55,40 @@ def insert_lead(lead_data):
|
||||
if not os.path.exists(DB_PATH):
|
||||
init_db()
|
||||
|
||||
# Extract metadata fields
|
||||
meta = {
|
||||
'area': lead_data.get('area'),
|
||||
'purpose': lead_data.get('purpose'),
|
||||
'zip': lead_data.get('zip'),
|
||||
'city': lead_data.get('city'),
|
||||
'role': lead_data.get('role'),
|
||||
'salutation': lead_data.get('salutation'),
|
||||
'phone': lead_data.get('phone'),
|
||||
'cleaning_functions': lead_data.get('cleaning_functions'),
|
||||
'is_free_mail': lead_data.get('is_free_mail', False),
|
||||
'is_low_quality': lead_data.get('is_low_quality', False)
|
||||
}
|
||||
|
||||
# Use provided received_at or default to now
|
||||
received_at = lead_data.get('received_at') or datetime.now()
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
try:
|
||||
c.execute('''
|
||||
INSERT INTO leads (source_id, received_at, company_name, contact_name, email, phone, raw_body, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO leads (source_id, received_at, company_name, contact_name, email, phone, raw_body, lead_metadata, status, source)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
lead_data.get('id'),
|
||||
datetime.now(),
|
||||
received_at,
|
||||
lead_data.get('company'),
|
||||
lead_data.get('contact'),
|
||||
lead_data.get('email'),
|
||||
lead_data.get('phone'),
|
||||
lead_data.get('raw_body'),
|
||||
'new'
|
||||
json.dumps(meta),
|
||||
'new',
|
||||
lead_data.get('source') # Added source
|
||||
))
|
||||
conn.commit()
|
||||
return True
|
||||
@@ -64,6 +97,14 @@ def insert_lead(lead_data):
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def update_lead_metadata(lead_id, meta_data):
|
||||
"""Helper to update metadata for existing leads (repair)"""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('UPDATE leads SET lead_metadata = ? WHERE id = ?', (json.dumps(meta_data), lead_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def get_leads():
|
||||
if not os.path.exists(DB_PATH):
|
||||
init_db()
|
||||
@@ -85,3 +126,19 @@ def update_lead_status(lead_id, status, response_draft=None):
|
||||
c.execute('UPDATE leads SET status = ? WHERE id = ?', (status, lead_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def update_lead_draft(lead_id, draft_text):
|
||||
"""Saves a generated email draft to the database."""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('UPDATE leads SET response_draft = ? WHERE id = ?', (draft_text, lead_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def reset_lead(lead_id):
|
||||
"""Resets a lead to 'new' status and clears enrichment data."""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('UPDATE leads SET status = "new", enrichment_data = NULL WHERE id = ?', (lead_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user