Stundenaufzeichnung/src/routes/dokumente/+page.server.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-02-04 21:41:16 +01:00
import type { PageServerLoad, Actions } from "./$types";
2025-03-25 15:25:04 +01:00
import { fail, redirect } from "@sveltejs/kit"
2025-02-04 21:41:16 +01:00
2025-01-17 13:32:28 +01:00
import { toInt } from "$lib/util"
2025-03-25 15:25:04 +01:00
import { getAllFiles, getRecordFiles, getEstimateFiles, generateEstimatePDF, generateRecordPDF } from "$lib/server/docstore";
2025-01-17 13:32:28 +01:00
2025-02-04 21:41:16 +01:00
export const load: PageServerLoad = async ({ locals }) => {
2025-01-17 13:32:28 +01:00
return {
2025-03-25 15:25:04 +01:00
documents: await getAllFiles(locals.user),
records: await getRecordFiles(locals.user),
estimates: await getEstimateFiles(locals.user)
2025-01-17 13:32:28 +01:00
}
}
export const actions = {
2025-02-04 21:41:16 +01:00
create_estimate: async ({ locals, request }) => {
2025-01-17 13:32:28 +01:00
const data = await request.formData();
2025-02-04 21:41:16 +01:00
const quarter = toInt(data.get("quarter") ?? "");
const year = toInt(data.get("year") ?? "");
2025-01-17 13:32:28 +01:00
2025-02-04 21:41:16 +01:00
if (isNaN(year) || isNaN(quarter) || quarter < 1 || quarter > 4) {
return fail(400, { success: false, message: "Invalid parameter", year: year, quarter: quarter });
}
2025-01-17 13:32:28 +01:00
2025-02-04 21:41:16 +01:00
try {
await generateEstimatePDF(locals.user, year, quarter);
} catch (e) {
console.log(e);
return fail(403, { success: false, message: e.toString(), year: year, quarter: quarter });
2025-01-17 13:32:28 +01:00
}
2025-02-04 21:41:16 +01:00
return { success: true };
},
create_record: async ({ locals, request }) => {
const data = await request.formData();
const month = toInt(data.get("month") ?? "");
const year = toInt(data.get("year") ?? "");
2025-01-17 13:32:28 +01:00
2025-02-04 21:41:16 +01:00
if (isNaN(year) || isNaN(month) || month < 1 || month > 12) {
return fail(400, { success: false, message: "Invalid parameter", year: year, month: month });
}
try {
await generateRecordPDF(locals.user, year, month);
} catch (e) {
console.log(e);
return fail(403, { success: false, message: e.toString(), year: year, month: month });
}
2025-03-25 15:25:04 +01:00
2025-02-04 21:41:16 +01:00
2025-03-25 15:25:04 +01:00
redirect(303, "dokumente")
2025-02-04 21:41:16 +01:00
return { success: true };
2025-01-17 13:32:28 +01:00
}
2025-02-04 21:41:16 +01:00
} satisfies Actions;