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

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-03-25 15:25:04 +01:00
import type { PageServerLoad, Actions } from "./$types";
2025-06-05 14:32:12 +02:00
import type { FileProperties } from "$lib/server/docstore"
2025-03-25 15:25:04 +01:00
import { fail } from '@sveltejs/kit';
2025-03-25 15:25:04 +01:00
import { MONTHS, toInt, toFloat } from "$lib/util";
import { getEstimateFiles } from "$lib/server/docstore";
export const load: PageServerLoad = async ({ locals }) => {
2025-06-05 14:32:12 +02:00
if (!locals.user) {
return fail(403, { message: "Unauthorized user" })
}
2025-03-25 15:25:04 +01:00
let estimates = locals.user.get_estimates();
let documents = await getEstimateFiles(locals.user);
2025-06-05 14:32:12 +02:00
let estimates_grouped: Map<number, Map<number, {month: string, estimate: number}[]>> = new Map();
Map.groupBy(estimates, (v) => v.year).forEach((value, key, _) => {
2025-03-25 15:25:04 +01:00
2025-06-05 14:32:12 +02:00
let quarters = new Map()
Map.groupBy(value, (v) => v.quarter).forEach((_value, _key, _) => {
2025-03-25 15:25:04 +01:00
let months = _value.map((v) => [
{ month: MONTHS[(v.quarter - 1) * 3 + 2], estimate: v.estimate_2 },
{ month: MONTHS[(v.quarter - 1) * 3 + 1], estimate: v.estimate_1 },
{ month: MONTHS[(v.quarter - 1) * 3 + 0], estimate: v.estimate_0 },
] ).flat();
2025-06-05 14:32:12 +02:00
quarters.set(_key, months);
2025-03-25 15:25:04 +01:00
})
2025-06-05 14:32:12 +02:00
estimates_grouped.set(key, quarters);
2025-03-25 15:25:04 +01:00
})
2025-06-05 14:32:12 +02:00
let documents_grouped: Map<number, Map<number, FileProperties[]>> = new Map()
Map.groupBy(documents, (v) => toInt(v.identifier.slice(0, 4))).forEach((value, key, _) => {
2025-03-25 15:25:04 +01:00
let quarters = Map.groupBy(value, (v) => toInt(v.identifier.slice(5, 7)));
2025-06-05 14:32:12 +02:00
documents_grouped.set(key, quarters);
2025-03-25 15:25:04 +01:00
})
return {
2025-03-25 15:25:04 +01:00
estimates: estimates_grouped,
documents: documents_grouped,
};
}
export const actions = {
2025-06-05 14:32:12 +02:00
add_quarter: async ({ locals, request }) => {
if (!locals.user) {
return fail(403, { message: "Unauthorized user" })
}
2025-06-05 14:32:12 +02:00
const data = await request.formData();
2025-04-12 22:49:53 +02:00
2025-06-05 14:32:12 +02:00
const year = data.get("year") as string;
const quart = data.get("quarter") as string;
const estimate_0 = data.get("estimate_0") as string;
const estimate_1 = data.get("estimate_1") as string;
const estimate_2 = data.get("estimate_2") as string;
if (year == null || quart == null || estimate_0 == null || estimate_1 == null || estimate_2 == null) {
return fail(400, { year: year, quarter: quart, estimate_0: estimate_0, estimate_1: estimate_1, estimate_2: estimate_2 });
}
const y = toInt(year);
2025-03-25 15:25:04 +01:00
const q = toInt(quart);
const est_0 = toFloat(estimate_0);
const est_1 = toFloat(estimate_1);
const est_2 = toFloat(estimate_2);
if (isNaN(y) || isNaN(q) || q < 1 || q > 4 || isNaN(est_0) || isNaN(est_1) || isNaN(est_2)) {
return fail(400, { year: year, quarter: quart, estimate_0: estimate_0, estimate_1: estimate_1, estimate_2: estimate_2 });
}
2025-03-25 15:25:04 +01:00
if (locals.user.get_estimate(y, q)) {
return fail(409, { reason: "Estimate already exists" } );
}
2025-04-12 22:49:53 +02:00
const res = locals.user.insert_estimate(y, q, est_0, est_1, est_2)
if (!res) {
return fail(500, {});
}
return { success: true };
}
2025-03-25 15:25:04 +01:00
} satisfies Actions;