informe-comercial-alfa/app/cuentas/page.js

67 lines
2.9 KiB
JavaScript
Raw Normal View History

import Link from "next/link";
import { auth } from "../lib/auth";
import { buscarCuentas } from "../lib/cuentasServer";
import { esAsistente } from "../lib/authz";
import { resolveOwnerId } from "../lib/queries";
import LoginPrompt from "../components/LoginPrompt";
import ErrorNotice from "../components/ErrorNotice";
export const dynamic = "force-dynamic";
// Cuentas del CRM: buscar una cuenta por nombre y abrir su ficha (datos, contactos, oportunidades, actividades).
export default async function CuentasPage({ searchParams }) {
const session = await auth();
if (!session?.email) return <LoginPrompt />;
const sp = await searchParams;
const q = String(sp.q || "").trim().slice(0, 60);
let cuentas = null;
if (q.length >= 2) {
try {
// Ejecutivo: solo sus cuentas. Gerente y asistente de ventas: todas.
const veTodas = session.isAdmin || esAsistente(session.email);
const mio = veTodas ? null : await resolveOwnerId(session.email, session.accessToken);
cuentas = veTodas ? await buscarCuentas(session.accessToken, q, 30) : mio ? await buscarCuentas(session.accessToken, q, 30, mio) : [];
} catch (error) {
return <ErrorNotice error={error} />;
}
}
return (
<div style={{ padding: 24 }}>
<h2>Cuentas</h2>
<p className="hint">Busca una cuenta del CRM para ver sus datos, contactos y oportunidades, y agregarle actividades.{session.isAdmin || esAsistente(session.email) ? "" : " Aquí aparecen las cuentas de las que eres propietario."}</p>
<form method="get" className="busca-opp">
<input type="search" name="q" defaultValue={q} placeholder="🔍 Nombre o nombre comercial de la cuenta…" aria-label="Buscar cuenta" autoFocus />
<button type="submit" className="gst-go" style={{ padding: "8px 18px" }}>Buscar</button>
</form>
{q.length > 0 && q.length < 2 && <p className="hint">Escribe al menos 2 letras.</p>}
{cuentas && (
<div className="panel">
<h3>{cuentas.length ? `${cuentas.length}${cuentas.length === 30 ? "+" : ""} cuenta${cuentas.length === 1 ? "" : "s"} para «${q}»` : `Sin cuentas para «${q}»`}</h3>
{cuentas.length > 0 && (
<table className="cta-tabla">
<thead>
<tr><th>Cuenta</th><th>Tipo</th><th>Propietario</th><th>Teléfono</th></tr>
</thead>
<tbody>
{cuentas.map((c) => (
<tr key={c.id}>
<td className="dual-cel">
<Link className="opp" href={`/cuentas/${c.id}`}>{c.nombre}</Link>
{c.comercial && <span className="sub">{c.comercial}</span>}
</td>
<td>{c.tipo || "—"}</td>
<td>{c.propietario || "—"}</td>
<td>{c.telefono || "—"}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
)}
</div>
);
}