Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -387,26 +387,56 @@ def update_user_limit(uid):
|
|
| 387 |
|
| 388 |
|
| 389 |
# User management endpoint for profile
|
| 390 |
-
@app.route('/api/user/
|
| 391 |
-
def
|
| 392 |
try:
|
| 393 |
uid = verify_token(request.headers.get('Authorization', '').split(' ')[1])
|
| 394 |
-
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
return jsonify({
|
| 398 |
-
'
|
| 399 |
-
'
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
'last_reset': user_data.get('last_reset'),
|
| 403 |
-
'is_admin': user_data.get('is_admin', False)
|
| 404 |
})
|
| 405 |
except Exception as e:
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
user_data = db.reference(f'users/{uid}').get()
|
| 409 |
-
return jsonify({'error': str(e)+ f'user data: {user_data}'}), 500
|
| 410 |
|
| 411 |
# Receipt media endpoints
|
| 412 |
|
|
|
|
| 387 |
|
| 388 |
|
| 389 |
# User management endpoint for profile
|
| 390 |
+
@app.route('/api/user/spending-overview', methods=['GET'])
|
| 391 |
+
def get_spending_overview():
|
| 392 |
try:
|
| 393 |
uid = verify_token(request.headers.get('Authorization', '').split(' ')[1])
|
| 394 |
+
transactions_ref = db.reference('transactions')
|
| 395 |
+
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
|
| 396 |
+
|
| 397 |
+
if transactions is None:
|
| 398 |
+
return jsonify({
|
| 399 |
+
'daily_spending': [],
|
| 400 |
+
'recent_transactions': []
|
| 401 |
+
})
|
| 402 |
+
|
| 403 |
+
# Create dataframe
|
| 404 |
+
df = pd.DataFrame.from_dict(transactions, orient='index')
|
| 405 |
+
|
| 406 |
+
# Check if dataframe is empty
|
| 407 |
+
if df.empty:
|
| 408 |
+
return jsonify({
|
| 409 |
+
'daily_spending': [],
|
| 410 |
+
'recent_transactions': []
|
| 411 |
+
})
|
| 412 |
+
|
| 413 |
+
# Check if all the data is here, otherwise we delete the data.
|
| 414 |
+
df.dropna(subset=['uid','total','items','date', 'receipt_number', 'timestamp','hash', 'manual_entry'], inplace=True)
|
| 415 |
+
if df.empty:
|
| 416 |
+
return jsonify({
|
| 417 |
+
'daily_spending': [],
|
| 418 |
+
'recent_transactions': []
|
| 419 |
+
})
|
| 420 |
|
| 421 |
+
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y', errors='coerce')
|
| 422 |
+
df.dropna(subset=['date'], inplace=True)
|
| 423 |
+
if df.empty:
|
| 424 |
+
return jsonify({
|
| 425 |
+
'daily_spending': [],
|
| 426 |
+
'recent_transactions': []
|
| 427 |
+
})
|
| 428 |
+
|
| 429 |
+
daily_spending = df.groupby(df['date'].dt.date)['total'].sum().reset_index()
|
| 430 |
+
|
| 431 |
return jsonify({
|
| 432 |
+
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 433 |
+
'recent_transactions': df.sort_values(by='timestamp', ascending=False)
|
| 434 |
+
.head(10)
|
| 435 |
+
.to_dict(orient='records')
|
|
|
|
|
|
|
| 436 |
})
|
| 437 |
except Exception as e:
|
| 438 |
+
return jsonify({'error': str(e)}), 500
|
| 439 |
+
|
|
|
|
|
|
|
| 440 |
|
| 441 |
# Receipt media endpoints
|
| 442 |
|