feat(overdracht): E0 + E1 - Database setup en API nursing logs
Epic 0 - Database Setup: - nursing_logs tabel met indexes en constraints - RLS policies (SELECT/INSERT/UPDATE/DELETE) - TypeScript types gegenereerd Epic 1 - API Nursing Logs: - GET/POST /api/nursing-logs (lijst + aanmaken) - PATCH/DELETE /api/nursing-logs/[id] (bewerken + verwijderen) - Zod validatie schemas - Automatische shift_date berekening Documentatie: - PRD, FO, TO en Bouwplan voor Overdracht Dashboard 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
182
app/api/nursing-logs/[id]/route.ts
Normal file
182
app/api/nursing-logs/[id]/route.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { createClient } from '@/lib/auth/server';
|
||||
import {
|
||||
UpdateNursingLogSchema,
|
||||
calculateShiftDate,
|
||||
} from '@/lib/types/nursing-log';
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
if (!z.string().uuid().safeParse(id).success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'id moet een geldige UUID zijn' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const result = UpdateNursingLogSchema.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Validatiefout',
|
||||
details: result.error.issues.map((issue) => ({
|
||||
field: issue.path.join('.'),
|
||||
message: issue.message,
|
||||
})),
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const { data: authData } = await supabase.auth.getUser();
|
||||
|
||||
if (!authData?.user) {
|
||||
return NextResponse.json({ error: 'Niet geautoriseerd' }, { status: 401 });
|
||||
}
|
||||
|
||||
// Check if log exists and belongs to current user
|
||||
const { data: existingLog, error: fetchError } = await supabase
|
||||
.from('nursing_logs')
|
||||
.select('id, created_by')
|
||||
.eq('id', id)
|
||||
.single();
|
||||
|
||||
if (fetchError || !existingLog) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Dagnotitie niet gevonden' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (existingLog.created_by !== authData.user.id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Je kunt alleen je eigen notities bewerken' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// Build update object
|
||||
const updateData: Record<string, unknown> = {};
|
||||
const { category, content, timestamp, include_in_handover } = result.data;
|
||||
|
||||
if (category !== undefined) updateData.category = category;
|
||||
if (content !== undefined) updateData.content = content;
|
||||
if (include_in_handover !== undefined)
|
||||
updateData.include_in_handover = include_in_handover;
|
||||
|
||||
// If timestamp changes, recalculate shift_date
|
||||
if (timestamp !== undefined) {
|
||||
updateData.timestamp = timestamp;
|
||||
updateData.shift_date = calculateShiftDate(timestamp);
|
||||
}
|
||||
|
||||
if (Object.keys(updateData).length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Geen velden om te updaten' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('nursing_logs')
|
||||
.update(updateData)
|
||||
.eq('id', id)
|
||||
.select('*')
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating nursing log:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Bijwerken mislukt', details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error('Unexpected error in PATCH /api/nursing-logs/[id]:', error);
|
||||
if (error instanceof SyntaxError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Ongeldige JSON in request body' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: 'Onverwachte serverfout' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
if (!z.string().uuid().safeParse(id).success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'id moet een geldige UUID zijn' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const { data: authData } = await supabase.auth.getUser();
|
||||
|
||||
if (!authData?.user) {
|
||||
return NextResponse.json({ error: 'Niet geautoriseerd' }, { status: 401 });
|
||||
}
|
||||
|
||||
// Check if log exists and belongs to current user
|
||||
const { data: existingLog, error: fetchError } = await supabase
|
||||
.from('nursing_logs')
|
||||
.select('id, created_by')
|
||||
.eq('id', id)
|
||||
.single();
|
||||
|
||||
if (fetchError || !existingLog) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Dagnotitie niet gevonden' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (existingLog.created_by !== authData.user.id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Je kunt alleen je eigen notities verwijderen' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// Hard delete (RLS policy already ensures user can only delete own logs)
|
||||
const { error } = await supabase
|
||||
.from('nursing_logs')
|
||||
.delete()
|
||||
.eq('id', id);
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting nursing log:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Verwijderen mislukt', details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return new NextResponse(null, { status: 204 });
|
||||
} catch (error) {
|
||||
console.error('Unexpected error in DELETE /api/nursing-logs/[id]:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Onverwachte serverfout' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
146
app/api/nursing-logs/route.ts
Normal file
146
app/api/nursing-logs/route.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { createClient } from '@/lib/auth/server';
|
||||
import {
|
||||
CreateNursingLogSchema,
|
||||
calculateShiftDate,
|
||||
type NursingLogListResponse,
|
||||
} from '@/lib/types/nursing-log';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const patientId = searchParams.get('patientId');
|
||||
const date = searchParams.get('date'); // Optional: YYYY-MM-DD format
|
||||
|
||||
if (!patientId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'patientId query parameter is verplicht' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!z.string().uuid().safeParse(patientId).success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'patientId moet een geldige UUID zijn' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Validate date format if provided
|
||||
if (date && !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'date moet in YYYY-MM-DD formaat zijn' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
|
||||
let query = supabase
|
||||
.from('nursing_logs')
|
||||
.select('*')
|
||||
.eq('patient_id', patientId)
|
||||
.order('timestamp', { ascending: false });
|
||||
|
||||
// Filter by shift_date if date is provided
|
||||
if (date) {
|
||||
query = query.eq('shift_date', date);
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching nursing logs:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Fout bij ophalen dagnotities', details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const response: NursingLogListResponse = {
|
||||
logs: data ?? [],
|
||||
total: data?.length ?? 0,
|
||||
};
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
console.error('Unexpected error in GET /api/nursing-logs:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Onverwachte serverfout' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const result = CreateNursingLogSchema.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Validatiefout',
|
||||
details: result.error.issues.map((issue) => ({
|
||||
field: issue.path.join('.'),
|
||||
message: issue.message,
|
||||
})),
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const { data: authData } = await supabase.auth.getUser();
|
||||
|
||||
if (!authData?.user) {
|
||||
return NextResponse.json({ error: 'Niet geautoriseerd' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { patient_id, category, content, timestamp, include_in_handover } =
|
||||
result.data;
|
||||
|
||||
// Use provided timestamp or current time
|
||||
const logTimestamp = timestamp || new Date().toISOString();
|
||||
|
||||
// Calculate shift_date from timestamp
|
||||
const shiftDate = calculateShiftDate(logTimestamp);
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('nursing_logs')
|
||||
.insert({
|
||||
patient_id,
|
||||
category,
|
||||
content,
|
||||
timestamp: logTimestamp,
|
||||
shift_date: shiftDate,
|
||||
include_in_handover: include_in_handover ?? false,
|
||||
created_by: authData.user.id,
|
||||
})
|
||||
.select('*')
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Error creating nursing log:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Opslaan mislukt', details: error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(data, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Unexpected error in POST /api/nursing-logs:', error);
|
||||
if (error instanceof SyntaxError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Ongeldige JSON in request body' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: 'Onverwachte serverfout' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user