Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -450,33 +450,59 @@ def admin_get_pending_roles():
|
|
| 450 |
return jsonify(pending_apps or {}), 200
|
| 451 |
except Exception as e: return handle_route_errors(e, uid_context=admin_uid)
|
| 452 |
|
| 453 |
-
@app.route('/api/admin/roles/
|
| 454 |
-
def admin_action_on_role(application_id):
|
| 455 |
auth_header = request.headers.get('Authorization')
|
| 456 |
admin_uid = None
|
| 457 |
try:
|
| 458 |
admin_uid = verify_admin(auth_header)
|
| 459 |
-
if not FIREBASE_INITIALIZED:
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
user_profile_ref = db.reference(f'users/{user_id}', app=db_app)
|
| 466 |
update_time = datetime.now(timezone.utc).isoformat()
|
| 467 |
message_text = ""
|
|
|
|
| 468 |
if action == 'approve':
|
| 469 |
-
app_ref.update({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
user_profile_ref.child('roles').update({role: True})
|
| 471 |
user_profile_ref.child('role_applications').update({role: 'approved'})
|
| 472 |
message_text = f"Role {role} for user {user_id} approved."
|
| 473 |
-
else:
|
| 474 |
-
app_ref.update({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
user_profile_ref.child('role_applications').update({role: 'rejected'})
|
| 476 |
message_text = f"Role {role} for user {user_id} rejected."
|
| 477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
return jsonify({'success': True, 'message': message_text}), 200
|
| 479 |
-
|
|
|
|
|
|
|
| 480 |
|
| 481 |
#--- Marketplace Endpoints ---
|
| 482 |
def _create_listing(uid_lister, listing_type, data):
|
|
|
|
| 450 |
return jsonify(pending_apps or {}), 200
|
| 451 |
except Exception as e: return handle_route_errors(e, uid_context=admin_uid)
|
| 452 |
|
| 453 |
+
@app.route('/api/admin/roles/<application_id>/<action>', methods=['POST'])
|
| 454 |
+
def admin_action_on_role(application_id, action):
|
| 455 |
auth_header = request.headers.get('Authorization')
|
| 456 |
admin_uid = None
|
| 457 |
try:
|
| 458 |
admin_uid = verify_admin(auth_header)
|
| 459 |
+
if not FIREBASE_INITIALIZED:
|
| 460 |
+
return jsonify({'error': 'Server configuration error: Firebase not ready.'}), 503
|
| 461 |
+
|
| 462 |
+
if action not in ['approve', 'reject']:
|
| 463 |
+
return jsonify({'error': 'Invalid action.'}), 400
|
| 464 |
+
|
| 465 |
+
app_ref = db.reference(f'role_applications/{application_id}', app=db_app)
|
| 466 |
+
application = app_ref.get()
|
| 467 |
+
|
| 468 |
+
if not application or application.get('status') != 'pending':
|
| 469 |
+
return jsonify({'error': 'App not found or not pending.'}), 404
|
| 470 |
+
|
| 471 |
+
user_id = application['user_id']
|
| 472 |
+
role = application['role']
|
| 473 |
user_profile_ref = db.reference(f'users/{user_id}', app=db_app)
|
| 474 |
update_time = datetime.now(timezone.utc).isoformat()
|
| 475 |
message_text = ""
|
| 476 |
+
|
| 477 |
if action == 'approve':
|
| 478 |
+
app_ref.update({
|
| 479 |
+
'status': 'approved',
|
| 480 |
+
'reviewed_by': admin_uid,
|
| 481 |
+
'reviewed_at': update_time
|
| 482 |
+
})
|
| 483 |
user_profile_ref.child('roles').update({role: True})
|
| 484 |
user_profile_ref.child('role_applications').update({role: 'approved'})
|
| 485 |
message_text = f"Role {role} for user {user_id} approved."
|
| 486 |
+
else: # reject
|
| 487 |
+
app_ref.update({
|
| 488 |
+
'status': 'rejected',
|
| 489 |
+
'reviewed_by': admin_uid,
|
| 490 |
+
'reviewed_at': update_time
|
| 491 |
+
})
|
| 492 |
user_profile_ref.child('role_applications').update({role: 'rejected'})
|
| 493 |
message_text = f"Role {role} for user {user_id} rejected."
|
| 494 |
+
|
| 495 |
+
_send_system_notification(
|
| 496 |
+
user_id,
|
| 497 |
+
f"Your application for the '{role}' role has been {action}d.",
|
| 498 |
+
"role_status",
|
| 499 |
+
f"/profile/roles"
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
return jsonify({'success': True, 'message': message_text}), 200
|
| 503 |
+
|
| 504 |
+
except Exception as e:
|
| 505 |
+
return handle_route_errors(e, uid_context=admin_uid)
|
| 506 |
|
| 507 |
#--- Marketplace Endpoints ---
|
| 508 |
def _create_listing(uid_lister, listing_type, data):
|