import os from dotenv import load_dotenv import requests from fastapi import FastAPI from pydantic import BaseModel load_dotenv() # Load environment variables from .env file # 🔑 CONFIG BASEROW_TOKEN = "5WioOnzQce7V6ZS2smAjCOr3TEfXNBFM" LEAD_CLIENT_TABLE_ID = 914565 # if not BASEROW_TOKEN or not TABLE_ID: # raise Exception("❌ Missing BASEROW_TOKEN or TABLE_ID in environment variables") BASE_URL = f"https://api.baserow.io/api/database/rows/table/{LEAD_CLIENT_TABLE_ID}/" HEADERS = { "Authorization": f"Token {BASEROW_TOKEN}", "Content-Type": "application/json" } # 📥 Request Model class User(BaseModel): name: str mobile: str org: str # 🔍 Check if user exists (SAFE METHOD) def check_user_exists(mobile): try: response = requests.get(BASE_URL, headers=HEADERS) # print("\n--- CHECK USER DEBUG ---") # print("STATUS:", response.status_code) # print("RESPONSE:", response.text) if response.status_code == 200: data = response.json() for row in data.get("results", []): # ✅ use field ID if str(row.get("field_7932385")) == str(mobile): return { "name": row.get("field_7932386"), "mobile": row.get("field_7932385"), "org": row.get("field_7968316") } return False except Exception as e: print("CHECK ERROR:", str(e)) return False # 💾 Save user to Baserow def save_user(name, mobile, orgId): payload = { "field_7932386": name, # ✅ name "field_7932385": str(mobile), # ✅ mobile "field_7968316": orgId # ✅ organization ID } try: response = requests.post(BASE_URL, headers=HEADERS, json=payload) # print("\n--- SAVE USER DEBUG ---") # print("PAYLOAD:", payload) # print("STATUS:", response.status_code) # print("RESPONSE:", response.text) return response.status_code in [200, 201] except Exception as e: print("SAVE ERROR:", str(e)) return False