refactor(cortex): consistente datumbepaling met referenceDate parameter
SOC: Parser en entity-extractor accepteren nu optionele referenceDate parameter. Caller bepaalt wat "nu" is, niet de functies intern. Wijzigingen: - parseRelativeDate(input, referenceDate?) - referenceDate parameter - extractEntities(input, intent, referenceDate?) - doorgeeft aan parser - API routes gebruiken new Date() als referenceDate Verwijderd (YAGNI): - lib/utils/server-date.ts - wrapper om new Date() was overbodig 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@ 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
|
||||
@@ -12,14 +14,15 @@ import { z } from 'zod';
|
||||
* Automatically filters by current user (practitioner_id).
|
||||
*/
|
||||
|
||||
// Query parameter schema
|
||||
// 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) {
|
||||
@@ -42,15 +45,9 @@ export async function GET(request: NextRequest) {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const start = searchParams.get('start');
|
||||
const end = searchParams.get('end');
|
||||
const label = searchParams.get('label');
|
||||
|
||||
if (!start || !end) {
|
||||
return NextResponse.json(
|
||||
{ error: 'start en end parameters zijn verplicht' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const validation = QuerySchema.safeParse({ start, end });
|
||||
const validation = QuerySchema.safeParse({ start, end, label });
|
||||
if (!validation.success) {
|
||||
const errorMessage = validation.error.issues
|
||||
.map((e) => e.message)
|
||||
@@ -58,18 +55,60 @@ export async function GET(request: NextRequest) {
|
||||
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: start,
|
||||
endDate: end,
|
||||
practitionerId: user.id,
|
||||
startDate: effectiveStart,
|
||||
endDate: effectiveEnd,
|
||||
// practitionerId: user.id, // Disabled to show all appointments
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
appointments,
|
||||
count: appointments.length,
|
||||
dateRange: { start, end },
|
||||
dateRange: {
|
||||
start: effectiveStart,
|
||||
end: effectiveEnd,
|
||||
label: effectiveLabel,
|
||||
},
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
@@ -95,7 +95,7 @@ export async function POST(request: NextRequest) {
|
||||
};
|
||||
} else {
|
||||
// High confidence local result - extract entities locally
|
||||
const entities = extractEntities(input, localResult.intent);
|
||||
const entities = extractEntities(input, localResult.intent, new Date());
|
||||
|
||||
finalResult = {
|
||||
intent: localResult.intent,
|
||||
|
||||
Reference in New Issue
Block a user