habulaj commited on
Commit
41e5802
·
verified ·
1 Parent(s): 6f9b042

Update routes/stylist.py

Browse files
Files changed (1) hide show
  1. routes/stylist.py +36 -3
routes/stylist.py CHANGED
@@ -117,6 +117,24 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
117
  logger.error(f"❌ Error getting monthly revenue: {str(e)}")
118
  return list(monthly_data.values())
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  def get_active_subscribers(user_id: str, page: int) -> Dict[str, Any]:
121
  limit = 30
122
  offset = page * limit
@@ -127,18 +145,17 @@ def get_active_subscribers(user_id: str, page: int) -> Dict[str, Any]:
127
  subscriber_list = []
128
  for sub in subscribers:
129
  customer_id = sub.get("customer_id")
130
- created_at = datetime.strptime(sub.get("created_at"), "%Y-%m-%d %H:%M:%S.%f%z")
131
- formatted_date = created_at.strftime("%dth %B %Y")
132
  user_data_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{customer_id}"
133
  user_response = requests.get(user_data_url, headers=SUPABASE_HEADERS)
134
  if user_response.status_code == 200 and user_response.json():
135
  user_info = user_response.json()[0]
 
136
  subscriber_list.append({
137
  "id": user_info.get("id"),
138
  "name": user_info.get("name"),
139
  "avatar": user_info.get("avatar"),
140
  "blurhash": user_info.get("blurhash"),
141
- "subscription_date": formatted_date
142
  })
143
 
144
  has_next_page = len(subscribers) == limit
@@ -146,6 +163,22 @@ def get_active_subscribers(user_id: str, page: int) -> Dict[str, Any]:
146
 
147
  return {"subscribers": [], "has_next_page": False}
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  @router.get("/dashboard")
150
  def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int = Query(0, ge=0)):
151
  try:
 
117
  logger.error(f"❌ Error getting monthly revenue: {str(e)}")
118
  return list(monthly_data.values())
119
 
120
+ def format_subscription_date(created_at_str: str) -> str:
121
+ """Format the subscription date to the requested format: e.g. '13th January 2025'"""
122
+ try:
123
+ created_at = datetime.fromisoformat(created_at_str.replace('Z', '+00:00'))
124
+
125
+ # Add suffix to day
126
+ day = created_at.day
127
+ if 4 <= day <= 20 or 24 <= day <= 30:
128
+ suffix = "th"
129
+ else:
130
+ suffix = {1: "st", 2: "nd", 3: "rd"}.get(day % 10, "th")
131
+
132
+ # Format the date
133
+ return f"{day}{suffix} {created_at.strftime('%B %Y')}"
134
+ except Exception as e:
135
+ logger.error(f"❌ Error formatting subscription date: {str(e)}")
136
+ return "Unknown date"
137
+
138
  def get_active_subscribers(user_id: str, page: int) -> Dict[str, Any]:
139
  limit = 30
140
  offset = page * limit
 
145
  subscriber_list = []
146
  for sub in subscribers:
147
  customer_id = sub.get("customer_id")
 
 
148
  user_data_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{customer_id}"
149
  user_response = requests.get(user_data_url, headers=SUPABASE_HEADERS)
150
  if user_response.status_code == 200 and user_response.json():
151
  user_info = user_response.json()[0]
152
+ subscription_date = format_subscription_date(sub.get("created_at", ""))
153
  subscriber_list.append({
154
  "id": user_info.get("id"),
155
  "name": user_info.get("name"),
156
  "avatar": user_info.get("avatar"),
157
  "blurhash": user_info.get("blurhash"),
158
+ "subscription_date": subscription_date
159
  })
160
 
161
  has_next_page = len(subscribers) == limit
 
163
 
164
  return {"subscribers": [], "has_next_page": False}
165
 
166
+ def get_total_followers(user_id: str) -> int:
167
+ url = f"{SUPABASE_URL}/rest/v1/followers?following_id=eq.{user_id}"
168
+ response = requests.get(url, headers=SUPABASE_HEADERS)
169
+ if response.status_code == 200:
170
+ followers = response.json()
171
+ return len(followers)
172
+ return 0
173
+
174
+ def get_total_subscribers(user_id: str) -> int:
175
+ url = f"{SUPABASE_URL}/rest/v1/Subscriptions?stylist_id=eq.{user_id}&active=eq.true"
176
+ response = requests.get(url, headers=SUPABASE_HEADERS)
177
+ if response.status_code == 200:
178
+ subscribers = response.json()
179
+ return len(subscribers)
180
+ return 0
181
+
182
  @router.get("/dashboard")
183
  def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int = Query(0, ge=0)):
184
  try: