File size: 2,128 Bytes
0cebd53
 
 
 
 
 
 
 
 
cc8bcc7
 
0cebd53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5836210
0cebd53
 
 
 
 
 
0250f07
 
 
0cebd53
 
 
 
 
 
 
9f48b57
 
 
 
 
0cebd53
 
 
 
 
 
 
 
 
5836210
0cebd53
 
5836210
 
0cebd53
 
 
 
d053617
 
 
 
0cebd53
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
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