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