rairo commited on
Commit
755de7d
·
verified ·
1 Parent(s): c1e4dae

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +37 -20
main.py CHANGED
@@ -235,10 +235,10 @@ def get_or_create_profile(uid: str) -> dict:
235
  user_data = ref.get()
236
 
237
  fb_user = auth.get_user(uid)
238
- email = fb_user.email or ""
239
 
240
  # Check Admin Injection
241
- is_hardcoded_admin = email in HARDCODED_ADMIN_EMAILS
242
 
243
  # If user exists, update Admin status if needed
244
  if user_data:
@@ -247,7 +247,15 @@ def get_or_create_profile(uid: str) -> dict:
247
  if is_hardcoded_admin and not user_data.get("is_admin"):
248
  patch["is_admin"] = True
249
  patch["role"] = "admin" # Force role update
 
 
250
 
 
 
 
 
 
 
251
  # Social signin patch for display name
252
  if not user_data.get("displayName") and fb_user.display_name:
253
  patch["displayName"] = fb_user.display_name
@@ -258,15 +266,18 @@ def get_or_create_profile(uid: str) -> dict:
258
  return user_data
259
 
260
  # Create new profile
261
- role = "admin" if is_hardcoded_admin else "customer" # Default to customer unless on list
 
262
 
263
  new_user_data = {
264
  "email": email,
265
  "displayName": fb_user.display_name or "",
266
- "phone": "",
267
  "city": "",
268
  "role": role,
269
  "is_admin": is_hardcoded_admin,
 
 
270
  "verificationStatus": "unverified", # unverified | pending | verified | rejected
271
  "createdAt": now_iso()
272
  }
@@ -358,17 +369,19 @@ def signup():
358
 
359
  # Admin Injection logic for Signup
360
  is_admin = False
361
- if email in HARDCODED_ADMIN_EMAILS:
362
  role = "admin"
363
  is_admin = True
364
 
365
  user_data = {
366
  "email": email,
367
  "displayName": display_name,
368
- "phone": phone,
369
  "city": city,
370
  "role": role,
371
  "is_admin": is_admin,
 
 
372
  "verificationStatus": "unverified",
373
  "createdAt": now_iso()
374
  }
@@ -420,20 +433,23 @@ def set_role_after_social_signin():
420
 
421
  # IF ADMIN via injection, LOCK role changes
422
  if user_data.get("is_admin"):
423
- return jsonify({"success": True, "uid": uid, "profile": user_data, "note": "User is Admin, role locked."}), 200
 
 
 
 
 
 
 
424
 
425
  current_role = (user_data.get("role") or "").lower().strip()
426
 
427
- # Idempotent: already same role
428
- if current_role and current_role == requested_role:
429
- updated = user_ref.get() or {}
430
- return jsonify({"success": True, "uid": uid, "profile": updated, "note": "role unchanged"}), 200
431
-
432
- # If role is empty/missing -> allow setting
433
- if not current_role:
434
  patch = {
435
  "role": requested_role,
436
- "roleSetAt": now_iso(),
 
437
  "updatedAt": now_iso(),
438
  }
439
  user_ref.update(patch)
@@ -451,6 +467,7 @@ def set_role_after_social_signin():
451
  patch = {
452
  "role": "tasker",
453
  "roleUpgradedAt": now_iso(),
 
454
  "updatedAt": now_iso(),
455
  }
456
  user_ref.update(patch)
@@ -500,7 +517,7 @@ def update_user_profile():
500
  allowed = {}
501
 
502
  # Common fields
503
- for key in ["displayName", "phone", "city"]:
504
  if key in data:
505
  allowed[key] = data.get(key)
506
 
@@ -1190,9 +1207,9 @@ def submit_bid(task_id):
1190
  return jsonify({"error": "Task not open for bids"}), 400
1191
 
1192
  data = request.get_json() or {}
1193
- price = (data.get("price") or "").strip()
1194
- timeline = (data.get("timeline") or "").strip()
1195
- message = (data.get("message") or "").strip()
1196
 
1197
  if not price or not timeline:
1198
  return jsonify({"error": "price and timeline are required"}), 400
@@ -1670,4 +1687,4 @@ def admin_verify_user(target_uid):
1670
  # -----------------------------------------------------------------------------
1671
 
1672
  if __name__ == "__main__":
1673
- app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
 
235
  user_data = ref.get()
