import Link from "next/link"; import { auth } from "../lib/auth"; import { ownerFilter } from "../lib/authz"; import { getPipeline, getClosed } from "../lib/queries"; import LoginPrompt from "../components/LoginPrompt"; import ErrorNotice from "../components/ErrorNotice"; import { DYN, ACT_COLOR, money2 } from "../lib/constants"; export const dynamic = "force-dynamic"; function currentMonth() { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`; } const SETS = [ { key: "pipe", label: "Tubería activa" }, { key: "ganadas", label: "Ganadas" }, { key: "perdidas", label: "Perdidas" }, ]; export default async function DetallePage({ searchParams }) { const session = await auth(); if (!session?.email) return ; const { owner, set } = await searchParams; const scope = ownerFilter(session, owner); const detSet = SETS.some((s) => s.key === set) ? set : "pipe"; const mes = currentMonth(); let rows; try { if (detSet === "pipe") rows = await getPipeline(session.accessToken, scope); else if (detSet === "ganadas") rows = await getClosed(session.accessToken, scope, 1, mes); else rows = await getClosed(session.accessToken, scope, 2, mes); } catch (error) { return ; } const sorted = [...rows].sort((a, b) => (b.est ?? b.actual ?? 0) - (a.est ?? a.actual ?? 0)); const qs = (key) => { const p = new URLSearchParams(); if (owner) p.set("owner", owner); p.set("set", key); return `/detalle?${p.toString()}`; }; return (

Detalle

{SETS.map((s) => ( ))}

{rows.length} oportunidades{detSet !== "pipe" ? ` (${mes})` : ""} · clic en el nombre abre la oportunidad en Dynamics 365.

{sorted.length === 0 ? (

No hay oportunidades para este filtro.

) : (
{!scope.locked && } {detSet === "pipe" ? ( <> ) : ( <> )} {sorted.map((o) => ( {!scope.locked && } {detSet === "pipe" ? ( <> ) : ( <> )} ))}
Cliente Oportunidad Seg.EjecutivoPlan cierre Fase actual Actividad MRR est. ForecastTipo MRR Cierre
{o.cliente || "—"} {o.name} {o.segmento === "Cartera Activa" ? "CA" : "CN"} {o.owner}{(o.plan || "").replace(" - ", " ")} {o.faseActual || "—"} ● {o.actividad} {money2(o.est)} {money2(o.fc)}{o.tipo} {money2(o.actual)} {o.actualClose || "—"}
)}
); }