Überarbeitung Verbrauchsausweis+ Berechnung
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user