Files
mealprep/frontend/src/routes/(app)/planner/+server.ts
Marcel Raddatz a751b0758a feat(planner): add server.test.ts for GET /planner, fix sort + add error handling
- Sort uses scoreDelta instead of removed simulatedScore
- try/catch degrades gracefully to suggestions=[] on backend errors
- 6 tests cover: missing params, success, backend error, network throw, empty result

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 16:33:12 +02:00

29 lines
822 B
TypeScript

import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { apiClient } from '$lib/server/api';
// GET /planner?planId=&date= — returns suggestions JSON for C4 recipe picker
export const GET: RequestHandler = async ({ fetch, url }) => {
const planId = url.searchParams.get('planId');
const date = url.searchParams.get('date');
if (!planId || !date) {
return json({ suggestions: [] });
}
try {
const api = apiClient(fetch);
const { data } = await api.GET('/v1/week-plans/{id}/suggestions', {
params: { path: { id: planId }, query: { slotDate: date } }
});
const suggestions = (data?.suggestions ?? []).sort(
(a: any, b: any) => (b.scoreDelta ?? 0) - (a.scoreDelta ?? 0)
);
return json({ suggestions });
} catch {
return json({ suggestions: [] });
}
};