Bildupload und Kundendaten
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { AufnahmeClient, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { AufnahmeClient, OptionalNullable, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { exclude } from "#lib/exclude.js";
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { AufnahmeSchema, prisma } from "@ibcornelsen/database/server";
|
||||
@@ -57,7 +57,7 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
}
|
||||
},
|
||||
output: ZodOverlap<AufnahmeClient>(AufnahmeSchema.omit({
|
||||
output: ZodOverlap<OptionalNullable<AufnahmeClient>>(AufnahmeSchema.omit({
|
||||
id: true,
|
||||
objekt_id: true,
|
||||
benutzer_id: true
|
||||
|
||||
@@ -11,7 +11,7 @@ export const PUT = defineApiRoute({
|
||||
benutzer_id: true,
|
||||
objekt_id: true,
|
||||
}).merge(z.object({
|
||||
baujahr_klima: z.array(z.number().int().positive()).optional()
|
||||
baujahr_klima: z.array(z.number().int().positive()).nullish()
|
||||
})),
|
||||
uid_objekt: z.string().uuid()
|
||||
}),
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { z } from "zod";
|
||||
import moment from "moment";
|
||||
import { prisma } from "@ibcornelsen/database/server";
|
||||
import { TokenType, encodeToken } from "../../../lib/auth/token.js";
|
||||
import { encodeToken } from "../../../lib/auth/token.js";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { TokenType } from "#lib/auth/types.js";
|
||||
|
||||
export const GET = defineApiRoute({
|
||||
meta: {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { z } from "zod";
|
||||
import moment from "moment";
|
||||
import { prisma } from "@ibcornelsen/database/server";
|
||||
import { TokenType, encodeToken } from "../../../lib/auth/token.js";
|
||||
import { hashPassword, validatePassword } from "../../../lib/password.js";
|
||||
import { encodeToken } from "../../../lib/auth/token.js";
|
||||
import { validatePassword } from "../../../lib/password.js";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { TokenType } from "#lib/auth/types.js";
|
||||
|
||||
export const GET = defineApiRoute({
|
||||
meta: {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { GebaeudeBilderSchema, prisma } from "@ibcornelsen/database/server";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { z } from "astro:content";
|
||||
import { z } from "zod";
|
||||
import isBase64 from "is-base64";
|
||||
import { fileURLToPath } from "url";
|
||||
import { writeFileSync } from "fs";
|
||||
|
||||
export const PUT = defineApiRoute({
|
||||
input: GebaeudeBilderSchema.pick({
|
||||
@@ -20,7 +22,7 @@ export const PUT = defineApiRoute({
|
||||
if (!isBase64(base64, { mimeRequired: true })) {
|
||||
throw new APIError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Das Bild ist nicht base64 kodiert.",
|
||||
message: "Das Bild ist nicht base64.",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -58,15 +60,17 @@ export const PUT = defineApiRoute({
|
||||
},
|
||||
});
|
||||
|
||||
const filePath = `/persistent/images/${bild.uid}.webp`;
|
||||
const filePath = fileURLToPath(new URL(`../../../../../persistent/images/${bild.uid}.webp`, import.meta.url));
|
||||
|
||||
try {
|
||||
// Wir optimieren das Bild und konvertieren es in WebP
|
||||
// TODO: Sharp scheint nicht zu funktionieren, wir müssen das nochmal testen
|
||||
// const optimizedBuffer = await sharp(buffer).webp({ quality: 80 }).toArray();
|
||||
|
||||
await Bun.write(filePath, buffer)
|
||||
writeFileSync(filePath, buffer)
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
|
||||
// Bild wurde nicht gespeichert, wir löschen den Eintrag wieder
|
||||
await prisma.gebaeudeBilder.delete({
|
||||
where: {
|
||||
@@ -97,7 +101,8 @@ export const GET = defineApiRoute({
|
||||
|
||||
const objekt = await prisma.objekt.findUnique({
|
||||
where: {
|
||||
uid
|
||||
uid,
|
||||
benutzer_id: user.id
|
||||
},
|
||||
select: {
|
||||
benutzer_id: true,
|
||||
@@ -110,7 +115,7 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
})
|
||||
|
||||
if (!objekt || objekt.benutzer_id !== user.id) {
|
||||
if (!objekt) {
|
||||
throw new APIError({
|
||||
code: "FORBIDDEN",
|
||||
message: "Objekt existiert nicht oder gehört einem anderen Benutzer."
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ObjektClient, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { ObjektClient, OptionalNullable, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { exclude } from "#lib/exclude.js";
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { ObjektSchema, prisma } from "@ibcornelsen/database/server";
|
||||
@@ -59,7 +59,7 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
}
|
||||
},
|
||||
output: ZodOverlap<ObjektClient>(ObjektSchema.omit({
|
||||
output: ZodOverlap<OptionalNullable<ObjektClient>>(ObjektSchema.omit({
|
||||
benutzer_id: true,
|
||||
id: true
|
||||
})),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { VerbrauchsausweisWohnenClient, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { OptionalNullable, VerbrauchsausweisWohnenClient, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { exclude } from "#lib/exclude.js";
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { prisma, VerbrauchsausweisWohnenSchema } from "@ibcornelsen/database/server";
|
||||
@@ -60,7 +60,7 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
}
|
||||
},
|
||||
output: ZodOverlap<VerbrauchsausweisWohnenClient>(VerbrauchsausweisWohnenSchema.merge(z.object({
|
||||
output: ZodOverlap<OptionalNullable<VerbrauchsausweisWohnenClient>>(VerbrauchsausweisWohnenSchema.merge(z.object({
|
||||
uid_aufnahme: z.string().uuid(),
|
||||
uid_objekt: z.string().uuid(),
|
||||
uid_benutzer: z.string().uuid().optional()
|
||||
@@ -73,8 +73,12 @@ export const GET = defineApiRoute({
|
||||
async fetch(input, context, user) {
|
||||
const { uid } = context.params;
|
||||
|
||||
console.log(uid);
|
||||
|
||||
if (!uid) {
|
||||
throw new APIError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Missing uid in request params"
|
||||
})
|
||||
}
|
||||
|
||||
const ausweis = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
where: {
|
||||
|
||||
@@ -3,30 +3,65 @@
|
||||
import KundendatenModule from "#modules/KundendatenModule.svelte";
|
||||
import AusweisLayout from "#layouts/AusweisLayoutPruefung.astro";
|
||||
import { Enums } from "@ibcornelsen/database/client";
|
||||
import { createCaller } from "#lib/caller";
|
||||
import { createCaller } from "../astro-typesafe-api-caller";
|
||||
import { API_ACCESS_TOKEN_COOKIE_NAME, API_REFRESH_TOKEN_COOKIE_NAME } from "#lib/constants";
|
||||
import { validateAccessTokenServer } from "#server/lib/validateAccessToken";
|
||||
|
||||
// Man sollte nur auf diese Seite kommen, wenn ein Ausweis bereits vorliegt und in der Datenbank abgespeichert wurde.
|
||||
const uid = Astro.url.searchParams.get("uid");
|
||||
const valid = await validateAccessTokenServer(Astro)
|
||||
|
||||
if (!uid) {
|
||||
if (!uid || !valid) {
|
||||
return Astro.redirect("/404");
|
||||
}
|
||||
|
||||
const caller = createCaller(Astro);
|
||||
|
||||
const ausweis = await caller.v1.verbrauchsausweisWohnen.get({
|
||||
uid
|
||||
const caller = createCaller(Astro)
|
||||
|
||||
const ausweis = await caller["verbrauchsausweis-wohnen"]._uid.GET.fetch(undefined, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
||||
},
|
||||
params: {
|
||||
uid
|
||||
}
|
||||
})
|
||||
|
||||
const user = await caller.v1.benutzer.self();
|
||||
const aufnahme = await caller.aufnahme._uid.GET.fetch(undefined, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
||||
},
|
||||
params: {
|
||||
uid: ausweis.uid_aufnahme
|
||||
}
|
||||
})
|
||||
|
||||
const objekt = await caller.objekt._uid.GET.fetch(undefined, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
||||
},
|
||||
params: {
|
||||
uid: aufnahme.uid_objekt
|
||||
}
|
||||
})
|
||||
|
||||
const user = await caller.user.self.GET.fetch(undefined, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
||||
}
|
||||
});
|
||||
|
||||
aufnahme.ausweisart = "VerbrauchsausweisWohnen"
|
||||
|
||||
|
||||
if (!ausweis) {
|
||||
|
||||
|
||||
if (!ausweis || !user) {
|
||||
return Astro.redirect("/404");
|
||||
}
|
||||
---
|
||||
|
||||
<AusweisLayout title="Kundendaten Aufnehmen - IBCornelsen">
|
||||
<KundendatenModule user={user} ausweis={ausweis} selectedPaymentType={Enums.Bezahlmethoden.paypal} client:load></KundendatenModule>
|
||||
<KundendatenModule {user} {ausweis} {objekt} {aufnahme} selectedPaymentType={Enums.Bezahlmethoden.paypal} client:load></KundendatenModule>
|
||||
</AusweisLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user