Tu Nombre commited on
Commit
f6ca537
·
1 Parent(s): 3faef5f

limite por plan + 1 canal por crear

Browse files
Files changed (1) hide show
  1. main.py +35 -20
main.py CHANGED
@@ -601,29 +601,44 @@ async def activar_canal(token: str = "", plan: str = ""):
601
  @app.post("/crear")
602
  async def crear(request: Request):
603
  data = await request.json()
604
- email = data.get("email","")
605
- canales = data.get("canales", []) # lista de {nicho, tipo}
 
606
  token = data.get("token","")
607
- if not token or token not in TOKENS_VALIDOS:
 
 
 
 
 
 
608
  return JSONResponse({"error": "Token invalido"}, status_code=403)
609
- if not canales:
610
- return JSONResponse({"error": "Sin canales"}, status_code=400)
611
- urls = []
612
- for canal in canales:
613
- nicho = canal.get("nicho", "tecnologia")
614
- tipo = canal.get("tipo", "largo")
615
- u = await crear_space(nicho, email, tipo)
616
- if u:
617
- urls.append(u)
618
- if urls:
 
 
 
 
 
 
 
 
 
619
  del TOKENS_VALIDOS[token]
620
- # Enviar email con todas las URLs
621
- try:
622
- enviar_email_multi(email, urls)
623
- except Exception as e:
624
- print(f"Error email multi: {e}", flush=True)
625
- return {"success": True, "urls": urls, "url": urls[0]}
626
- return JSONResponse({"error": "No se pudo crear ningun Space"}, status_code=500)
627
 
628
 
629
  def enviar_email_multi(email, urls):
 
601
  @app.post("/crear")
602
  async def crear(request: Request):
603
  data = await request.json()
604
+ email = data.get("email","").strip().lower()
605
+ nicho = data.get("nicho","tecnologia")
606
+ tipo = data.get("tipo","largo")
607
  token = data.get("token","")
608
+ # Verificar token o sesion existente
609
+ plan = "basico"
610
+ if token and token in TOKENS_VALIDOS:
611
+ plan = TOKENS_VALIDOS[token].get("plan", "basico") if isinstance(TOKENS_VALIDOS[token], dict) else "basico"
612
+ elif email in CLIENTES and CLIENTES[email].get("plan"):
613
+ plan = CLIENTES[email]["plan"]
614
+ else:
615
  return JSONResponse({"error": "Token invalido"}, status_code=403)
616
+ # Verificar limite del plan
617
+ limite = PLANES_CANALES.get(plan, 1)
618
+ canales_actuales = len(CLIENTES.get(email, {}).get("canales", []))
619
+ if canales_actuales >= limite:
620
+ return JSONResponse({"error": f"Has alcanzado el limite de tu plan {plan} ({limite} canales)"}, status_code=400)
621
+ # Crear el canal
622
+ u = await crear_space(nicho, email, tipo)
623
+ if not u:
624
+ return JSONResponse({"error": "No se pudo crear el canal"}, status_code=500)
625
+ # Guardar en CLIENTES
626
+ if email not in CLIENTES:
627
+ CLIENTES[email] = {"canales": [], "plan": plan}
628
+ CLIENTES[email]["plan"] = plan
629
+ if "canales" not in CLIENTES[email]:
630
+ CLIENTES[email]["canales"] = []
631
+ CLIENTES[email]["canales"].append({"url": u, "nicho": nicho, "tipo": tipo})
632
+ guardar_clientes()
633
+ # Consumir token si era nuevo
634
+ if token in TOKENS_VALIDOS:
635
  del TOKENS_VALIDOS[token]
636
+ urls = [c["url"] for c in CLIENTES[email]["canales"]]
637
+ try:
638
+ enviar_email_multi(email, urls)
639
+ except Exception as e:
640
+ print(f"Error email: {e}", flush=True)
641
+ return {"success": True, "url": u, "total": len(urls), "limite": limite}
 
642
 
643
 
644
  def enviar_email_multi(email, urls):