rairo commited on
Commit
ef44260
·
verified ·
1 Parent(s): 7de721e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -7
main.py CHANGED
@@ -288,7 +288,12 @@ def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes,
288
  @app.route('/api/user/spending-overview', methods=['GET'])
289
  def get_spending_overview():
290
  try:
291
- uid = verify_token(request.headers.get('Authorization', '').split(' ')[1])
 
 
 
 
 
292
  transactions_ref = db.reference('transactions')
293
  transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
294
 
@@ -298,6 +303,7 @@ def get_spending_overview():
298
  'recent_transactions': []
299
  })
300
 
 
301
  df = pd.DataFrame.from_dict(transactions, orient='index')
302
  if df.empty:
303
  return jsonify({
@@ -305,14 +311,16 @@ def get_spending_overview():
305
  'recent_transactions': []
306
  })
307
 
308
- df.dropna(subset=['uid','total','items','date', 'receipt_number', 'timestamp','hash', 'manual_entry'], inplace=True)
 
309
  if df.empty:
310
  return jsonify({
311
  'daily_spending': [],
312
  'recent_transactions': []
313
  })
314
 
315
- df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y', errors='coerce')
 
316
  df.dropna(subset=['date'], inplace=True)
317
  if df.empty:
318
  return jsonify({
@@ -320,17 +328,24 @@ def get_spending_overview():
320
  'recent_transactions': []
321
  })
322
 
 
323
  daily_spending = df.groupby(df['date'].dt.date)['total'].sum().reset_index()
324
-
 
 
 
 
 
 
 
325
  return jsonify({
326
  'daily_spending': daily_spending.to_dict(orient='records'),
327
- 'recent_transactions': df.sort_values(by='timestamp', ascending=False)
328
- .head(10)
329
- .to_dict(orient='records')
330
  })
331
  except Exception as e:
332
  return jsonify({'error': str(e)}), 500
333
 
 
334
  # ========================================
335
  # Modified verify_admin function (now checks database is_admin flag)
336
  # ========================================
 
288
  @app.route('/api/user/spending-overview', methods=['GET'])
289
  def get_spending_overview():
290
  try:
291
+ # Get UID from auth token.
292
+ auth_header = request.headers.get('Authorization', '')
293
+ token = auth_header.split(' ')[1] if len(auth_header.split(' ')) > 1 else ''
294
+ uid = verify_token(token)
295
+
296
+ # Get transactions for this user.
297
  transactions_ref = db.reference('transactions')
298
  transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
299
 
 
303
  'recent_transactions': []
304
  })
305
 
306
+ # Create DataFrame from the transactions.
307
  df = pd.DataFrame.from_dict(transactions, orient='index')
308
  if df.empty:
309
  return jsonify({
 
311
  'recent_transactions': []
312
  })
313
 
314
+ # Drop rows missing essential fields (image_url is optional).
315
+ df.dropna(subset=['uid', 'total', 'items', 'date', 'receipt_number', 'timestamp', 'hash', 'manual_entry'], inplace=True)
316
  if df.empty:
317
  return jsonify({
318
  'daily_spending': [],
319
  'recent_transactions': []
320
  })
321
 
322
+ # Parse the date column without a fixed format (allows "Fri, 12 Jun 2020 00:00:00 GMT").
323
+ df['date'] = pd.to_datetime(df['date'], errors='coerce')
324
  df.dropna(subset=['date'], inplace=True)
325
  if df.empty:
326
  return jsonify({
 
328
  'recent_transactions': []
329
  })
330
 
331
+ # Calculate daily spending.
332
  daily_spending = df.groupby(df['date'].dt.date)['total'].sum().reset_index()
333
+ # Replace any NaN with None.
334
+ daily_spending = daily_spending.replace({np.nan: None})
335
+
336
+ # Get the 10 most recent transactions.
337
+ recent_transactions = df.sort_values(by='timestamp', ascending=False).head(10)
338
+ # Replace any NaN values (e.g. for image_url) with None.
339
+ recent_transactions = recent_transactions.replace({np.nan: None})
340
+
341
  return jsonify({
342
  'daily_spending': daily_spending.to_dict(orient='records'),
343
+ 'recent_transactions': recent_transactions.to_dict(orient='records')
 
 
344
  })
345
  except Exception as e:
346
  return jsonify({'error': str(e)}), 500
347
 
348
+
349
  # ========================================
350
  # Modified verify_admin function (now checks database is_admin flag)
351
  # ========================================