87 lines
2.5 KiB
Plaintext
87 lines
2.5 KiB
Plaintext
---
|
|
import { AufnahmeClient, BenutzerClient, getAusweisartFromUUID, ObjektClient, UploadedGebaeudeBild, VerbrauchsausweisGewerbeClient, VerbrauchsausweisWohnenClient } from "#components/Ausweis/types";
|
|
import { API_ACCESS_TOKEN_COOKIE_NAME } from "#lib/constants";
|
|
import { pdfVerbrauchsausweisWohnen } from "#lib/pdf/pdfVerbrauchsausweisWohnen";
|
|
import { Enums } from "@ibcornelsen/database/client";
|
|
import { createCaller } from "src/astro-typesafe-api-caller";
|
|
|
|
const base64 = Astro.url.searchParams.get("base64");
|
|
let ausweis: VerbrauchsausweisWohnenClient | VerbrauchsausweisGewerbeClient | null = null;
|
|
let aufnahme: AufnahmeClient = {} as AufnahmeClient;
|
|
let objekt: ObjektClient = {} as ObjektClient;
|
|
let user: BenutzerClient = {} as BenutzerClient;
|
|
let images: UploadedGebaeudeBild[] = []
|
|
if (base64) {
|
|
const buffer = Buffer.from(base64, "base64");
|
|
const json = buffer.toString("utf-8");
|
|
ausweis = JSON.parse(json) as VerbrauchsausweisWohnenClient;
|
|
} else {
|
|
const uidAusweis = Astro.url.searchParams.get("uid");
|
|
|
|
if (!uidAusweis) {
|
|
return Astro.redirect("/404");
|
|
}
|
|
|
|
const ausweisart = getAusweisartFromUUID(uidAusweis)
|
|
|
|
const caller = createCaller(Astro);
|
|
|
|
if (ausweisart === Enums.Ausweisart.VerbrauchsausweisWohnen) {
|
|
ausweis = await caller["verbrauchsausweis-wohnen"]._uid.GET.fetch(undefined, {
|
|
params: {
|
|
uid: uidAusweis
|
|
},
|
|
headers: {
|
|
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
|
}
|
|
});
|
|
} else if (ausweisart === Enums.Ausweisart.VerbrauchsausweisGewerbe) {
|
|
ausweis = await caller["verbrauchsausweis-gewerbe"]._uid.GET.fetch(undefined, {
|
|
params: {
|
|
uid: uidAusweis
|
|
},
|
|
headers: {
|
|
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
if (!ausweis) {
|
|
return Astro.redirect("/");
|
|
}
|
|
|
|
aufnahme = await caller.aufnahme._uid.GET.fetch(undefined, {
|
|
params: {
|
|
uid: ausweis.uid_aufnahme
|
|
},
|
|
headers: {
|
|
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
|
}
|
|
});
|
|
|
|
objekt = await caller.objekt._uid.GET.fetch(undefined, {
|
|
params: {
|
|
uid: ausweis.uid_objekt
|
|
},
|
|
headers: {
|
|
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
|
}
|
|
});
|
|
|
|
user = await caller.user.self.GET.fetch(undefined, {
|
|
headers: {
|
|
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
|
}
|
|
});
|
|
}
|
|
|
|
const pdf = await pdfVerbrauchsausweisWohnen(ausweis, aufnahme, objekt, images, user);
|
|
|
|
return new Response(pdf, {
|
|
headers: {
|
|
"Content-Type": "application/pdf",
|
|
},
|
|
});
|
|
---
|