habulaj commited on
Commit
33b9207
·
verified ·
1 Parent(s): 7ea385a

Update routes/stylist.py

Browse files
Files changed (1) hide show
  1. routes/stylist.py +67 -0
routes/stylist.py CHANGED
@@ -357,6 +357,69 @@ def get_courtesy_consultations(user_id: str) -> Dict[str, Any]:
357
  "monthly_courtesy_consultations": list(monthly_data.values())
358
  }
359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  @router.post("/bank_account_dashboard_link")
361
  async def generate_bank_account_dashboard_link(data: UserIDRequest):
362
  try:
@@ -407,6 +470,9 @@ def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int =
407
  # Obter dados de consultas gratuitas dos últimos 6 meses
408
  courtesy_data = get_courtesy_consultations(user_id)
409
 
 
 
 
410
  # Obter detalhes da conta conectada
411
  account_details = get_connected_account_details(stripe_id)
412
 
@@ -419,6 +485,7 @@ def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int =
419
  "monthly_revenue": revenue_data["monthly_data"],
420
  "total_revenue_last_6_months": revenue_data["total_last_6_months"],
421
  "monthly_courtesy_consultations": courtesy_data["monthly_courtesy_consultations"],
 
422
  "total_followers": get_total_followers(user_id),
423
  "total_subscribers": get_total_subscribers(user_id),
424
  "account_details": account_details,
 
357
  "monthly_courtesy_consultations": list(monthly_data.values())
358
  }
359
 
360
+ def get_monthly_likes(user_id: str) -> Dict[str, Any]:
361
+ ny_timezone = pytz.timezone('America/New_York')
362
+ now_ny = datetime.now(ny_timezone)
363
+ monthly_data = {}
364
+
365
+ # Inicializar dados para os últimos 6 meses
366
+ for i in range(6):
367
+ target_date = now_ny - relativedelta(months=i)
368
+ month_num = target_date.month
369
+ month_name = target_date.strftime('%b')
370
+ year = target_date.year
371
+ month_key = f"{year}-{month_num}"
372
+ monthly_data[month_key] = {
373
+ "month": month_num,
374
+ "name": month_name,
375
+ "current": (month_num == now_ny.month and year == now_ny.year),
376
+ "likes_count": 0
377
+ }
378
+
379
+ # Calcular data de início (6 meses atrás)
380
+ start_date = (now_ny - relativedelta(months=6)).strftime('%Y-%m-%d')
381
+
382
+ try:
383
+ # Primeiro precisamos obter os itens do feed deste usuário
384
+ feed_url = f"{SUPABASE_URL}/rest/v1/feed?user_id=eq.{user_id}"
385
+ feed_response = requests.get(feed_url, headers=SUPABASE_HEADERS)
386
+
387
+ if feed_response.status_code == 200:
388
+ feed_items = feed_response.json()
389
+ feed_item_ids = [item.get("id") for item in feed_items]
390
+
391
+ if feed_item_ids:
392
+ # Agora buscar likes para esses itens de feed
393
+ # Usando o operador "in" do Supabase para buscar múltiplos IDs
394
+ likes_url = f"{SUPABASE_URL}/rest/v1/likes?feed_item_id=in.({','.join(map(str, feed_item_ids))})&created_at=gte.{start_date}"
395
+ likes_response = requests.get(likes_url, headers=SUPABASE_HEADERS)
396
+
397
+ if likes_response.status_code == 200:
398
+ likes = likes_response.json()
399
+
400
+ # Contar likes por mês
401
+ for like in likes:
402
+ # Converter a data para o fuso horário de NY
403
+ like_date_str = like.get("created_at")
404
+ like_date = datetime.fromisoformat(like_date_str.replace('Z', '+00:00')).astimezone(ny_timezone)
405
+
406
+ month_key = f"{like_date.year}-{like_date.month}"
407
+ if month_key in monthly_data:
408
+ monthly_data[month_key]["likes_count"] += 1
409
+
410
+ # Convertendo para lista e ordenando por mês (do mais recente para o mais antigo)
411
+ result = list(monthly_data.values())
412
+ result.sort(key=lambda x: (now_ny.year * 12 + now_ny.month - (x["month"])) % 12)
413
+
414
+ return {
415
+ "monthly_likes": result
416
+ }
417
+ except Exception as e:
418
+ logger.error(f"❌ Error getting monthly likes: {str(e)}")
419
+ return {
420
+ "monthly_likes": list(monthly_data.values())
421
+ }
422
+
423
  @router.post("/bank_account_dashboard_link")
424
  async def generate_bank_account_dashboard_link(data: UserIDRequest):
425
  try:
 
470
  # Obter dados de consultas gratuitas dos últimos 6 meses
471
  courtesy_data = get_courtesy_consultations(user_id)
472
 
473
+ # Obter dados de likes dos últimos 6 meses
474
+ likes_data = get_monthly_likes(user_id)
475
+
476
  # Obter detalhes da conta conectada
477
  account_details = get_connected_account_details(stripe_id)
478
 
 
485
  "monthly_revenue": revenue_data["monthly_data"],
486
  "total_revenue_last_6_months": revenue_data["total_last_6_months"],
487
  "monthly_courtesy_consultations": courtesy_data["monthly_courtesy_consultations"],
488
+ "monthly_likes": likes_data["monthly_likes"],
489
  "total_followers": get_total_followers(user_id),
490
  "total_subscribers": get_total_subscribers(user_id),
491
  "account_details": account_details,