habulaj commited on
Commit
2617ed3
·
verified ·
1 Parent(s): 129de0c

Update routes/subscription.py

Browse files
Files changed (1) hide show
  1. routes/subscription.py +26 -2
routes/subscription.py CHANGED
@@ -60,6 +60,24 @@ class CreatePriceRequest(BaseModel):
60
  emergency_price: int # Valor de emergência (ex: 500 para R$5,00)
61
  consultations: int # Número de consultas (ex: 3)
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  def get_active_subscribers_by_price_id(price_id: str) -> list:
64
  """
65
  Retorna uma lista de customer_ids que têm uma assinatura ativa com o price_id fornecido.
@@ -940,6 +958,7 @@ async def create_price(data: CreatePriceRequest, user_token: str = Header(None,
940
 
941
  affected_users = []
942
 
 
943
  if existing_price_id:
944
  subscriptions = stripe.Subscription.list(status="active")
945
  for sub in subscriptions.auto_paging_iter():
@@ -976,10 +995,15 @@ async def create_price(data: CreatePriceRequest, user_token: str = Header(None,
976
  if update_response.status_code not in [200, 204]:
977
  raise HTTPException(status_code=500, detail=f"Failed to update user: {update_response.text}")
978
 
979
- # 🔥 Cria notificações para os afetados
980
  create_notifications_for_price_change(affected_users, stylist_id=user_id)
 
 
 
 
 
981
 
982
- logger.info(f"✅ User updated and notifications sent")
983
 
984
  return {"message": "Price created and user updated successfully!", "price_id": new_price_id}
985
 
 
60
  emergency_price: int # Valor de emergência (ex: 500 para R$5,00)
61
  consultations: int # Número de consultas (ex: 3)
62
 
63
+ async def send_changeprice_push(target_user_id: str, stylist_id: str, token: str):
64
+ async with aiohttp.ClientSession() as session:
65
+ payload = {
66
+ "keyword": "changeprice",
67
+ "target_user_id": target_user_id,
68
+ "reference": "" # opcional
69
+ }
70
+ headers = {
71
+ "User-key": token, # token do estilista para autenticar
72
+ "Content-Type": "application/json"
73
+ }
74
+ url = "http://localhost:8000/send-notification" # ajuste a URL conforme seu servidor
75
+
76
+ async with session.post(url, json=payload, headers=headers) as resp:
77
+ if resp.status != 200:
78
+ text = await resp.text()
79
+ logger.warning(f"⚠️ Failed to send push notification to {target_user_id}: {resp.status} - {text}")
80
+
81
  def get_active_subscribers_by_price_id(price_id: str) -> list:
82
  """
83
  Retorna uma lista de customer_ids que têm uma assinatura ativa com o price_id fornecido.
 
958
 
959
  affected_users = []
960
 
961
+ # 🔥 Atualiza assinaturas existentes, se houver
962
  if existing_price_id:
963
  subscriptions = stripe.Subscription.list(status="active")
964
  for sub in subscriptions.auto_paging_iter():
 
995
  if update_response.status_code not in [200, 204]:
996
  raise HTTPException(status_code=500, detail=f"Failed to update user: {update_response.text}")
997
 
998
+ # 🔥 Cria notificações no Supabase
999
  create_notifications_for_price_change(affected_users, stylist_id=user_id)
1000
+ logger.info(f"✅ Supabase notifications created")
1001
+
1002
+ # 🔔 Envia push notifications para os afetados
1003
+ for target_user in affected_users:
1004
+ asyncio.create_task(send_changeprice_push(target_user_id=target_user, stylist_id=user_id, token=user_token))
1005
 
1006
+ logger.info(f"✅ Push notifications dispatched")
1007
 
1008
  return {"message": "Price created and user updated successfully!", "price_id": new_price_id}
1009