import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { useToast } from "@/hooks/use-toast"; import { useAuth } from "@/hooks/use-auth"; import { Plus, ClipboardCheck, Clock, CheckCircle, AlertCircle } from "lucide-react"; import { format } from "date-fns"; import { Appointment, Patient } from "@repo/db/types"; export default function PreAuthorizationsPage() { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [isPreAuthFormOpen, setIsPreAuthFormOpen] = useState(false); const [selectedPatient, setSelectedPatient] = useState(null); const [selectedProcedure, setSelectedProcedure] = useState(null); const { toast } = useToast(); const { user } = useAuth(); // Fetch patients const { data: patients = [], isLoading: isLoadingPatients } = useQuery({ queryKey: ["/api/patients"], enabled: !!user, }); // Fetch appointments const { data: appointments = [] as Appointment[], isLoading: isLoadingAppointments } = useQuery({ queryKey: ["/api/appointments"], enabled: !!user, }); const toggleMobileMenu = () => { setIsMobileMenuOpen(!isMobileMenuOpen); }; const handleNewPreAuth = (patientId: number, procedure: string) => { setSelectedPatient(patientId); setSelectedProcedure(procedure); setIsPreAuthFormOpen(true); // Show a toast notification of success const patient = patients.find(p => p.id === patientId); toast({ title: "Pre-authorization Request Started", description: `Started pre-auth for ${patient?.firstName} ${patient?.lastName} - ${procedure}`, }); }; // Common dental procedures requiring pre-authorization const dentalProcedures = [ { code: "D2740", name: "Crown - porcelain/ceramic" }, { code: "D2950", name: "Core buildup, including any pins" }, { code: "D3330", name: "Root Canal - molar" }, { code: "D4341", name: "Periodontal scaling & root planing" }, { code: "D4910", name: "Periodontal maintenance" }, { code: "D5110", name: "Complete denture - maxillary" }, { code: "D6010", name: "Surgical placement of implant body" }, { code: "D7240", name: "Removal of impacted tooth" }, ]; // Get patients with active insurance const patientsWithInsurance = patients.filter(patient => patient.insuranceProvider && patient.insuranceId ); // Sample pre-authorization data const samplePreAuths = [ { id: "PA2023-001", patientId: patientsWithInsurance[0]?.id || 1, procedureCode: "D2740", procedureName: "Crown - porcelain/ceramic", requestDate: new Date(new Date().setDate(new Date().getDate() - 14)), status: "approved", approvalDate: new Date(new Date().setDate(new Date().getDate() - 7)), expirationDate: new Date(new Date().setMonth(new Date().getMonth() + 6)), }, { id: "PA2023-002", patientId: patientsWithInsurance[0]?.id || 1, procedureCode: "D3330", procedureName: "Root Canal - molar", requestDate: new Date(new Date().setDate(new Date().getDate() - 5)), status: "pending", approvalDate: null, expirationDate: null, }, { id: "PA2023-003", patientId: patientsWithInsurance[0]?.id || 1, procedureCode: "D7240", procedureName: "Removal of impacted tooth", requestDate: new Date(new Date().setDate(new Date().getDate() - 30)), status: "denied", approvalDate: null, expirationDate: null, denialReason: "Not medically necessary based on submitted documentation", } ]; return (
{/* Header */}

Pre-authorizations

Manage insurance pre-authorizations for dental procedures

{/* New Pre-Authorization Request Section */}

New Pre-Authorization Request

Recent Patients for Pre-Authorization {isLoadingPatients ? (
Loading patients data...
) : patientsWithInsurance.length > 0 ? (
{patientsWithInsurance.map((patient) => (
{ setSelectedPatient(Number(patient.id)); handleNewPreAuth( patient.id, dentalProcedures[Math.floor(Math.random() * 3)].name ); }} >

{patient.firstName} {patient.lastName}

Insurance: {patient.insuranceProvider === 'delta' ? 'Delta Dental' : patient.insuranceProvider === 'metlife' ? 'MetLife' : patient.insuranceProvider === 'cigna' ? 'Cigna' : patient.insuranceProvider === 'aetna' ? 'Aetna' : patient.insuranceProvider} ID: {patient.insuranceId} Procedure needed: {dentalProcedures[0].name}
))}
) : (

No patients with insurance

Add insurance information to patients to request pre-authorizations

)}
{/* Pre-Authorization Submitted Section */}

Pre-Authorization Submitted

Pending Pre-Authorization Requests {patientsWithInsurance.length > 0 ? (
{samplePreAuths.filter(auth => auth.status === 'pending').map((preAuth) => { const patient = patients.find(p => p.id === preAuth.patientId) || { firstName: "Unknown", lastName: "Patient" }; return (
toast({ title: "Pre-Authorization Details", description: `Viewing details for ${preAuth.id}` })} >

{patient.firstName} {patient.lastName} - {preAuth.procedureName}

ID: {preAuth.id} Submitted: {format(preAuth.requestDate, 'MMM dd, yyyy')} Expected Response: {format(new Date(preAuth.requestDate.getTime() + 7 * 24 * 60 * 60 * 1000), 'MMM dd, yyyy')}
Pending
); })} {samplePreAuths.filter(auth => auth.status === 'pending').length === 0 && (

No pending requests

Submitted pre-authorization requests will appear here

)}
) : (

No pre-authorization history

Submitted pre-authorization requests will appear here

)}
{/* Pre-Authorization Results Section */}

Pre-Authorization Results

Completed Pre-Authorization Requests {patientsWithInsurance.length > 0 ? (
{samplePreAuths.filter(auth => auth.status !== 'pending').map((preAuth) => { const patient = patients.find(p => p.id === preAuth.patientId) || { firstName: "Unknown", lastName: "Patient" }; return (
toast({ title: "Pre-Authorization Details", description: `Viewing details for ${preAuth.id}` })} >

{patient.firstName} {patient.lastName} - {preAuth.procedureName}

ID: {preAuth.id} Requested: {format(preAuth.requestDate, 'MMM dd, yyyy')} {preAuth.status === 'approved' && ( <> Expires: {format(preAuth.expirationDate as Date, 'MMM dd, yyyy')} )} {preAuth.status === 'denied' && preAuth.denialReason && ( <> Reason: {preAuth.denialReason} )}
{preAuth.status === 'approved' ? ( Approved ) : ( Denied )}
); })} {samplePreAuths.filter(auth => auth.status !== 'pending').length === 0 && (

No completed requests

Processed pre-authorization results will appear here

)}
) : (

No pre-authorization results

Completed pre-authorization requests will appear here

)}
); }