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

@@ -1,10 +1,11 @@
'use server';
import { eq } from 'drizzle-orm';
import { and, eq, ne } from 'drizzle-orm';
import { revalidatePath } from 'next/cache';
import { db } from '@/db';
import { tenants } from '@/db/schema';
import { getCurrentTenantId as getTenantId } from '@/lib/auth/current-user';
import { validarSlug } from '@/lib/validation/signup';
const LOGO_MAX_BYTES = 500_000;
const LOGO_TIPOS = ['image/png', 'image/jpeg', 'image/webp', 'image/svg+xml'];
@@ -20,10 +21,32 @@ export async function actualizarEmpresa(formData: FormData) {
if (!nombreEmpresa) {
throw new Error('El nombre de la empresa es obligatorio.');
}
const slugRaw = limpiar(formData.get('slug'));
if (!slugRaw) {
throw new Error('El enlace de tu funnel es obligatorio.');
}
const validacion = validarSlug(slugRaw);
if (!validacion.ok) {
throw new Error(validacion.error);
}
const slug = validacion.slug;
// El slug es único en todo el sistema; comprobamos que no lo use otro reformista.
const [colision] = await db
.select({ id: tenants.id })
.from(tenants)
.where(and(eq(tenants.slug, slug), ne(tenants.id, tenantId)))
.limit(1);
if (colision) {
throw new Error('Ese enlace ya está en uso. Elige otro.');
}
await db
.update(tenants)
.set({
nombreEmpresa,
slug,
cif: limpiar(formData.get('cif')),
direccion: limpiar(formData.get('direccion')),
provincia: limpiar(formData.get('provincia')),

View File

@@ -1,3 +1,4 @@
import { headers } from 'next/headers';
import { getTenantPerfil } from '@/db/tenant-queries';
import { actualizarEmpresa } from './actions';
import LogoUploader from '@/components/panel/LogoUploader';
@@ -7,6 +8,11 @@ export const dynamic = 'force-dynamic';
export default async function EmpresaPage() {
const perfil = await getTenantPerfil();
const h = await headers();
const host = h.get('x-forwarded-host') ?? h.get('host') ?? 'reformix.dv3.com.es';
const proto = h.get('x-forwarded-proto') ?? 'https';
const funnelUrl = `${proto}://${host}/${perfil.slug}`;
return (
<div className="space-y-10 max-w-2xl">
<div>
@@ -34,6 +40,32 @@ export default async function EmpresaPage() {
className="w-full border border-gray-300 rounded-lg px-3 py-2"
/>
</label>
<label className="text-sm md:col-span-2">
<span className="block text-gray-500 mb-1">Enlace de tu funnel *</span>
<div className="flex items-center border border-gray-300 rounded-lg overflow-hidden">
<span className="px-3 py-2 text-gray-400 bg-gray-50 border-r border-gray-200 select-none whitespace-nowrap">
{host}/
</span>
<input
name="slug"
required
defaultValue={perfil.slug}
pattern="[a-z0-9-]+"
className="flex-1 px-3 py-2 outline-none min-w-0"
/>
</div>
<span className="block text-gray-400 mt-1.5 text-xs">
Esta es la dirección que compartes con tus clientes:{' '}
<a
href={funnelUrl}
target="_blank"
rel="noopener noreferrer"
className="text-black underline underline-offset-2 break-all"
>
{funnelUrl}
</a>
</span>
</label>
<label className="text-sm">
<span className="block text-gray-500 mb-1">CIF / NIF</span>
<input