Notifications + Plausibilitätsprüfung

This commit is contained in:
Moritz Utcke
2023-05-12 23:40:31 +04:00
parent 33c8a1d447
commit 5559f5ca4d
37 changed files with 855 additions and 357 deletions

View File

@@ -1,5 +1,8 @@
import type { APIRoute } from "astro";
import { ActionFailedError, error, success } from "src/lib/APIResponse";
import { ActionFailedError, MissingEntityError, error, success } from "src/lib/APIResponse";
import { Ausweis } from "src/lib/Ausweis/Ausweis";
import { Energiekennwerte } from "src/lib/Energiekennwerte";
import { Gebaeude } from "src/lib/Gebaeude";
import { db } from "src/lib/shared";
import { z } from "zod";
@@ -62,6 +65,10 @@ const AusweisUploadChecker = z.object({
ausweis_uid: z.string().optional(),
});
const AusweisDownloadChecker = z.object({
uid: z.string()
})
/**
* Erstellt einen Verbrauchsausweis anhand der gegebenen Daten und trägt ihn in die Datenbank ein.
* @param param0
@@ -135,4 +142,40 @@ export const post: APIRoute = async ({ request }) => {
kennwerte: kennwerte[0],
gebaeude: gebaeude[0],
});
};
export const get: APIRoute = async ({ request }) => {
const body: z.infer<typeof AusweisDownloadChecker> = await request.json();
const validation = AusweisDownloadChecker.safeParse(body);
if (!validation.success) {
return error(validation.error.issues);
}
let result = await db<{ gebaeude: Gebaeude, kennwerte: Energiekennwerte, ausweis: Ausweis}>("gebaeude")
.select([
db.raw("(json_agg(gebaeude)->0) AS gebaeude"),
db.raw("(json_agg(energiekennwerte)->0) AS kennwerte"),
db.raw("(json_agg(energieausweise)->0) AS ausweis"),
])
.leftJoin(
"energiekennwerte",
"energiekennwerte.gebaeude_id",
"gebaeude.id"
)
.leftJoin(
"energieausweise",
"energieausweise.gebaeude_id",
"gebaeude.id"
)
.where("gebaeude.uid", body.uid)
.groupBy("gebaeude.id")
.first();
if (!result) {
return MissingEntityError("gebäude");
}
return result;
};

View File

@@ -23,7 +23,7 @@ export const post: APIRoute = async ({ request }) => {
}
// Validate Password
if (!validatePassword(user.password, body.password)) {
if (!validatePassword(user.passwort, body.password)) {
return error(["Invalid email or password."]);
}

View File

@@ -1,7 +1,8 @@
import type { APIRoute } from "astro";
import { success, MissingPropertyError, MissingEntityError, ActionFailedError, InvalidDataError } from "../../lib/APIResponse";
import { success, MissingPropertyError, MissingEntityError, ActionFailedError, InvalidDataError, error } from "../../lib/APIResponse";
import { User } from "../../lib/User";
import { UserRegisterValidator, UserType, UserTypeValidator } from "../../lib/User/type";
import { z } from "zod";
/**
* Ruft einen Nutzer anhand seiner uid aus der Datenbank ab.
@@ -24,7 +25,7 @@ export const get: APIRoute = async ({ request }) => {
}
export const put: APIRoute = async ({ request }) => {
const body = await request.json();
const body: z.infer<typeof UserRegisterValidator> = await request.json();
const validate = UserRegisterValidator.safeParse(body);
@@ -32,6 +33,12 @@ export const put: APIRoute = async ({ request }) => {
return InvalidDataError(validate.error);
}
const user = await User.fromEmail(body.email);
if (user) {
return error(["Email address is already being used."]);
}
const result = await User.create(body as UserType);
if (!result) {