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

Update routes/stylist.py

Browse files
Files changed (1) hide show
  1. routes/stylist.py +57 -0
routes/stylist.py CHANGED
@@ -303,7 +303,60 @@ def get_total_subscribers(user_id: str) -> int:
303
  subscribers = response.json()
304
  return len(subscribers)
305
  return 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
  @router.post("/bank_account_dashboard_link")
308
  async def generate_bank_account_dashboard_link(data: UserIDRequest):
309
  try:
@@ -351,6 +404,9 @@ def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int =
351
  # Obter dados de receita mensal e total dos últimos 6 meses
352
  revenue_data = get_monthly_revenue(stripe_id)
353
 
 
 
 
354
  # Obter detalhes da conta conectada
355
  account_details = get_connected_account_details(stripe_id)
356
 
@@ -362,6 +418,7 @@ def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int =
362
  "available_balance": get_account_balance(stripe_id),
363
  "monthly_revenue": revenue_data["monthly_data"],
364
  "total_revenue_last_6_months": revenue_data["total_last_6_months"],
 
365
  "total_followers": get_total_followers(user_id),
366
  "total_subscribers": get_total_subscribers(user_id),
367
  "account_details": account_details,
 
303
  subscribers = response.json()
304
  return len(subscribers)
305
  return 0
306
+
307
+ def get_courtesy_consultations(user_id: str) -> Dict[str, Any]:
308
+ ny_timezone = pytz.timezone('America/New_York')
309
+ now_ny = datetime.now(ny_timezone)
310
+ monthly_data = {}
311
+
312
+ # Inicializar dados para os últimos 6 meses
313
+ for i in range(6):
314
+ target_date = now_ny - relativedelta(months=i)
315
+ month_num = target_date.month
316
+ month_name = target_date.strftime('%b')
317
+ year = target_date.year
318
+ month_key = f"{year}-{month_num}"
319
+ monthly_data[month_key] = {
320
+ "month": month_num,
321
+ "name": month_name,
322
+ "current": (month_num == now_ny.month and year == now_ny.year),
323
+ "courtesy_count": 0
324
+ }
325
 
326
+ # Calcular data de início (6 meses atrás)
327
+ start_date = (now_ny - relativedelta(months=6)).strftime('%Y-%m-%d')
328
+
329
+ try:
330
+ # Consultar agendamentos de cortesia
331
+ url = f"{SUPABASE_URL}/rest/v1/schedules?stylist_id=eq.{user_id}&courtesy=eq.true&date=gte.{start_date}"
332
+ response = requests.get(url, headers=SUPABASE_HEADERS)
333
+
334
+ if response.status_code == 200:
335
+ schedules = response.json()
336
+
337
+ # Contar consultas por mês
338
+ for schedule in schedules:
339
+ # Converter a data para o fuso horário de NY
340
+ schedule_date_str = schedule.get("date")
341
+ schedule_date = datetime.fromisoformat(schedule_date_str.replace('Z', '+00:00')).astimezone(ny_timezone)
342
+
343
+ month_key = f"{schedule_date.year}-{schedule_date.month}"
344
+ if month_key in monthly_data:
345
+ monthly_data[month_key]["courtesy_count"] += 1
346
+
347
+ # Convertendo para lista e ordenando por mês (do mais recente para o mais antigo)
348
+ result = list(monthly_data.values())
349
+ result.sort(key=lambda x: (now_ny.year * 12 + now_ny.month - (x["month"])) % 12)
350
+
351
+ return {
352
+ "monthly_courtesy_consultations": result
353
+ }
354
+ except Exception as e:
355
+ logger.error(f"❌ Error getting courtesy consultations: {str(e)}")
356
+ return {
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:
 
404
  # Obter dados de receita mensal e total dos últimos 6 meses
405
  revenue_data = get_monthly_revenue(stripe_id)
406
 
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
 
 
418
  "available_balance": get_account_balance(stripe_id),
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,