c912afd46a
Extends the previous loader-only sweep across the full tree: every remaining `await locals.auth()` now falls back through `locals.session ?? await locals.auth()`, so the hook's cached result is reused. 68 files, 107 sites touched — loaders, form actions, and API endpoints across cospend / tasks / fitness / faith / recipe / admin. hooks.server.ts is intentionally left alone since it's the originating call that populates locals.session in the first place.
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { json } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { dbConnect } from '$utils/db';
|
|
import { IntervalTemplate, validateIntervalEntries } from '$models/IntervalTemplate';
|
|
|
|
export const GET: RequestHandler = async ({ locals }) => {
|
|
const session = locals.session ?? await locals.auth();
|
|
if (!session || !session.user?.nickname) {
|
|
return json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
await dbConnect();
|
|
const templates = await IntervalTemplate.find({ createdBy: session.user.nickname }).sort({ updatedAt: -1 });
|
|
return json({ templates });
|
|
} catch (error) {
|
|
console.error('Error fetching interval templates:', error);
|
|
return json({ error: 'Failed to fetch interval templates' }, { status: 500 });
|
|
}
|
|
};
|
|
|
|
export const POST: RequestHandler = async ({ request, locals }) => {
|
|
const session = locals.session ?? await locals.auth();
|
|
if (!session || !session.user?.nickname) {
|
|
return json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
await dbConnect();
|
|
const data = await request.json();
|
|
const { name, steps } = data;
|
|
|
|
if (!name || !steps) {
|
|
return json({ error: 'Name and at least one step are required' }, { status: 400 });
|
|
}
|
|
|
|
const validationError = validateIntervalEntries(steps);
|
|
if (validationError) return json({ error: validationError }, { status: 400 });
|
|
|
|
const template = new IntervalTemplate({
|
|
name,
|
|
steps,
|
|
createdBy: session.user.nickname
|
|
});
|
|
|
|
await template.save();
|
|
return json({ template }, { status: 201 });
|
|
} catch (error) {
|
|
console.error('Error creating interval template:', error);
|
|
return json({ error: 'Failed to create interval template' }, { status: 500 });
|
|
}
|
|
};
|