import { auth } from "../lib/auth";
import { ownerFilter } from "../lib/authz";
import { getActividades, getSystemUsers } from "../lib/queries";
import { agg, aggOrd, actAncla, actEstadoFinal, EJECUTIVOS_VENTA, ETAPA_ORDER } from "../lib/derive";
import LoginPrompt from "../components/LoginPrompt";
import OwnerControl from "../components/OwnerControl";
import ErrorNotice from "../components/ErrorNotice";
import Panel from "../components/pipeline/Panel";
import { actividadUrl } from "../lib/constants";
export const dynamic = "force-dynamic";
function fechaHoraTxt(a, fmt) {
return fmt || "—";
}
function TablaTipo({ titulo, rows, scope }) {
if (!rows.length) return null;
return (
{titulo} — {rows.length}
| Asunto |
Referente |
Objetivo |
{!scope.locked && Ejecutivo | }
Estado |
Fecha |
{rows.map((a) => (
|
{a.asunto}
|
{a.referente?.nombre || "—"} |
{a.objetivo || "—"} |
{!scope.locked && {a.owner} | }
{a.estadoFinal}
|
{fechaHoraTxt(a, a.scheduledendFmt || a.scheduledstartFmt)} |
))}
);
}
export default async function ActividadesPage({ searchParams }) {
const session = await auth();
if (!session?.email) return ;
const { owner } = await searchParams;
const scope = ownerFilter(session, owner);
let raw, allUsers;
try {
[raw, allUsers] = await Promise.all([
getActividades(session.accessToken, scope),
session.isAdmin ? getSystemUsers(session.accessToken) : Promise.resolve([]),
]);
} catch (error) {
return ;
}
const owners = allUsers.filter((u) => EJECUTIVOS_VENTA.includes(u.name));
const actividades = raw.filter((a) => a.estado !== "cancelada").map((a) => ({ ...a, estadoFinal: actEstadoFinal(a) }));
const completadas = actividades.filter((a) => a.estadoFinal === "Completada");
const vencidas = actividades.filter((a) => a.estadoFinal === "Vencida");
const pendientes = actividades.filter((a) => a.estadoFinal === "Pendiente");
const cumplimiento = actividades.length ? Math.round((completadas.length / actividades.length) * 100) : 0;
const conObjetivo = actividades.filter((a) => a.objetivo).length;
const conOportunidad = actividades.filter((a) => a.referente?.tipo === "opportunity").length;
const porEjecutivo = EJECUTIVOS_VENTA.map((nombre) => {
const propias = actividades.filter((a) => a.owner === nombre);
const hechas = propias.filter((a) => a.estadoFinal === "Completada").length;
const pct = propias.length ? Math.round((hechas / propias.length) * 100) : null;
return { nombre, total: propias.length, hechas, pct };
}).sort((a, b) => (b.pct ?? -1) - (a.pct ?? -1));
return (
Plan de Actividades
No hay seguimiento registrado en el CRM ≠ no hubo seguimiento — la mayoría del contacto comercial se hace por
WhatsApp, que no está integrado a Dynamics (ver CLAUDE.md). Ventana: últimos 90 días cerrados + todo lo que
sigue abierto.
{actividades.length}
Actividades (sin canceladas)
{cumplimiento}%
Cumplimiento (completadas/total)
{pendientes.length}
Pendientes
{vencidas.length}
Vencidas
Con Objetivo de Actividad asignado: {conObjetivo} ({actividades.length ? Math.round((conObjetivo / actividades.length) * 100) : 0}%) —
adopción real del campo. Referidas a Oportunidad: {conOportunidad} ({actividades.length ? Math.round((conOportunidad / actividades.length) * 100) : 0}%).
Cumplimiento por ejecutivo
Completadas ÷ total de actividades (sin canceladas) de cada quien
{porEjecutivo.map((r) => (
{r.nombre}
= 70 ? "" : r.pct >= 50 ? "alerta" : "pierde"}`}
style={{ width: `${Math.min(100, r.pct ?? 0)}%` }}
/>
{r.pct == null ? "n/d" : `${r.pct}%`}
{r.hechas}/{r.total}
))}
Detalle
a.tipo === "Cita")} scope={scope} />
a.tipo === "Llamada")} scope={scope} />
a.tipo === "Tarea")} scope={scope} />
);
}