Epic 3 compleet: Backend integratie voor Swift Agenda Planning. Nieuwe endpoints: - GET /api/swift/agenda - Query afspraken op datumrange - POST /api/swift/agenda/create - Nieuwe afspraak aanmaken - POST /api/swift/agenda/cancel - Afspraak annuleren - POST /api/swift/agenda/reschedule - Afspraak verzetten - GET /api/swift/patients/search - Fuzzy patiënt zoeken Alle endpoints bevatten: - Supabase authenticatie + resource ownership checks - Zod validatie met Nederlandse foutmeldingen - Hergebruik van bestaande agenda server actions Test documentatie: docs/swift/test-plan-epic3-backend.md Progress: 3/7 Epics compleet (E0, E1, E2, E3) Story points: 11 SP (E3.S1: 3, E3.S2: 3, E3.S3: 3, E3.S4: 2)
87 lines
2.2 KiB
TypeScript
87 lines
2.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';
|
|
|
|
/**
|
|
* 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
|
|
const QuerySchema = z.object({
|
|
start: z.string().refine((val) => !isNaN(Date.parse(val)), {
|
|
message: 'start moet een geldige datum zijn',
|
|
}),
|
|
end: z.string().refine((val) => !isNaN(Date.parse(val)), {
|
|
message: 'end moet een geldige datum zijn',
|
|
}),
|
|
});
|
|
|
|
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
|
|
const searchParams = request.nextUrl.searchParams;
|
|
const start = searchParams.get('start');
|
|
const end = searchParams.get('end');
|
|
|
|
if (!start || !end) {
|
|
return NextResponse.json(
|
|
{ error: 'start en end parameters zijn verplicht' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const validation = QuerySchema.safeParse({ start, end });
|
|
if (!validation.success) {
|
|
const errorMessage = validation.error.issues
|
|
.map((e) => e.message)
|
|
.join(', ');
|
|
return NextResponse.json({ error: errorMessage }, { status: 400 });
|
|
}
|
|
|
|
// Fetch appointments
|
|
const appointments = await getEncounters({
|
|
startDate: start,
|
|
endDate: end,
|
|
practitionerId: user.id,
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{
|
|
appointments,
|
|
count: appointments.length,
|
|
dateRange: { start, end },
|
|
},
|
|
{ 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 }
|
|
);
|
|
}
|
|
}
|