Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,6 @@ import logging
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
from flask_cors import CORS
|
| 7 |
-
|
| 8 |
import firebase_admin
|
| 9 |
from firebase_admin import credentials, auth, firestore
|
| 10 |
|
|
@@ -178,12 +177,16 @@ def get_user_profile():
|
|
| 178 |
|
| 179 |
return jsonify({'uid': uid, **user_doc.to_dict()})
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
@app.route('/api/user/profile/phone', methods=['PUT'])
|
| 182 |
def update_user_phone():
|
| 183 |
"""
|
| 184 |
Allows a user to submit their WhatsApp phone number for admin approval.
|
| 185 |
-
**
|
| 186 |
-
|
| 187 |
"""
|
| 188 |
uid = verify_token(request.headers.get('Authorization'))
|
| 189 |
if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
|
|
@@ -197,16 +200,13 @@ def update_user_phone():
|
|
| 197 |
phone_number_stripped = phone_number.strip()
|
| 198 |
|
| 199 |
try:
|
| 200 |
-
# Validate
|
| 201 |
existing_user_query = db.collection('users').where('phone', '==', phone_number_stripped).limit(1).stream()
|
| 202 |
-
|
| 203 |
conflicting_users = list(existing_user_query)
|
| 204 |
-
if len(conflicting_users) > 0:
|
| 205 |
-
|
| 206 |
-
logger.warning(f"User {uid} tried to claim phone number {phone_number_stripped}, but it's already in use.")
|
| 207 |
-
return jsonify({'error': 'This phone number is already registered to another account.'}), 409
|
| 208 |
|
| 209 |
-
#
|
| 210 |
user_ref = db.collection('users').document(uid)
|
| 211 |
user_ref.update({
|
| 212 |
'phone': phone_number_stripped,
|
|
@@ -216,17 +216,24 @@ def update_user_phone():
|
|
| 216 |
logging.info(f"User {uid} submitted phone number {phone_number_stripped} for approval.")
|
| 217 |
|
| 218 |
# --- THE FIX IS HERE ---
|
| 219 |
-
# Re-fetch the updated user document
|
| 220 |
updated_user_doc = user_ref.get()
|
| 221 |
if not updated_user_doc.exists:
|
| 222 |
return jsonify({'error': 'Failed to retrieve updated profile.'}), 500
|
| 223 |
|
| 224 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
return jsonify({
|
| 226 |
'success': True,
|
| 227 |
'message': 'Phone number submitted for approval.',
|
| 228 |
'uid': uid,
|
| 229 |
-
**
|
| 230 |
}), 200
|
| 231 |
# --- END OF FIX ---
|
| 232 |
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
from flask_cors import CORS
|
|
|
|
| 7 |
import firebase_admin
|
| 8 |
from firebase_admin import credentials, auth, firestore
|
| 9 |
|
|
|
|
| 177 |
|
| 178 |
return jsonify({'uid': uid, **user_doc.to_dict()})
|
| 179 |
|
| 180 |
+
# dashboard_server.py
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
@app.route('/api/user/profile/phone', methods=['PUT'])
|
| 185 |
def update_user_phone():
|
| 186 |
"""
|
| 187 |
Allows a user to submit their WhatsApp phone number for admin approval.
|
| 188 |
+
**DEFINITIVE FIX**: Correctly handles the `datetime` object returned from Firestore,
|
| 189 |
+
preventing the JSON serialization error that causes the MutationFn to fail.
|
| 190 |
"""
|
| 191 |
uid = verify_token(request.headers.get('Authorization'))
|
| 192 |
if not uid: return jsonify({'error': 'Invalid or expired token'}), 401
|
|
|
|
| 200 |
phone_number_stripped = phone_number.strip()
|
| 201 |
|
| 202 |
try:
|
| 203 |
+
# Validate uniqueness
|
| 204 |
existing_user_query = db.collection('users').where('phone', '==', phone_number_stripped).limit(1).stream()
|
|
|
|
| 205 |
conflicting_users = list(existing_user_query)
|
| 206 |
+
if len(conflicting_users) > 0 and conflicting_users[0].id != uid:
|
| 207 |
+
return jsonify({'error': 'This phone number is already registered to another account.'}), 409
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
# Update the user's profile
|
| 210 |
user_ref = db.collection('users').document(uid)
|
| 211 |
user_ref.update({
|
| 212 |
'phone': phone_number_stripped,
|
|
|
|
| 216 |
logging.info(f"User {uid} submitted phone number {phone_number_stripped} for approval.")
|
| 217 |
|
| 218 |
# --- THE FIX IS HERE ---
|
| 219 |
+
# 1. Re-fetch the updated user document
|
| 220 |
updated_user_doc = user_ref.get()
|
| 221 |
if not updated_user_doc.exists:
|
| 222 |
return jsonify({'error': 'Failed to retrieve updated profile.'}), 500
|
| 223 |
|
| 224 |
+
# 2. Convert the document to a dictionary
|
| 225 |
+
user_data = updated_user_doc.to_dict()
|
| 226 |
+
|
| 227 |
+
# 3. IMPORTANT: Check for and convert any datetime objects to strings
|
| 228 |
+
if 'createdAt' in user_data and isinstance(user_data['createdAt'], datetime):
|
| 229 |
+
user_data['createdAt'] = user_data['createdAt'].isoformat() + "Z"
|
| 230 |
+
|
| 231 |
+
# 4. Return the now JSON-safe data, with the correct top-level structure
|
| 232 |
return jsonify({
|
| 233 |
'success': True,
|
| 234 |
'message': 'Phone number submitted for approval.',
|
| 235 |
'uid': uid,
|
| 236 |
+
**user_data
|
| 237 |
}), 200
|
| 238 |
# --- END OF FIX ---
|
| 239 |
|