Update routes/stylist.py
Browse files- routes/stylist.py +59 -37
routes/stylist.py
CHANGED
@@ -50,6 +50,7 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
|
|
50 |
current_month = now_ny.month
|
51 |
current_year = now_ny.year
|
52 |
monthly_data = {}
|
|
|
53 |
for i in range(6):
|
54 |
target_date = now_ny - relativedelta(months=i)
|
55 |
month_num = target_date.month
|
@@ -62,7 +63,7 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
|
|
62 |
"name": month_name,
|
63 |
"current": is_current,
|
64 |
"amount": 0,
|
65 |
-
"growth": {"
|
66 |
}
|
67 |
start_date = now_ny - relativedelta(months=6)
|
68 |
start_timestamp = int(start_date.timestamp())
|
@@ -82,40 +83,68 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
|
|
82 |
result = list(monthly_data.values())
|
83 |
result.sort(key=lambda x: (current_month - x["month"]) % 12)
|
84 |
for i in range(1, len(result)):
|
85 |
-
prev_amount = result[i-1]["amount"]
|
86 |
-
|
87 |
if prev_amount > 0:
|
88 |
-
growth_percentage = ((
|
89 |
else:
|
90 |
-
growth_percentage = 100 if
|
91 |
-
direction = "up" if growth_percentage > 0 else "down"
|
92 |
result[i]["growth"] = {
|
93 |
-
"
|
94 |
-
"percentage": round(
|
95 |
-
"formatted": f"{
|
96 |
}
|
97 |
return result
|
98 |
except Exception as e:
|
99 |
logger.error(f"❌ Error getting monthly revenue: {str(e)}")
|
100 |
return list(monthly_data.values())
|
101 |
|
102 |
-
def
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
response = requests.get(url, headers=SUPABASE_HEADERS)
|
105 |
if response.status_code == 200:
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
119 |
return []
|
120 |
|
121 |
@router.get("/dashboard")
|
@@ -140,22 +169,15 @@ def get_dashboard(user_token: str = Header(None, alias="User-key")):
|
|
140 |
"monthly_revenue": [],
|
141 |
"total_followers": 0,
|
142 |
"total_subscribers": 0,
|
143 |
-
"
|
144 |
}
|
145 |
-
balance_info = get_account_balance(stripe_id)
|
146 |
-
monthly_revenue = get_monthly_revenue(stripe_id)
|
147 |
-
total_followers = get_total_followers(user_id)
|
148 |
-
total_subscribers = get_total_subscribers(user_id)
|
149 |
-
active_subscribers = get_active_subscribers(user_id)
|
150 |
return {
|
151 |
"stripe_id": stripe_id,
|
152 |
-
"available_balance":
|
153 |
-
"pending_balance":
|
154 |
-
"currency":
|
155 |
-
"monthly_revenue":
|
156 |
-
"
|
157 |
-
"total_subscribers": total_subscribers,
|
158 |
-
"active_subscribers": active_subscribers
|
159 |
}
|
160 |
except HTTPException as http_err:
|
161 |
raise http_err
|
|
|
50 |
current_month = now_ny.month
|
51 |
current_year = now_ny.year
|
52 |
monthly_data = {}
|
53 |
+
|
54 |
for i in range(6):
|
55 |
target_date = now_ny - relativedelta(months=i)
|
56 |
month_num = target_date.month
|
|
|
63 |
"name": month_name,
|
64 |
"current": is_current,
|
65 |
"amount": 0,
|
66 |
+
"growth": {"status": "", "percentage": 0, "formatted": "0.0%"}
|
67 |
}
|
68 |
start_date = now_ny - relativedelta(months=6)
|
69 |
start_timestamp = int(start_date.timestamp())
|
|
|
83 |
result = list(monthly_data.values())
|
84 |
result.sort(key=lambda x: (current_month - x["month"]) % 12)
|
85 |
for i in range(1, len(result)):
|
86 |
+
prev_amount = result[i - 1]["amount"]
|
87 |
+
current_amount = result[i]["amount"]
|
88 |
if prev_amount > 0:
|
89 |
+
growth_percentage = ((current_amount - prev_amount) / prev_amount) * 100
|
90 |
else:
|
91 |
+
growth_percentage = 100 if current_amount > 0 else 0
|
|
|
92 |
result[i]["growth"] = {
|
93 |
+
"status": "up" if growth_percentage > 0 else "down",
|
94 |
+
"percentage": round(growth_percentage, 1),
|
95 |
+
"formatted": f"{round(growth_percentage, 1)}%"
|
96 |
}
|
97 |
return result
|
98 |
except Exception as e:
|
99 |
logger.error(f"❌ Error getting monthly revenue: {str(e)}")
|
100 |
return list(monthly_data.values())
|
101 |
|
102 |
+
def get_account_balance(account_id: str) -> Dict[str, Any]:
|
103 |
+
try:
|
104 |
+
balance = stripe.Balance.retrieve(stripe_account=account_id)
|
105 |
+
available_balance = 0
|
106 |
+
pending_balance = 0
|
107 |
+
currency = "BRL"
|
108 |
+
for balance_item in balance.available:
|
109 |
+
if balance_item.currency.upper() == "BRL":
|
110 |
+
available_balance = balance_item.amount
|
111 |
+
break
|
112 |
+
for balance_item in balance.pending:
|
113 |
+
if balance_item.currency.upper() == "BRL":
|
114 |
+
pending_balance = balance_item.amount
|
115 |
+
break
|
116 |
+
return {
|
117 |
+
"available_balance": available_balance,
|
118 |
+
"pending_balance": pending_balance,
|
119 |
+
"currency": currency
|
120 |
+
}
|
121 |
+
except Exception as e:
|
122 |
+
logger.error(f"❌ Error getting account balance: {str(e)}")
|
123 |
+
return {
|
124 |
+
"available_balance": 0,
|
125 |
+
"pending_balance": 0,
|
126 |
+
"currency": "BRL"
|
127 |
+
}
|
128 |
+
|
129 |
+
def get_active_subscribers(user_id: str) -> List[Dict[str, Any]]:
|
130 |
+
url = f"{SUPABASE_URL}/rest/v1/Subscriptions?stylist_id=eq.{user_id}&active=eq.true&limit=30"
|
131 |
response = requests.get(url, headers=SUPABASE_HEADERS)
|
132 |
if response.status_code == 200:
|
133 |
+
subscribers = response.json()
|
134 |
+
subscriber_list = []
|
135 |
+
for sub in subscribers:
|
136 |
+
customer_id = sub.get("customer_id")
|
137 |
+
user_data_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{customer_id}"
|
138 |
+
user_response = requests.get(user_data_url, headers=SUPABASE_HEADERS)
|
139 |
+
if user_response.status_code == 200 and user_response.json():
|
140 |
+
user_info = user_response.json()[0]
|
141 |
+
subscriber_list.append({
|
142 |
+
"id": user_info.get("id"),
|
143 |
+
"name": user_info.get("name"),
|
144 |
+
"avatar": user_info.get("avatar"),
|
145 |
+
"blurhash": user_info.get("blurhash")
|
146 |
+
})
|
147 |
+
return subscriber_list
|
148 |
return []
|
149 |
|
150 |
@router.get("/dashboard")
|
|
|
169 |
"monthly_revenue": [],
|
170 |
"total_followers": 0,
|
171 |
"total_subscribers": 0,
|
172 |
+
"subscribers": []
|
173 |
}
|
|
|
|
|
|
|
|
|
|
|
174 |
return {
|
175 |
"stripe_id": stripe_id,
|
176 |
+
"available_balance": get_account_balance(stripe_id)["available_balance"],
|
177 |
+
"pending_balance": get_account_balance(stripe_id)["pending_balance"],
|
178 |
+
"currency": get_account_balance(stripe_id)["currency"],
|
179 |
+
"monthly_revenue": get_monthly_revenue(stripe_id),
|
180 |
+
"subscribers": get_active_subscribers(user_id)
|
|
|
|
|
181 |
}
|
182 |
except HTTPException as http_err:
|
183 |
raise http_err
|