Überarbeitung Verbrauchsausweis+ Berechnung

This commit is contained in:
Moritz Utcke
2023-09-20 13:22:17 +06:00
parent 471f3bae8d
commit ec7a9ad88c
15 changed files with 204 additions and 202 deletions

View File

@@ -3,30 +3,26 @@ import moment from "moment";
import { ActionFailedError, InvalidDataError, MissingPropertyError, error, success } from "src/lib/APIResponse";
import { getClimateFactor } from "src/lib/Klimafaktoren/getClimateFactor";
export const get: APIRoute = async function({ request }) {
let body;
try {
body = Object.fromEntries(new URLSearchParams(request.url.split("?")[1]))
} catch(e) {
return error(["Failed to parse URL"]);
}
export const get: APIRoute = async function({ url }) {
const body = url.searchParams;
let zip = body.get("zip");
if (!body.date) {
if (!body.get("date")) {
return MissingPropertyError(["date"]);
}
let accuracy = body.accuracy || "months";
let accuracy = body.get("accuracy") || "months";
if (accuracy !== "months" && accuracy !== "years") {
return error(["Accuracy must be either 'months' or 'years'."])
}
if (!body.zip) {
if (!zip) {
return error(["Invalid ZIP Code, must be 4 or 5 characters long."])
}
let start = moment(body.date);
let end = moment(body.date).add("2", "years");
let start = moment(body.get("date"));
let end = moment(body.get("date")).add("2", "years");
if (!start.isValid()) {
return error(["Invalid start date given."]);
@@ -49,7 +45,7 @@ export const get: APIRoute = async function({ request }) {
currentDate.add(1, accuracy);
}
const climateFactors = await getClimateFactor(intervals, body.zip);
const climateFactors = await getClimateFactor(intervals, zip);
if (!climateFactors) {
return ActionFailedError();