236
 
237
  fb_user = auth.get_user(uid)
238
+ email = (fb_user.email or "").lower()
239
 
240
  # Check Admin Injection
241
+ is_hardcoded_admin = email in [e.lower() for e in HARDCODED_ADMIN_EMAILS]
242
 
243
  # If user exists, update Admin status if needed
244
  if user_data:
 
247
  if is_hardcoded_admin and not user_data.get("is_admin"):
248
  patch["is_admin"] = True
249
  patch["role"] = "admin" # Force role update
250
+ patch["onboardingComplete"] = True
251
+ patch["roleSetAt"] = user_data.get("roleSetAt") or now_iso()
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
+ patch["onboardingComplete"] = True
256
+ if not user_data.get("roleSetAt"):
257
+ patch["roleSetAt"] = user_data.get("createdAt") or now_iso()
258
+
259
  # Social signin patch for display name
260
  if not user_data.get("displayName") and fb_user.display_name:
261
  patch["displayName"] = fb_user.display_name
 
266
  return user_data
267
 
268
  # Create new profile
269
+ role = "admin" if is_hardcoded_admin else "" # Empty role triggers onboarding for non-admins
270
+ onboarding_complete = True if is_hardcoded_admin else False
271
 
272
  new_user_data = {
273
  "email": email,
274
  "displayName": fb_user.display_name or "",
275
+ "phone_number": "",
276
  "city": "",
277
  "role": role,
278
  "is_admin": is_hardcoded_admin,
279
+ "onboardingComplete": onboarding_complete,
280
+ "roleSetAt": now_iso() if is_hardcoded_admin else None,
281
  "verificationStatus": "unverified", # unverified | pending | verified | rejected
282
  "createdAt": now_iso()
283
  }
 
369
 
370
  # Admin Injection logic for Signup
371
  is_admin = False
372
+ if email.lower() in [e.lower() for e in HARDCODED_ADMIN_EMAILS]:
373
  role = "admin"
374
  is_admin = True
375
 
376
  user_data = {
377
  "email": email,
378
  "displayName": display_name,
379
+ "phone_number": phone,
380
  "city": city,
381
  "role": role,
382
  "is_admin": is_admin,
383
+ "onboardingComplete": True,
384
+ "roleSetAt": now_iso(),
385
  "verificationStatus": "unverified",
386
  "createdAt": now_iso()
387
  }
 
433
 
434
  # IF ADMIN via injection, LOCK role changes
435
  if user_data.get("is_admin"):
436
+ patch = {
437
+ "onboardingComplete": True,
438
+ "roleSetAt": user_data.get("roleSetAt") or now_iso(),
439
+ "updatedAt": now_iso()
440
+ }
441
+ user_ref.update(patch)
442
+ updated = user_ref.get()
443
+ return jsonify({"success": True, "uid": uid, "profile": updated, "note": "User is Admin, role locked."}), 200
444
 
445
  current_role = (user_data.get("role") or "").lower().strip()
446
 
447
+ # Idempotent: already same role OR role was empty (first selection)
448
+ if not current_role or current_role == requested_role:
 
 
 
 
 
449
  patch = {
450
  "role": requested_role,
451
+ "roleSetAt": user_data.get("roleSetAt") or now_iso(),
452
+ "onboardingComplete": True,
453
  "updatedAt": now_iso(),
454
  }
455
  user_ref.update(patch)
 
467
  patch = {
468
  "role": "tasker",
469
  "roleUpgradedAt": now_iso(),
470
+ "onboardingComplete": True,
471
  "updatedAt": now_iso(),
472
  }
473
  user_ref.update(patch)
 
517
  allowed = {}
518
 
519
  # Common fields
520
+ for key in ["displayName", "phone_number", "city"]:
521
  if key in data:
522
  allowed[key] = data.get(key)
523
 
 
1207
  return jsonify({"error": "Task not open for bids"}), 400
1208
 
1209
  data = request.get_json() or {}
1210
+ price = str(data.get("price") or "").strip()
1211
+ timeline = str(data.get("timeline") or "").strip()
1212
+ message = str(data.get("message") or "").strip()
1213
 
1214
  if not price or not timeline:
1215
  return jsonify({"error": "price and timeline are required"}), 400
 
1687
  # -----------------------------------------------------------------------------
1688
 
1689
  if __name__ == "__main__":
1690
+ app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))