Update routes/subscription.py
Browse files- routes/subscription.py +32 -25
routes/subscription.py
CHANGED
@@ -517,21 +517,28 @@ def check_subscription(
|
|
517 |
)
|
518 |
if response_subscriptions.status_code == 200:
|
519 |
subscriptions_data = response_subscriptions.json()
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
update_response = requests.patch(
|
527 |
-
|
528 |
headers=SUPABASE_HEADERS,
|
529 |
json=update_data
|
530 |
)
|
531 |
if update_response.status_code == 200:
|
532 |
-
logger.info(f"✅
|
533 |
else:
|
534 |
-
logger.error(f"❌ Failed to deactivate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
535 |
else:
|
536 |
logger.error(f"❌ Failed to fetch subscriptions from Supabase for stylist {data.stylist_id} and user {user_id}.")
|
537 |
|
@@ -603,28 +610,28 @@ def check_subscription(
|
|
603 |
)
|
604 |
if response_subscriptions.status_code == 200:
|
605 |
subscriptions_data = response_subscriptions.json()
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
update_response = requests.patch(
|
613 |
-
|
614 |
headers=SUPABASE_HEADERS,
|
615 |
json=update_data
|
616 |
)
|
617 |
-
|
618 |
-
# Logar resposta da requisição PATCH
|
619 |
if update_response.status_code == 200:
|
620 |
-
logger.info(f"✅
|
621 |
else:
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
|
|
|
|
628 |
|
629 |
except stripe.error.StripeError as e:
|
630 |
logger.error(f"Stripe error: {str(e)}")
|
|
|
517 |
)
|
518 |
if response_subscriptions.status_code == 200:
|
519 |
subscriptions_data = response_subscriptions.json()
|
520 |
+
|
521 |
+
# 🔹 Preparar a atualização em lote
|
522 |
+
update_data = [{"id": sub["id"], "active": False} for sub in subscriptions_data]
|
523 |
+
|
524 |
+
# Atualiza todas as assinaturas do estilista no banco de dados de uma vez
|
525 |
+
if update_data:
|
526 |
update_response = requests.patch(
|
527 |
+
f"{SUPABASE_URL}/rest/v1/Subscriptions",
|
528 |
headers=SUPABASE_HEADERS,
|
529 |
json=update_data
|
530 |
)
|
531 |
if update_response.status_code == 200:
|
532 |
+
logger.info(f"✅ All subscriptions for stylist {data.stylist_id} deactivated successfully.")
|
533 |
else:
|
534 |
+
logger.error(f"❌ Failed to deactivate subscriptions for stylist {data.stylist_id}.")
|
535 |
+
|
536 |
+
# Retorna as assinaturas desativadas no banco
|
537 |
+
return {
|
538 |
+
"status": "inactive",
|
539 |
+
"message": "No active subscription found for stylist.",
|
540 |
+
"subscriptions_updated": subscriptions_data
|
541 |
+
}
|
542 |
else:
|
543 |
logger.error(f"❌ Failed to fetch subscriptions from Supabase for stylist {data.stylist_id} and user {user_id}.")
|
544 |
|
|
|
610 |
)
|
611 |
if response_subscriptions.status_code == 200:
|
612 |
subscriptions_data = response_subscriptions.json()
|
613 |
+
|
614 |
+
# 🔹 Preparar a atualização em lote
|
615 |
+
update_data = [{"id": sub["id"], "active": False} for sub in subscriptions_data]
|
616 |
+
|
617 |
+
# Atualiza todas as assinaturas do estilista no banco de dados de uma vez
|
618 |
+
if update_data:
|
619 |
update_response = requests.patch(
|
620 |
+
f"{SUPABASE_URL}/rest/v1/Subscriptions",
|
621 |
headers=SUPABASE_HEADERS,
|
622 |
json=update_data
|
623 |
)
|
|
|
|
|
624 |
if update_response.status_code == 200:
|
625 |
+
logger.info(f"✅ All subscriptions for stylist {data.stylist_id} deactivated successfully.")
|
626 |
else:
|
627 |
+
logger.error(f"❌ Failed to deactivate subscriptions for stylist {data.stylist_id}.")
|
628 |
+
|
629 |
+
# Retorna as assinaturas desativadas no banco
|
630 |
+
return {
|
631 |
+
"status": "inactive",
|
632 |
+
"message": "No active subscription found for stylist.",
|
633 |
+
"subscriptions_updated": subscriptions_data
|
634 |
+
}
|
635 |
|
636 |
except stripe.error.StripeError as e:
|
637 |
logger.error(f"Stripe error: {str(e)}")
|