zenaight commited on
Commit ·
829620f
1
Parent(s): 6beacc7
Implement user persona creation and update logic in database
Browse files- Enhanced `update_user_persona` function to first check for an existing persona before updating.
- If no persona exists, a new record is created with the provided updates and a timestamp.
- This change improves data integrity by ensuring that user personas are accurately maintained in the database.
- database.py +20 -5
database.py
CHANGED
|
@@ -214,11 +214,26 @@ async def update_user_persona(wa_id: str, updates: dict):
|
|
| 214 |
|
| 215 |
try:
|
| 216 |
updates_with_ts = {**updates, "updated_at": datetime.utcnow().isoformat()}
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
except Exception as e:
|
| 223 |
print(f"Error updating user persona: {e}")
|
| 224 |
|
|
|
|
| 214 |
|
| 215 |
try:
|
| 216 |
updates_with_ts = {**updates, "updated_at": datetime.utcnow().isoformat()}
|
| 217 |
+
|
| 218 |
+
# First try to get existing persona
|
| 219 |
+
existing = supabase.table("personas").select("*").eq("wa_id", wa_id).execute()
|
| 220 |
+
|
| 221 |
+
if existing.data:
|
| 222 |
+
# Update existing record
|
| 223 |
+
supabase\
|
| 224 |
+
.table("personas")\
|
| 225 |
+
.update(updates_with_ts)\
|
| 226 |
+
.eq("wa_id", wa_id)\
|
| 227 |
+
.execute()
|
| 228 |
+
else:
|
| 229 |
+
# Create new record
|
| 230 |
+
new_persona = {
|
| 231 |
+
"wa_id": wa_id,
|
| 232 |
+
"created_at": datetime.utcnow().isoformat(),
|
| 233 |
+
**updates_with_ts
|
| 234 |
+
}
|
| 235 |
+
supabase.table("personas").insert(new_persona).execute()
|
| 236 |
+
|
| 237 |
except Exception as e:
|
| 238 |
print(f"Error updating user persona: {e}")
|
| 239 |
|