innercontext/frontend/src/routes/health/medications/new/+page.server.ts

34 lines
932 B
TypeScript

import { createMedication } from '$lib/api';
import { fail, redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async () => {
return {};
};
export const actions: Actions = {
default: async ({ request }) => {
const form = await request.formData();
const kind = form.get('kind') as string;
const product_name = form.get('product_name') as string;
const active_substance = form.get('active_substance') as string;
const notes = form.get('notes') as string;
if (!kind || !product_name) {
return fail(400, { error: 'Kind and product name are required' });
}
try {
await createMedication({
kind,
product_name,
active_substance: active_substance || undefined,
notes: notes || undefined
});
} catch (error) {
return fail(500, { error: (error as Error).message });
}
redirect(303, '/health/medications');
}
};