145 lines
7 KiB
JavaScript
145 lines
7 KiB
JavaScript
|
|
import Link from "next/link";
|
||
|
|
import { auth } from "../../lib/auth";
|
||
|
|
import { leerCuenta, esGuid } from "../../lib/cuentasServer";
|
||
|
|
import { hoyManagua } from "../../lib/plan";
|
||
|
|
import { esAsistente } from "../../lib/authz";
|
||
|
|
import { resolveOwnerId } from "../../lib/queries";
|
||
|
|
import { money, registroUrl, DYN } from "../../lib/constants";
|
||
|
|
import LoginPrompt from "../../components/LoginPrompt";
|
||
|
|
import ErrorNotice from "../../components/ErrorNotice";
|
||
|
|
import NuevaActividadBoton from "../../components/actividades/NuevaActividadBoton";
|
||
|
|
import GestionarActividad from "../../components/actividades/GestionarActividad";
|
||
|
|
|
||
|
|
export const dynamic = "force-dynamic";
|
||
|
|
|
||
|
|
const fecha = (iso) => (iso ? `${iso.slice(8, 10)}/${iso.slice(5, 7)}/${iso.slice(0, 4)}` : "—");
|
||
|
|
// Fecha de Nicaragua (UTC-6) de un instante ISO.
|
||
|
|
const diaLocal = (iso) => (iso ? new Date(new Date(iso).getTime() - 6 * 3600 * 1000).toISOString().slice(0, 10) : null);
|
||
|
|
|
||
|
|
export default async function CuentaPage({ params }) {
|
||
|
|
const session = await auth();
|
||
|
|
if (!session?.email) return <LoginPrompt />;
|
||
|
|
const { id } = await params;
|
||
|
|
if (!esGuid(id)) return <div style={{ padding: 24 }}><p>Cuenta no válida. <Link href="/cuentas">Volver a Cuentas</Link></p></div>;
|
||
|
|
|
||
|
|
let c;
|
||
|
|
try {
|
||
|
|
c = await leerCuenta(session.accessToken, id);
|
||
|
|
} catch (error) {
|
||
|
|
if (String(error?.message || "").includes("DATAVERSE_404")) return <div style={{ padding: 24 }}><p>No encontré esa cuenta. <Link href="/cuentas">Volver a Cuentas</Link></p></div>;
|
||
|
|
return <ErrorNotice error={error} />;
|
||
|
|
}
|
||
|
|
// Ejecutivo: solo las cuentas de las que es propietario. Gerente y asistente de ventas: todas.
|
||
|
|
if (!(session.isAdmin || esAsistente(session.email))) {
|
||
|
|
const mio = await resolveOwnerId(session.email, session.accessToken);
|
||
|
|
if (!mio || mio !== c.propietarioId) {
|
||
|
|
return <div style={{ padding: 24 }}><p>No tienes acceso a esta cuenta: solo puedes ver las cuentas de las que eres propietario. <Link href="/cuentas">Volver a Cuentas</Link></p></div>;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
const mrr = c.oportunidades.reduce((a, o) => a + (o.valor || 0), 0);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div style={{ padding: 24 }}>
|
||
|
|
<p style={{ margin: 0 }}><Link href="/cuentas">← Cuentas</Link></p>
|
||
|
|
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", flexWrap: "wrap", gap: 12, margin: "6px 0 14px" }}>
|
||
|
|
<div>
|
||
|
|
<h2 style={{ margin: 0 }}>{c.nombre}{c.vip ? " ⭐" : ""}</h2>
|
||
|
|
{c.comercial && <div className="hint" style={{ margin: 0 }}>{c.comercial}</div>}
|
||
|
|
</div>
|
||
|
|
<div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
|
||
|
|
<NuevaActividadBoton hoy={hoyManagua()} referente={{ tipo: "account", id: c.id, nombre: c.nombre }} oportunidades={c.oportunidades.map((o) => ({ id: o.id, nombre: o.nombre }))} />
|
||
|
|
<a className="gst-sec-btn" style={{ padding: "8px 14px", textDecoration: "none" }} href={registroUrl("account", c.id)} target="_blank" rel="noopener">Abrir en Dynamics ↗</a>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="kpis">
|
||
|
|
<div className="kpi"><span className="n">{c.oportunidades.length}</span><span className="l">Oportunidades abiertas</span></div>
|
||
|
|
<div className="kpi"><span className="n">{money(mrr)}</span><span className="l">Valor estimado abierto</span></div>
|
||
|
|
<div className="kpi"><span className="n">{c.contactos.length}</span><span className="l">Contactos activos</span></div>
|
||
|
|
{c.puntuacion != null && <div className="kpi"><span className="n">{Math.round(c.puntuacion)}</span><span className="l">Puntuación de la cuenta</span></div>}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="panel">
|
||
|
|
<h3>Datos de la cuenta</h3>
|
||
|
|
<table className="cta-tabla">
|
||
|
|
<tbody>
|
||
|
|
{[
|
||
|
|
["Propietario", c.propietario],
|
||
|
|
["Tipo de relación", c.tipo],
|
||
|
|
["Nivel", c.nivel],
|
||
|
|
["Clasificación", c.clasificacion],
|
||
|
|
["Teléfono", c.telefono],
|
||
|
|
["Correo", c.correo],
|
||
|
|
["Dirección", [c.direccion, c.ciudad].filter(Boolean).join(", ")],
|
||
|
|
["Creada", c.creada ? fecha(c.creada) : null],
|
||
|
|
].map(([k, v]) => (
|
||
|
|
<tr key={k}><th style={{ width: 160, textAlign: "left" }}>{k}</th><td>{v || "—"}</td></tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="panel">
|
||
|
|
<h3>Oportunidades abiertas ({c.oportunidades.length})</h3>
|
||
|
|
{c.oportunidades.length ? (
|
||
|
|
<table className="cta-tabla">
|
||
|
|
<thead><tr><th>Oportunidad</th><th>Tipo</th><th>Forecast</th><th className="num">MRR est.</th><th>Cierre est.</th><th>Propietario</th></tr></thead>
|
||
|
|
<tbody>
|
||
|
|
{c.oportunidades.map((o) => (
|
||
|
|
<tr key={o.id}>
|
||
|
|
<td><a className="opp" href={`${DYN}${o.id}`} target="_blank" rel="noopener">{o.nombre}</a></td>
|
||
|
|
<td>{o.tipo || "—"}</td>
|
||
|
|
<td>{o.forecast || "—"}</td>
|
||
|
|
<td className="num">{money(o.valor)}</td>
|
||
|
|
<td>{fecha(o.cierre)}</td>
|
||
|
|
<td>{o.propietario || "—"}</td>
|
||
|
|
</tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
) : <p className="hint" style={{ margin: 0 }}>Sin oportunidades abiertas.</p>}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="panel">
|
||
|
|
<h3>Contactos ({c.contactos.length})</h3>
|
||
|
|
{c.contactos.length ? (
|
||
|
|
<table className="cta-tabla">
|
||
|
|
<thead><tr><th>Nombre</th><th>Cargo</th><th>Correo</th><th>Teléfono</th></tr></thead>
|
||
|
|
<tbody>
|
||
|
|
{c.contactos.map((k, i) => (
|
||
|
|
<tr key={i}><td>{k.nombre}</td><td>{k.cargo || "—"}</td><td>{k.correo || "—"}</td><td>{k.movil || "—"}</td></tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
) : <p className="hint" style={{ margin: 0 }}>Sin contactos activos registrados.</p>}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="panel">
|
||
|
|
<h3>Últimas actividades</h3>
|
||
|
|
<p className="hint">De la cuenta y de sus oportunidades abiertas. Haz clic en una abierta para gestionarla.</p>
|
||
|
|
{c.actividades.length ? (
|
||
|
|
<table className="cta-tabla">
|
||
|
|
<thead><tr><th>Tipo</th><th>Asunto</th><th>Referente</th><th>Fecha</th><th>Estado</th><th>Propietario</th></tr></thead>
|
||
|
|
<tbody>
|
||
|
|
{c.actividades.map((a) => (
|
||
|
|
<tr key={a.id}>
|
||
|
|
<td>{a.tipo === "Cita" ? "Visita" : a.tipo}</td>
|
||
|
|
<td>
|
||
|
|
{a.estado === "abierta" ? (
|
||
|
|
<GestionarActividad a={{ ...a, referente: null }} className="gst-link" title={a.asunto}>{a.asunto}</GestionarActividad>
|
||
|
|
) : a.asunto}
|
||
|
|
</td>
|
||
|
|
<td className="elip">{a.referente || "—"}</td>
|
||
|
|
<td>{fecha(diaLocal(a.scheduledend || a.scheduledstart))}</td>
|
||
|
|
<td>{a.estado}</td>
|
||
|
|
<td>{a.owner || "—"}</td>
|
||
|
|
</tr>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</table>
|
||
|
|
) : <p className="hint" style={{ margin: 0 }}>Sin actividades registradas.</p>}
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|