rairo commited on
Commit
d8db4e7
·
verified ·
1 Parent(s): eb4f11c

Update utility_tunasonga.py

Browse files
Files changed (1) hide show
  1. utility_tunasonga.py +33 -51
utility_tunasonga.py CHANGED
@@ -111,67 +111,49 @@ def get_user_by_phone(phone_number: str, app_instance: Any) -> Optional[Dict[str
111
  try:
112
  users_ref = db.reference('users', app=app_instance)
113
 
114
- # DEBUG: Get all users to see what phone numbers exist
115
- all_users = users_ref.get()
116
- if all_users:
117
- logger.info("=== DEBUG: All phone numbers in database ===")
118
- for uid, user_data in all_users.items():
119
- if isinstance(user_data, dict) and 'phone_number' in user_data:
120
- stored_phone = user_data['phone_number']
121
- logger.info(f"User {uid}: phone_number = '{stored_phone}' (type: {type(stored_phone)})")
122
- logger.info("=== END DEBUG ===")
123
-
124
  # Normalize the input phone number by removing '+' if present
125
  normalized_input = phone_number.lstrip('+')
126
- logger.info(f"Input phone: '{phone_number}' -> Normalized: '{normalized_input}'")
127
 
128
- # First, try with '+' prefix since your database stores numbers with '+'
129
- # This handles the case where database stores "+263..." and WhatsApp sends "263..."
130
- prefixed_number = '+' + normalized_input
131
- logger.info(f"Trying query 1 with: '{prefixed_number}' (len={len(prefixed_number)})")
132
- query = users_ref.order_by_child('phone_number').equal_to(prefixed_number).limit_to_first(1)
133
- snapshot = query.get()
134
- logger.info(f"Query 1 result: {snapshot}")
135
- logger.info(f"Query 1 result type: {type(snapshot)}, bool: {bool(snapshot)}")
 
 
 
 
136
 
137
- # Let's also try a manual search to see if Firebase indexing is the issue
138
- logger.info("=== Manual search through all users ===")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  all_users = users_ref.get()
140
  if all_users:
141
  for uid, user_data in all_users.items():
142
  if isinstance(user_data, dict) and 'phone_number' in user_data:
143
  stored_phone = user_data['phone_number']
144
- if stored_phone == prefixed_number:
145
- logger.info(f"MANUAL MATCH FOUND! User {uid} has phone '{stored_phone}' matching '{prefixed_number}'")
146
- # Return this user manually
147
  user_data['uid'] = uid
148
  return user_data
149
- logger.info("=== End manual search ===")
150
-
151
- # If not found, try without '+' prefix
152
- # This handles the case where database might store "263..." but input has "+"
153
- if not snapshot:
154
- logger.info(f"Trying query 2 with: '{normalized_input}'")
155
- query = users_ref.order_by_child('phone_number').equal_to(normalized_input).limit_to_first(1)
156
- snapshot = query.get()
157
- logger.info(f"Query 2 result: {snapshot}")
158
-
159
- # If still not found, try the original input as-is
160
- # This is a fallback in case there are other formatting differences
161
- if not snapshot and phone_number != normalized_input and phone_number != prefixed_number:
162
- logger.info(f"Trying query 3 with original: '{phone_number}'")
163
- query = users_ref.order_by_child('phone_number').equal_to(phone_number).limit_to_first(1)
164
- snapshot = query.get()
165
- logger.info(f"Query 3 result: {snapshot}")
166
-
167
- if snapshot:
168
- for user_uid, user_data in snapshot.items():
169
- if isinstance(user_data, dict):
170
- user_data['uid'] = user_uid
171
- logger.info(f"User found for phone {phone_number}: UID {user_uid}")
172
- return user_data
173
- else:
174
- logger.warning(f"User data for phone {phone_number} (UID {user_uid}) is not a dictionary: {user_data}")
175
 
176
  logger.info(f"No user found for phone number: {phone_number}")
177
  return None
@@ -182,7 +164,7 @@ def get_user_by_phone(phone_number: str, app_instance: Any) -> Optional[Dict[str
182
  except Exception as e:
183
  logger.error(f"Unexpected error fetching user by phone {phone_number}: {e}")
184
  return None
185
-
186
  def format_listings_for_whatsapp(listings_data: Dict[str, Any], list_type_description: str = "items") -> str:
187
  if not listings_data:
188
  return f"You currently have no active {list_type_description}."
 
111
  try:
112
  users_ref = db.reference('users', app=app_instance)
113
 
 
 
 
 
 
 
 
 
 
 
114
  # Normalize the input phone number by removing '+' if present
115
  normalized_input = phone_number.lstrip('+')
 
116
 
117
+ # Create possible phone number variations to search for
118
+ search_variations = [
119
+ '+' + normalized_input, #
120
+ normalized_input, #
121
+ phone_number # Original input
122
+ ]
123
+
124
+ # Remove duplicates while preserving order
125
+ unique_variations = []
126
+ for variation in search_variations:
127
+ if variation not in unique_variations:
128
+ unique_variations.append(variation)
129
 
130
+ logger.info(f"Searching for phone variations: {unique_variations}")
131
+
132
+ # Try Firebase queries first (in case indexing gets fixed later)
133
+ for variation in unique_variations:
134
+ try:
135
+ query = users_ref.order_by_child('phone_number').equal_to(variation).limit_to_first(1)
136
+ snapshot = query.get()
137
+ if snapshot:
138
+ for user_uid, user_data in snapshot.items():
139
+ if isinstance(user_data, dict):
140
+ user_data['uid'] = user_uid
141
+ logger.info(f"User found via Firebase query for phone {phone_number}: UID {user_uid}")
142
+ return user_data
143
+ except Exception as query_error:
144
+ logger.warning(f"Firebase query failed for '{variation}': {query_error}")
145
+
146
+ # Fallback to manual search if queries fail (due to indexing issues)
147
+ logger.info("Firebase queries failed, falling back to manual search...")
148
  all_users = users_ref.get()
149
  if all_users:
150
  for uid, user_data in all_users.items():
151
  if isinstance(user_data, dict) and 'phone_number' in user_data:
152
  stored_phone = user_data['phone_number']
153
+ if stored_phone in unique_variations:
154
+ logger.info(f"User found via manual search for phone {phone_number}: UID {uid}")
 
155
  user_data['uid'] = uid
156
  return user_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  logger.info(f"No user found for phone number: {phone_number}")
159
  return None
 
164
  except Exception as e:
165
  logger.error(f"Unexpected error fetching user by phone {phone_number}: {e}")
166
  return None
167
+
168
  def format_listings_for_whatsapp(listings_data: Dict[str, Any], list_type_description: str = "items") -> str:
169
  if not listings_data:
170
  return f"You currently have no active {list_type_description}."