126 lines
4.0 KiB
TypeScript
126 lines
4.0 KiB
TypeScript
import { AufnahmeClient, BenutzerClient, getAusweisartFromUUID, ObjektClient, UploadedGebaeudeBild, VerbrauchsausweisGewerbeClient, VerbrauchsausweisWohnenClient } from "#components/Ausweis/types.js";
|
|
import { API_ACCESS_TOKEN_COOKIE_NAME } from "#lib/constants.js";
|
|
import { pdfVerbrauchsausweisGewerbe } from "#lib/pdf/pdfVerbrauchsausweisGewerbe.js";
|
|
import { pdfVerbrauchsausweisWohnen } from "#lib/pdf/pdfVerbrauchsausweisWohnen.js";
|
|
import { Enums } from "#lib/client/prisma.js";
|
|
import { APIRoute } from "astro";
|
|
import { createCaller } from "src/astro-typesafe-api-caller.js";
|
|
|
|
export const GET: APIRoute = async (Astro) => {
|
|
const uidAusweis = Astro.url.searchParams.get("uid");
|
|
|
|
if (!uidAusweis) {
|
|
return Astro.redirect("/404")
|
|
}
|
|
|
|
const ausweisart = getAusweisartFromUUID(uidAusweis)
|
|
|
|
const caller = createCaller(Astro);
|
|
|
|
let ausweis: VerbrauchsausweisWohnenClient | VerbrauchsausweisGewerbeClient | null = null;
|
|
let aufnahme: AufnahmeClient = {} as AufnahmeClient;
|
|
let objekt: ObjektClient = {} as ObjektClient;
|
|
let user: BenutzerClient = {} as BenutzerClient;
|
|
let bilder: UploadedGebaeudeBild[] = []
|
|
|
|
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}`
|
|
}
|
|
});
|
|
|
|
let pdf: Uint8Array<ArrayBufferLike> | null = null;
|
|
if (ausweisart === Enums.Ausweisart.VerbrauchsausweisWohnen) {
|
|
pdf = await pdfVerbrauchsausweisWohnen(ausweis as VerbrauchsausweisWohnenClient, aufnahme, objekt, bilder, user);
|
|
} else if (ausweisart === Enums.Ausweisart.VerbrauchsausweisGewerbe) {
|
|
pdf = await pdfVerbrauchsausweisGewerbe(ausweis as VerbrauchsausweisGewerbeClient, aufnahme, objekt, bilder, user);
|
|
}
|
|
|
|
return new Response(pdf, {
|
|
headers: {
|
|
"Content-Type": "application/pdf",
|
|
},
|
|
});
|
|
}
|
|
|
|
export const POST: APIRoute = async (Astro) => {
|
|
const body = await Astro.request.text();
|
|
const params = new URLSearchParams(body);
|
|
|
|
const caller = createCaller(Astro);
|
|
|
|
const ausweis = JSON.parse(params.get("ausweis") || "{}");
|
|
const aufnahme = JSON.parse(params.get("aufnahme") || "{}");
|
|
const objekt = JSON.parse(params.get("objekt") || "{}");
|
|
const bilder = JSON.parse(params.get("bilder") || "[]");
|
|
const ausweisart: Enums.Ausweisart = JSON.parse(params.get("ausweisart") || "")
|
|
|
|
|
|
let user: BenutzerClient = {};
|
|
|
|
try {
|
|
user = await caller.user.self.GET.fetch(undefined, {
|
|
headers: {
|
|
Authorization: `Bearer ${Astro.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value}`
|
|
}
|
|
});
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
let pdf: Uint8Array<ArrayBufferLike> | null = null;
|
|
if (ausweisart === Enums.Ausweisart.VerbrauchsausweisWohnen) {
|
|
pdf = await pdfVerbrauchsausweisWohnen(ausweis, aufnahme, objekt, bilder, user);
|
|
} else if (ausweisart === Enums.Ausweisart.VerbrauchsausweisGewerbe) {
|
|
pdf = await pdfVerbrauchsausweisGewerbe(ausweis, aufnahme, objekt, bilder, user);
|
|
}
|
|
|
|
return new Response(pdf, {
|
|
headers: {
|
|
"Content-Type": "application/pdf",
|
|
},
|
|
});
|
|
} |