import Link from "next/link"; import { redirect } from "next/navigation"; import { SectionCard } from "@/components/ui"; import { ShellPage } from "@/components/page-templates"; import { getSession } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; function formatDate(value: Date | null | undefined) { if (!value) { return "-"; } return new Intl.DateTimeFormat("id-ID", { day: "2-digit", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" }).format(value); } function formatMoney(value: number) { return new Intl.NumberFormat("id-ID", { style: "currency", currency: "IDR", maximumFractionDigits: 0 }).format(value); } export default async function TenantDetailPage({ params }: { params: Promise<{ tenantId: string }> }) { const session = await getSession(); if (!session || session.role !== "super_admin") { redirect("/unauthorized"); } const { tenantId } = await params; const tenant = await prisma.tenant.findUnique({ where: { id: tenantId }, include: { plan: true, channels: { orderBy: { createdAt: "desc" }, include: { _count: { select: { conversations: true } } } }, users: { orderBy: { fullName: "asc" } }, contacts: { select: { id: true } }, billingInvoices: { include: { plan: true }, orderBy: { dueDate: "desc" }, take: 5 } } }); if (!tenant) { redirect("/super-admin/tenants?error=tenant_not_found"); } const [openConversationCount, invoiceTotal, unresolvedWebhook] = await Promise.all([ prisma.conversation.count({ where: { tenantId, status: { in: ["OPEN", "PENDING"] } } }), prisma.billingInvoice.aggregate({ where: { tenantId }, _sum: { totalAmount: true } }), prisma.webhookEvent.count({ where: { tenantId, processStatus: "failed" } }) ]); return (

Name: {tenant.name}

Company: {tenant.companyName}

Slug: {tenant.slug}

Status: {tenant.status}

Timezone: {tenant.timezone}

Plan: {tenant.plan.name}

Seats: {tenant.users.length}/{tenant.plan.seatQuota}

Contacts: {tenant.contacts.length}

Open/Pending conversations: {openConversationCount}
Unresolved webhook events: {unresolvedWebhook}
Outstanding invoices: {tenant.billingInvoices.filter((invoice) => invoice.paymentStatus !== "PAID").length}

Edit tenant Connect channel
{tenant.channels.length === 0 ?

No channels connected.

: null}
    {tenant.channels.map((channel) => (
  • {channel.channelName}

    {channel.displayPhoneNumber || "No number"}

    Status: {channel.status}

    Conversations: {channel._count.conversations}

    Open detail
  • ))}
{tenant.billingInvoices.length === 0 ?

No invoices found.

: null}
    {tenant.billingInvoices.map((invoice) => (
  • {invoice.invoiceNumber}

    {invoice.plan.name} • {formatMoney(invoice.totalAmount)} • {invoice.paymentStatus}

    Due: {formatDate(invoice.dueDate)}

    View
  • ))}
{tenant.users.length === 0 ?

No users.

: null}
    {tenant.users.map((user) => (
  • {user.fullName}

    {user.email}

  • ))}

Total invoice amount: {formatMoney(invoiceTotal._sum.totalAmount ?? 0)}

); }