Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -243,16 +243,23 @@ def get_or_create_profile(uid: str) -> dict:
|
|
| 243 |
# If user exists, update Admin status if needed
|
| 244 |
if user_data:
|
| 245 |
patch = {}
|
| 246 |
-
# If they are on the list
|
| 247 |
-
if is_hardcoded_admin
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 252 |
|
| 253 |
# FIX: Ensure onboardingComplete is true if role is already set
|
| 254 |
if user_data.get("role") and not user_data.get("onboardingComplete"):
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
if not user_data.get("roleSetAt"):
|
| 257 |
patch["roleSetAt"] = user_data.get("createdAt") or now_iso()
|
| 258 |
|
|
@@ -282,7 +289,7 @@ def get_or_create_profile(uid: str) -> dict:
|
|
| 282 |
"createdAt": now_iso()
|
| 283 |
}
|
| 284 |
ref.set(new_user_data)
|
| 285 |
-
return new_user_data
|
| 286 |
|
| 287 |
|
| 288 |
def push_notification(to_uid: str, notif_type: str, title: str, body: str, meta: dict | None = None):
|
|
@@ -444,41 +451,45 @@ def set_role_after_social_signin():
|
|
| 444 |
|
| 445 |
current_role = (user_data.get("role") or "").lower().strip()
|
| 446 |
|
| 447 |
-
#
|
| 448 |
-
if
|
| 449 |
patch = {
|
| 450 |
-
"role":
|
| 451 |
-
"roleSetAt": user_data.get("roleSetAt") or now_iso(),
|
| 452 |
"onboardingComplete": True,
|
| 453 |
"updatedAt": now_iso(),
|
| 454 |
}
|
|
|
|
|
|
|
|
|
|
| 455 |
user_ref.update(patch)
|
| 456 |
updated = user_ref.get() or {}
|
| 457 |
return jsonify({"success": True, "uid": uid, "profile": updated}), 200
|
| 458 |
|
| 459 |
-
#
|
| 460 |
-
if
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
"error": "Role change blocked",
|
| 464 |
-
"reason": "Customer -> Tasker upgrade already used.",
|
| 465 |
-
}), 409
|
| 466 |
-
|
| 467 |
patch = {
|
| 468 |
"role": "tasker",
|
| 469 |
-
"roleUpgradedAt": now_iso(),
|
| 470 |
"onboardingComplete": True,
|
| 471 |
"updatedAt": now_iso(),
|
| 472 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
user_ref.update(patch)
|
| 474 |
updated = user_ref.get() or {}
|
| 475 |
-
return jsonify({"success": True, "uid": uid, "profile": updated
|
| 476 |
|
| 477 |
-
# Block any other change
|
| 478 |
return jsonify({
|
| 479 |
"error": "Role change blocked",
|
| 480 |
-
"reason": "
|
| 481 |
-
}),
|
| 482 |
|
| 483 |
except Exception as e:
|
| 484 |
logger.error(f"[SET ROLE] failed: {e}")
|
|
@@ -1687,4 +1698,4 @@ def admin_verify_user(target_uid):
|
|
| 1687 |
# -----------------------------------------------------------------------------
|
| 1688 |
|
| 1689 |
if __name__ == "__main__":
|
| 1690 |
-
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 243 |
# If user exists, update Admin status if needed
|
| 244 |
if user_data:
|
| 245 |
patch = {}
|
| 246 |
+
# If they are on the list, ensure they are admin
|
| 247 |
+
if is_hardcoded_admin:
|
| 248 |
+
if not user_data.get("is_admin") or user_data.get("role") != "admin":
|
| 249 |
+
patch["is_admin"] = True
|
| 250 |
+
patch["role"] = "admin"
|
| 251 |
+
patch["onboardingComplete"] = True
|
| 252 |
+
patch["roleSetAt"] = user_data.get("roleSetAt") or now_iso()
|
| 253 |
|
| 254 |
# FIX: Ensure onboardingComplete is true if role is already set
|
| 255 |
if user_data.get("role") and not user_data.get("onboardingComplete"):
|
| 256 |
+
# Admins always complete onboarding
|
| 257 |
+
if user_data.get("role") == "admin" or is_hardcoded_admin:
|
| 258 |
+
patch["onboardingComplete"] = True
|
| 259 |
+
else:
|
| 260 |
+
# For others, only if they have a non-empty role
|
| 261 |
+
patch["onboardingComplete"] = True
|
| 262 |
+
|
| 263 |
if not user_data.get("roleSetAt"):
|
| 264 |
patch["roleSetAt"] = user_data.get("createdAt") or now_iso()
|
| 265 |
|
|
|
|
| 289 |
"createdAt": now_iso()
|
| 290 |
}
|
| 291 |
ref.set(new_user_data)
|
| 292 |
+
return new_user_data
|
| 293 |
|
| 294 |
|
| 295 |
def push_notification(to_uid: str, notif_type: str, title: str, body: str, meta: dict | None = None):
|
|
|
|
| 451 |
|
| 452 |
current_role = (user_data.get("role") or "").lower().strip()
|
| 453 |
|
| 454 |
+
# Allow switching to customer anytime
|
| 455 |
+
if requested_role == "customer":
|
| 456 |
patch = {
|
| 457 |
+
"role": "customer",
|
|
|
|
| 458 |
"onboardingComplete": True,
|
| 459 |
"updatedAt": now_iso(),
|
| 460 |
}
|
| 461 |
+
if not user_data.get("roleSetAt"):
|
| 462 |
+
patch["roleSetAt"] = now_iso()
|
| 463 |
+
|
| 464 |
user_ref.update(patch)
|
| 465 |
updated = user_ref.get() or {}
|
| 466 |
return jsonify({"success": True, "uid": uid, "profile": updated}), 200
|
| 467 |
|
| 468 |
+
# Allow switching to tasker
|
| 469 |
+
if requested_role == "tasker":
|
| 470 |
+
# If they are already verified, just let them switch
|
| 471 |
+
# If not, they can still switch but the UI will show them as pending/restricted
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
patch = {
|
| 473 |
"role": "tasker",
|
|
|
|
| 474 |
"onboardingComplete": True,
|
| 475 |
"updatedAt": now_iso(),
|
| 476 |
}
|
| 477 |
+
if not user_data.get("roleSetAt"):
|
| 478 |
+
patch["roleSetAt"] = now_iso()
|
| 479 |
+
|
| 480 |
+
# Record when they first became/requested to be a tasker
|
| 481 |
+
if current_role != "tasker" and not user_data.get("roleUpgradedAt"):
|
| 482 |
+
patch["roleUpgradedAt"] = now_iso()
|
| 483 |
+
|
| 484 |
user_ref.update(patch)
|
| 485 |
updated = user_ref.get() or {}
|
| 486 |
+
return jsonify({"success": True, "uid": uid, "profile": updated}), 200
|
| 487 |
|
| 488 |
+
# Block any other change (like trying to set role to admin)
|
| 489 |
return jsonify({
|
| 490 |
"error": "Role change blocked",
|
| 491 |
+
"reason": "Invalid role requested.",
|
| 492 |
+
}), 400
|
| 493 |
|
| 494 |
except Exception as e:
|
| 495 |
logger.error(f"[SET ROLE] failed: {e}")
|
|
|
|
| 1698 |
# -----------------------------------------------------------------------------
|
| 1699 |
|
| 1700 |
if __name__ == "__main__":
|
| 1701 |
+
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|