45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
|
|
import { fail } from '@sveltejs/kit';
|
||
|
|
|
||
|
|
import { toInt, toFloat } from "$lib/util";
|
||
|
|
|
||
|
|
export async function load({ locals }) {
|
||
|
|
return {
|
||
|
|
estimates: locals.user.get_estimates()
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export const actions = {
|
||
|
|
add_quarter: async ({ locals, request }) => {
|
||
|
|
|
||
|
|
const data = await request.formData();
|
||
|
|
|
||
|
|
const year = data.get("year");
|
||
|
|
const quart = data.get("quarter");
|
||
|
|
const estimate_0 = data.get("estimate_0");
|
||
|
|
const estimate_1 = data.get("estimate_1");
|
||
|
|
const estimate_2 = data.get("estimate_2");
|
||
|
|
|
||
|
|
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);
|
||
|
|
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 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const res = locals.user.insert_estimate(y, q, est_0, est_1, est_2)
|
||
|
|
|
||
|
|
if (!res) {
|
||
|
|
return fail(500, {});
|
||
|
|
}
|
||
|
|
|
||
|
|
return { success: true };
|
||
|
|
}
|
||
|
|
}
|