'use client'; import { useState } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { Skeleton } from '@/components/ui/skeleton'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { ThemeToggle } from '@/components/theme-toggle'; import { Search, Server, FileText, DollarSign, Calendar, AlertCircle, CheckCircle, Ticket, Info } from 'lucide-react'; import { format } from 'date-fns'; interface EnrichmentData { configItem: any; billingItems: any[]; invoices: any[]; tickets: any[]; summary: { totalBilled: number; purchaseDate?: string; lastInvoiceDate?: string; ticketCount: number; }; } export default function ConfigEnrichmentTestPage() { const [configItemId, setConfigItemId] = useState(''); const [invoiceId, setInvoiceId] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [data, setData] = useState(null); const handleSearch = async () => { if (!configItemId && !invoiceId) return; setLoading(true); setError(null); try { const params = new URLSearchParams(); if (invoiceId) { params.append('invoiceId', invoiceId); } else if (configItemId) { params.append('configItemId', configItemId); } const response = await fetch(`/api/config-enrichment?${params.toString()}`); if (!response.ok) { throw new Error('Failed to fetch enrichment data'); } const result = await response.json(); setData(result); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; return (
{/* Header */}

Config Item Enrichment Test

Find invoices and tickets by serial number

{/* Main Content */}
{/* Search Card */} Search Configuration Item Enter a Configuration Item ID to find related invoices and tickets (last 90 days)
{ setConfigItemId(e.target.value); if (e.target.value) setInvoiceId(''); }} onKeyDown={(e) => e.key === 'Enter' && handleSearch()} />
{ setInvoiceId(e.target.value); if (e.target.value) setConfigItemId(''); }} onKeyDown={(e) => e.key === 'Enter' && handleSearch()} />
{/* Error Display */} {error && (

Error: {error}

)} {/* Loading State */} {loading && (
)} {/* Results */} {data && !loading && ( <> {/* Config Item Info */} Configuration Item Details

{data.configItem?.referenceTitle || '-'}

{data.configItem?.serialNumber || '-'}

{data.configItem?.installDate ? format(new Date(data.configItem.installDate), 'MMM d, yyyy') : '-'}

{data.configItem?.isActive ? 'Active' : 'Inactive'}
{/* Summary Cards */}

Total Billed

${data.summary.totalBilled.toFixed(2)}

Invoices Found

{data.invoices.length}

Tickets Found

{data.summary.ticketCount}

Purchase Date

{data.summary.purchaseDate ? format(new Date(data.summary.purchaseDate), 'MMM d, yyyy') : '-'}

{/* Invoices */} {data.invoices.length > 0 && ( Company Invoices (Last 90 Days) All invoices for {data.configItem?.companyName || 'this company'} Invoice # Date Total Status Due Date {data.invoices.map((invoice) => ( {invoice.invoiceNumber || invoice.id} {invoice.invoiceDateTime ? format(new Date(invoice.invoiceDateTime), 'MMM d, yyyy') : '-'} ${(invoice.invoiceTotal || 0).toFixed(2)} {invoice.paidDate ? 'Paid' : 'Unpaid'} {invoice.dueDate ? format(new Date(invoice.dueDate), 'MMM d, yyyy') : '-'} ))}
)} {/* Billing Items */} {data.billingItems.length > 0 && ( {data.configItem ? 'Purchase History for This Device' : 'Hardware Purchases'} {data.configItem ? `Billing items matching serial number ${data.configItem.serialNumber} (last 90 days)` : 'Hardware line items from invoice'} Date Description Serial/Notes Qty Unit Price Total Invoice {data.billingItems.map((item, index) => ( {item.itemDate ? format(new Date(item.itemDate), 'MMM d, yyyy') : '-'}
{item.description || '-'}
{item.itemName && item.itemName !== item.description && (
{item.itemName}
)}
{item.serialNumber && (
SN: {item.serialNumber}
)} {item.internalNotes && (
{item.internalNotes}
)} {item.vendorInvoiceNumber && (
Vendor: {item.vendorInvoiceNumber}
)}
{item.quantity || 1} ${(item.unitPrice || 0).toFixed(2)} ${(item.totalAmount || 0).toFixed(2)} {item.invoiceID}
))}
)} {/* Tickets */} {data.tickets.length > 0 && ( Related Tickets Tickets mentioning this serial number (last 90 days) Ticket # Title Status Created Priority {data.tickets.map((ticket) => ( {ticket.ticketNumber} {ticket.title} {ticket.status} {format(new Date(ticket.createDate), 'MMM d, yyyy')} {ticket.priority} ))}
)} {/* Debug: Show all fields from first billing item - only when searching by invoice */} {data.billingItems.length > 0 && !data.configItem && invoiceId && ( Debug: Available Fields (First Item) All fields available in BillingItems entity
                    {JSON.stringify(data.billingItems[0], null, 2)}
                  
)} {/* No Results */} {data.billingItems.length === 0 && data.tickets.length === 0 && (
{data.configItem ? ( <>

No purchase records found for this device

Serial number: {data.configItem.serialNumber || 'Not set'}

This device may have been:

  • • Purchased more than 1 year ago
  • • Added manually without an invoice
  • • Invoiced without serial number in description
) : ( <>

No billing items or tickets found in the last 90 days

Try a different configuration item or expand the date range

)}
)} )}
); }