From 51fb7ad9b6c136d82f25ed102fd662ad94936303 Mon Sep 17 00:00:00 2001 From: Moritz Utcke Date: Sat, 25 Jan 2025 09:02:51 +0700 Subject: [PATCH] =?UTF-8?q?API=20Vollst=C3=A4ndig=20Umgezogen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/astro-typesafe-api-caller.ts | 5 +- src/client/lib/bilderHochladen.ts | 142 +++++++++++------- src/client/lib/spawnSignupPrompt.ts | 8 +- src/client/tickets/createTicket.ts | 9 +- src/components/Ausweis/types.ts | 8 +- .../Dashboard/DashboardAusweis.svelte | 45 ++++-- src/components/PlzSuche.svelte | 1 - src/components/Tickets/TicketPopup.svelte | 5 +- src/lib/Klimafaktoren.ts | 1 - .../Dashboard/DashboardAusweiseModule.svelte | 3 +- .../DashboardEinstellungenModule.svelte | 2 +- src/modules/EmbeddedRegisterModule.svelte | 4 +- src/modules/KaufabschlussModule.svelte | 13 +- src/pages/api/auth/access-token.ts | 11 +- src/pages/api/bilder/[uid].ts | 84 +++++++++++ src/pages/api/{ticket.ts => ticket/index.ts} | 0 .../api/verbrauchsausweis-wohnen/[uid].ts | 97 +++++++++++- tests/bilder/upload.test.ts | 4 +- 18 files changed, 329 insertions(+), 113 deletions(-) create mode 100644 src/pages/api/bilder/[uid].ts rename src/pages/api/{ticket.ts => ticket/index.ts} (100%) diff --git a/src/astro-typesafe-api-caller.ts b/src/astro-typesafe-api-caller.ts index 41cafcbe..78c7843a 100644 --- a/src/astro-typesafe-api-caller.ts +++ b/src/astro-typesafe-api-caller.ts @@ -3,16 +3,17 @@ import { createCallerFactory } from "astro-typesafe-api/server"; export const createCaller = createCallerFactory({ "klimafaktoren": await import("../src/pages/api/klimafaktoren.ts"), "postleitzahlen": await import("../src/pages/api/postleitzahlen.ts"), - "ticket": await import("../src/pages/api/ticket.ts"), "aufnahme/[uid]": await import("../src/pages/api/aufnahme/[uid].ts"), "aufnahme": await import("../src/pages/api/aufnahme/index.ts"), + "bilder/[uid]": await import("../src/pages/api/bilder/[uid].ts"), "auth/access-token": await import("../src/pages/api/auth/access-token.ts"), "auth/refresh-token": await import("../src/pages/api/auth/refresh-token.ts"), "bedarfsausweis-wohnen": await import("../src/pages/api/bedarfsausweis-wohnen/index.ts"), "objekt": await import("../src/pages/api/objekt/index.ts"), - "verbrauchsausweis-gewerbe": await import("../src/pages/api/verbrauchsausweis-gewerbe/index.ts"), + "ticket": await import("../src/pages/api/ticket/index.ts"), "user": await import("../src/pages/api/user/index.ts"), "user/self": await import("../src/pages/api/user/self.ts"), + "verbrauchsausweis-gewerbe": await import("../src/pages/api/verbrauchsausweis-gewerbe/index.ts"), "verbrauchsausweis-wohnen/[uid]": await import("../src/pages/api/verbrauchsausweis-wohnen/[uid].ts"), "verbrauchsausweis-wohnen": await import("../src/pages/api/verbrauchsausweis-wohnen/index.ts"), "objekt/[uid]/bilder": await import("../src/pages/api/objekt/[uid]/bilder.ts"), diff --git a/src/client/lib/bilderHochladen.ts b/src/client/lib/bilderHochladen.ts index 0c12ac49..fb9a78c8 100644 --- a/src/client/lib/bilderHochladen.ts +++ b/src/client/lib/bilderHochladen.ts @@ -1,65 +1,95 @@ -import { GebaeudeClient, UploadedGebaeudeBild, VerbrauchsausweisWohnenClient } from "#components/Ausweis/types"; +import { + ObjektClient, + UploadedGebaeudeBild, + VerbrauchsausweisWohnenClient, +} from "#components/Ausweis/types.js"; +import { API_ACCESS_TOKEN_COOKIE_NAME } from "#lib/constants.js"; import { Enums } from "@ibcornelsen/database/client"; import { addNotification, updateNotification } from "@ibcornelsen/ui"; -import { client } from "src/trpc"; +import { api } from "astro-typesafe-api/client"; +import Cookies from "js-cookie"; -export async function bilderHochladen(images: (UploadedGebaeudeBild & { base64?: string })[], gebaeude_uid: string) { - if (images.length == 0) { - return images; - } +export async function bilderHochladen( + images: (UploadedGebaeudeBild & { base64?: string, update?: boolean })[], + gebaeude_uid: string +) { + if (images.length == 0) { + return images; + } - // Wenn Bilder hochgeladen werden konvertieren wir sie zu base64, das heißt, dass die base64 Eigenschaft bei diesen Bildern - // existiert. Das müssen wir TypeScript nur wissen lassen, damit es uns in Ruhe lässt. - const imagesToUpload = images.filter(image => !image.uid || image.update) as unknown as { base64: string, kategorie: string, uid?: string, update: boolean }[]; + // Wenn Bilder hochgeladen werden konvertieren wir sie zu base64, das heißt, dass die base64 Eigenschaft bei diesen Bildern + // existiert. Das müssen wir TypeScript nur wissen lassen, damit es uns in Ruhe lässt. + const imagesToUpload = images.filter( + (image) => !image.uid || image.update + ) as unknown as { + base64: string; + kategorie: string; + uid?: string; + update: boolean; + }[]; - if (imagesToUpload.length == 0) { - return images; - } + if (imagesToUpload.length == 0) { + return images; + } - // Alle Bilder hochladen - const notification = addNotification({ - dismissable: false, - message: "Bilder hochladen.", - subtext: `${imagesToUpload.length} Bilder werden hochgeladen, bitte haben sie Geduld.`, - timeout: 0, - type: "info" - }) - for (let i = 0; i < imagesToUpload.length; i++) { - const image = imagesToUpload[i]; + // Alle Bilder hochladen + const notification = addNotification({ + dismissable: false, + message: "Bilder hochladen.", + subtext: `${imagesToUpload.length} Bilder werden hochgeladen, bitte haben sie Geduld.`, + timeout: 0, + type: "info", + }); + for (let i = 0; i < imagesToUpload.length; i++) { + const image = imagesToUpload[i]; - try { - if (image.update) { - await client.v1.bilder.update.mutate({ - uid: image.uid as string, - base64: image.base64, - kategorie: image.kategorie as Enums.BilderKategorie - }) - } else { - const response = await client.v1.bilder.upload.mutate({ - base64: image.base64, - kategorie: image.kategorie as Enums.BilderKategorie, - gebaeude_uid - }) - - image.uid = response.uid - } + try { + if (image.update) { + await api.bilder._uid.PATCH.fetch({ + base64: image.base64, + kategorie: image.kategorie as Enums.BilderKategorie, + }, { + params: { + uid: image.uid as string, + }, + headers: { + "Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}` + } + }); + } else { + const response = await api.objekt._uid.bilder.PUT.fetch({ + base64: image.base64, + kategorie: image.kategorie as Enums.BilderKategorie + }, { + params: { + uid: gebaeude_uid + }, + headers: { + "Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}` + } + }); - updateNotification(notification, { - dismissable: true, - message: "Bild hochgeladen.", - subtext: `${i + 1}/${imagesToUpload.length} Bildern wurden erfolgreich hochgeladen.`, - timeout: 3000 - }) - } catch (e) { - updateNotification(notification, { - dismissable: true, - message: "Bild konnte nicht hochgeladen werden.", - subtext: `Eines ihrer Bilder konnte nicht hochgeladen werden. Wir haben bereits ein Ticket erstellt und melden uns so schnell wie möglich bei ihnen.`, - timeout: 15000, - type: "error" - }) - } - } + image.uid = response.uid; + } - return images; -} \ No newline at end of file + updateNotification(notification, { + dismissable: true, + message: "Bild hochgeladen.", + subtext: `${i + 1}/${ + imagesToUpload.length + } Bildern wurden erfolgreich hochgeladen.`, + timeout: 3000, + }); + } catch (e) { + updateNotification(notification, { + dismissable: true, + message: "Bild konnte nicht hochgeladen werden.", + subtext: `Eines ihrer Bilder konnte nicht hochgeladen werden. Wir haben bereits ein Ticket erstellt und melden uns so schnell wie möglich bei ihnen.`, + timeout: 15000, + type: "error", + }); + } + } + + return images; +} diff --git a/src/client/lib/spawnSignupPrompt.ts b/src/client/lib/spawnSignupPrompt.ts index f8ba08e6..80b7290e 100644 --- a/src/client/lib/spawnSignupPrompt.ts +++ b/src/client/lib/spawnSignupPrompt.ts @@ -1,6 +1,6 @@ -import { dialogs } from "../../../svelte-dialogs.config"; -import { addNotification } from "#components/Notifications/shared"; -import { client } from "src/trpc"; +import { dialogs } from "../../../svelte-dialogs.config.js"; +import { addNotification } from "#components/Notifications/shared.js"; +import { api } from "astro-typesafe-api/client"; export async function spawnSignupPrompt() { const result = await dialogs.prompt( @@ -46,7 +46,7 @@ export async function spawnSignupPrompt() { const [vorname, name, email, passwort] = result; try { - const response = await client.v1.benutzer.erstellen.mutate({ + const response = await api.user.PUT.fetch({ email, passwort, vorname, diff --git a/src/client/tickets/createTicket.ts b/src/client/tickets/createTicket.ts index aec6754f..a9b14b14 100644 --- a/src/client/tickets/createTicket.ts +++ b/src/client/tickets/createTicket.ts @@ -1,7 +1,6 @@ -import { AppRouter } from "@ibcornelsen/api"; -import { inferProcedureInput } from "@trpc/server"; -import { client } from "src/trpc"; +import { OmitKeys, TicketClient } from "#components/Ausweis/types.js"; +import { api } from "astro-typesafe-api/client"; -export async function createTicket(info: inferProcedureInput) { - return await client.v1.tickets.erstellen.mutate(info) +export async function createTicket(info: OmitKeys) { + return await api.ticket.PUT.fetch(info) } \ No newline at end of file diff --git a/src/components/Ausweis/types.ts b/src/components/Ausweis/types.ts index ac3194d4..2a155710 100644 --- a/src/components/Ausweis/types.ts +++ b/src/components/Ausweis/types.ts @@ -5,12 +5,14 @@ import { Benutzer, GebaeudeBilder, Objekt, + Rechnung, + Tickets, VerbrauchsausweisGewerbe, VerbrauchsausweisWohnen, } from "@ibcornelsen/database/client"; import { z, ZodSchema } from "zod"; -type OmitKeys = Omit; +export type OmitKeys = Omit; export type UploadedGebaeudeBild = OmitKeys & { base64: string @@ -84,9 +86,11 @@ export type AufnahmeClient = OmitKeys< uid_objekt: string }; +export type TicketClient = OmitKeys + export type BenutzerClient = OmitKeys; -type ZodOverlapType = z.ZodType; +export type RechnungClient = OmitKeys export function ZodOverlap>(arg: S): S { return arg; diff --git a/src/components/Dashboard/DashboardAusweis.svelte b/src/components/Dashboard/DashboardAusweis.svelte index c84ea554..268afc63 100644 --- a/src/components/Dashboard/DashboardAusweis.svelte +++ b/src/components/Dashboard/DashboardAusweis.svelte @@ -1,5 +1,8 @@
- {#if ausweis.aufnahme.storniert} + {#if aufnahme.storniert}

Storniert

{/if}
0 && `/bilder/${bilder[0].uid}.webp`) || "/images/placeholder.jpg"} class="object-cover w-full h-full" alt="Gebäudebild" /> @@ -95,25 +108,25 @@
- {#if ausweis.aufnahme.ausweisart == "VerbrauchsausweisWohnen"} + {#if aufnahme.ausweisart == "VerbrauchsausweisWohnen"}
Verbrauchsausweis Wohnen
- {:else if ausweis.aufnahme.ausweisart == "BedarfsausweisWohnen"} + {:else if aufnahme.ausweisart == "BedarfsausweisWohnen"}
Bedarfsausweis Wohnen
- {:else if ausweis.aufnahme.ausweisart == "VerbrauchsausweisGewerbe"} + {:else if aufnahme.ausweisart == "VerbrauchsausweisGewerbe"}
Verbrauchsausweis Gewerbe
{/if} - {#if ausweis.erledigt} + {#if aufnahme.erledigt}
Ausgestellt
{/if}
-

{ausweis.aufnahme.objekt.adresse}

+

{objekt.adresse}

@@ -134,7 +147,7 @@
Erstellungsdatum {moment(ausweis.erstellungsdatum).format( + >{moment(aufnahme.erstellungsdatum).format( "DD.MM.YYYY" )} @@ -144,16 +157,16 @@ {ausweis.aufnahme.baujahr_gebaeude[0] || "N/A"} / - {ausweis.aufnahme.baujahr_heizung[0] || + >{aufnahme.baujahr_gebaeude[0] || "N/A"} / + {aufnahme.baujahr_heizung[0] || "N/A"}
Wohnfläche {ausweis.aufnahme.flaeche - ? `${ausweis.aufnahme.flaeche}m²` + >{aufnahme.flaeche + ? `${aufnahme.flaeche}m²` : "N/A"}
diff --git a/src/components/PlzSuche.svelte b/src/components/PlzSuche.svelte index dfc3bdf2..68c0d636 100644 --- a/src/components/PlzSuche.svelte +++ b/src/components/PlzSuche.svelte @@ -1,5 +1,4 @@