Files
triqura-ecd/app/api/cortex/agenda/route.ts
colinislit 52e07aaf80 fix(cortex): update date handling in API and UI components
- Adjusted date handling in the agenda API to ensure optional parameters for start, end, and label are correctly processed.
- Enhanced chat input and action parser to support optional date labels for relative dates.
- Updated agenda components to handle date ranges and loading states more effectively.
- Improved error handling and loading indicators in the agenda block for better user experience.

This commit ensures consistency in date handling across the application, aligning with the new requirements for relative date inputs.
2026-01-02 09:21:37 +01:00

127 lines
4.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/lib/auth/server';
import { getEncounters } from '@/app/epd/agenda/actions';
import { z } from 'zod';
import { startOfDay, endOfDay } from 'date-fns';
import { parseRelativeDate, isDateRange } from '@/lib/cortex/date-time-parser';
/**
* Swift Agenda Query API
*
* GET /api/swift/agenda?start=2024-12-27&end=2024-12-27
*
* Returns appointments for the specified date range.
* Automatically filters by current user (practitioner_id).
*/
// Query parameter schema - start/end zijn optioneel, default naar vandaag
const QuerySchema = z.object({
start: z.string().refine((val) => !isNaN(Date.parse(val)), {
message: 'start moet een geldige datum zijn',
}).optional(),
end: z.string().refine((val) => !isNaN(Date.parse(val)), {
message: 'end moet een geldige datum zijn',
}).optional(),
label: z.string().optional(), // Voor relatieve datums: vandaag, morgen, deze week, etc.
});
export async function GET(request: NextRequest) {
try {
// Auth check
const supabase = await createClient();
const {
data: { user },
error: authError,
} = await supabase.auth.getUser();
if (authError || !user) {
return NextResponse.json(
{ error: 'Niet geautoriseerd. Log opnieuw in.' },
{ status: 401 }
);
}
// Parse and validate query parameters
// Note: searchParams.get() returns null if not present, but Zod expects undefined
const searchParams = request.nextUrl.searchParams;
const start = searchParams.get('start') ?? undefined;
const end = searchParams.get('end') ?? undefined;
const label = searchParams.get('label') ?? undefined;
const validation = QuerySchema.safeParse({ start, end, label });
if (!validation.success) {
const errorMessage = validation.error.issues
.map((e) => e.message)
.join(', ');
return NextResponse.json({ error: errorMessage }, { status: 400 });
}
// Determine effective date range (server-side, consistent met EPD agenda)
const serverNow = new Date();
let effectiveStart: string;
let effectiveEnd: string;
let effectiveLabel: string;
if (start && end) {
// Expliciete datums meegegeven
effectiveStart = start;
effectiveEnd = end;
effectiveLabel = label || 'custom';
} else if (label) {
// Label meegegeven, server berekent de datum via centrale lib
// Pass serverNow als referenceDate voor consistentie
const parsed = parseRelativeDate(label, serverNow);
if (isDateRange(parsed)) {
effectiveStart = startOfDay(parsed.start).toISOString();
effectiveEnd = endOfDay(parsed.end).toISOString();
effectiveLabel = parsed.label;
} else if (parsed) {
effectiveStart = startOfDay(parsed).toISOString();
effectiveEnd = endOfDay(parsed).toISOString();
effectiveLabel = label;
} else {
// Label niet herkend, fallback naar vandaag
effectiveStart = startOfDay(serverNow).toISOString();
effectiveEnd = endOfDay(serverNow).toISOString();
effectiveLabel = 'vandaag';
}
} else {
// Geen params, default naar vandaag (server-side bepaald)
effectiveStart = startOfDay(serverNow).toISOString();
effectiveEnd = endOfDay(serverNow).toISOString();
effectiveLabel = 'vandaag';
}
// Fetch appointments
// Note: We don't filter by practitioner_id to show all appointments
// In a production system, this should be filtered by organization/team
const appointments = await getEncounters({
startDate: effectiveStart,
endDate: effectiveEnd,
// practitionerId: user.id, // Disabled to show all appointments
});
return NextResponse.json(
{
appointments,
count: appointments.length,
dateRange: {
start: effectiveStart,
end: effectiveEnd,
label: effectiveLabel,
},
},
{ status: 200 }
);
} catch (error) {
console.error('Error in agenda query API:', error);
return NextResponse.json(
{
error:
'Er ging iets mis bij het ophalen van afspraken. Probeer het opnieuw.',
},
{ status: 500 }
);
}
}