Reorganiza el routing multi-tenant: funnel por slug, B2B en raíz

- / y /b2b sirven la landing B2B estática (rewrites beforeFiles)
- /{slug} resuelve el funnel del reformista (app/[slug]/page.tsx) con
  branding propio (TenantBrand) y atribución de leads por tenant
- crearLead(slug) y páginas /solicitud usan el tenant del lead
- Panel: edición del slug del funnel + URL pública en /panel/empresa
- Helper de slugs reservados para evitar colisiones con rutas reales

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Carlos Narro
2026-06-01 11:09:44 +02:00
parent e26e6be38b
commit 1a1caaf0df
15 changed files with 268 additions and 86 deletions

View File

@@ -6,7 +6,7 @@ import { redirect } from 'next/navigation';
import { revalidatePath } from 'next/cache';
import { db } from '@/db';
import { leads, leadFotos, leadPipelineEventos } from '@/db/schema';
import { getDemoTenantId } from '@/lib/funnel/public-queries';
import { getTenantBySlug } from '@/lib/funnel/public-queries';
import { procesarLead } from '@/lib/funnel/orchestrator';
const MAX_FOTOS = 4;
@@ -28,7 +28,7 @@ export type CrearLeadResult =
| { ok: true; leadId: string }
| { ok: false; error: string };
export async function crearLead(input: CrearLeadInput): Promise<CrearLeadResult> {
export async function crearLead(slug: string, input: CrearLeadInput): Promise<CrearLeadResult> {
const parsed = crearLeadSchema.safeParse(input);
if (!parsed.success) {
return { ok: false, error: parsed.error.issues[0]?.message ?? 'Datos inválidos.' };
@@ -40,12 +40,16 @@ export async function crearLead(input: CrearLeadInput): Promise<CrearLeadResult>
return { ok: false, error: 'Debes aceptar la política de privacidad y las condiciones.' };
}
const tenantId = await getDemoTenantId();
// El lead se atribuye al reformista dueño del funnel (slug de la URL pública).
const tenant = await getTenantBySlug(slug);
if (!tenant) {
return { ok: false, error: 'No hemos podido identificar al reformista. Recarga la página.' };
}
const [lead] = await db
.insert(leads)
.values({
tenantId,
tenantId: tenant.id,
nombre: data.nombre,
email: data.email,
telefono: data.telefono,
@@ -79,14 +83,9 @@ async function fileToDataUri(file: File): Promise<string | null> {
// Guardamos las fotos como data URI (no hay storage externo en esta fase) y disparamos
// el orquestador que simula la llamada/render y calcula el presupuesto real.
export async function guardarDetallesYFotos(leadId: string, formData: FormData): Promise<void> {
const tenantId = await getDemoTenantId();
const [lead] = await db
.select()
.from(leads)
.where(and(eq(leads.id, leadId), eq(leads.tenantId, tenantId)))
.limit(1);
const [lead] = await db.select().from(leads).where(eq(leads.id, leadId)).limit(1);
if (!lead) throw new Error('Solicitud no encontrada.');
const tenantId = lead.tenantId;
const tipoRaw = String(formData.get('tipoReforma') ?? '');
const calidadRaw = String(formData.get('calidad') ?? '');