Verbesserungen
This commit is contained in:
143
src/pages/api/geg-nachweis-gewerbe/index.ts
Normal file
143
src/pages/api/geg-nachweis-gewerbe/index.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { UUidWithPrefix } from "#components/Ausweis/types.js";
|
||||
import { authorizationHeaders, authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { GEGNachweisGewerbeSchema, GEGNachweisWohnenSchema, prisma } from "@ibcornelsen/database/server";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const PUT = defineApiRoute({
|
||||
meta: {
|
||||
contentTypes: ["application/json"],
|
||||
description:
|
||||
"Erstellt einen neuen GEG Nachweis für Gewerbegebäude.",
|
||||
tags: ["GEG Nachweis"],
|
||||
},
|
||||
input: z.object({
|
||||
nachweis: GEGNachweisGewerbeSchema.omit({
|
||||
id: true,
|
||||
benutzer_id: true,
|
||||
uid: true,
|
||||
aufnahme_id: true,
|
||||
geg_einpreisung_id: true,
|
||||
}),
|
||||
uid_aufnahme: UUidWithPrefix
|
||||
}),
|
||||
output: z.object({
|
||||
uid: UUidWithPrefix,
|
||||
objekt_uid: UUidWithPrefix,
|
||||
aufnahme_uid: UUidWithPrefix,
|
||||
}),
|
||||
headers: authorizationHeaders,
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, ctx, user) {
|
||||
const aufnahme = await prisma.aufnahme.findUnique({
|
||||
where: {
|
||||
uid: input.uid_aufnahme
|
||||
}
|
||||
})
|
||||
|
||||
if (!aufnahme || aufnahme.benutzer_id !== user.id) {
|
||||
throw new APIError({
|
||||
code: "FORBIDDEN",
|
||||
message: "Aufnahme konnte nicht gefunden werden oder gehört nicht zu diesem Benutzer."
|
||||
})
|
||||
}
|
||||
|
||||
const nachweis = await prisma.gEGNachweisGewerbe.create({
|
||||
data: {
|
||||
...input.nachweis,
|
||||
benutzer: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
aufnahme: {
|
||||
connect: {
|
||||
id: aufnahme.id,
|
||||
},
|
||||
}
|
||||
},
|
||||
select: {
|
||||
uid: true,
|
||||
aufnahme: {
|
||||
select: {
|
||||
uid: true,
|
||||
objekt: {
|
||||
select: {
|
||||
uid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
uid: nachweis.uid,
|
||||
objekt_uid: nachweis.aufnahme.objekt.uid,
|
||||
aufnahme_uid: nachweis.aufnahme.uid,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const GET = defineApiRoute({
|
||||
meta: {
|
||||
description: "Gibt eine spezifische GEG Nachweis Anfrage des Benutzers zurück.",
|
||||
tags: ["GEG Nachweis"],
|
||||
headers: {
|
||||
Authorization: {
|
||||
description: "Ein gültiger Authentifizierungstoken",
|
||||
required: true,
|
||||
allowEmptyValue: false,
|
||||
examples: {
|
||||
Bearer: {
|
||||
value: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
headers: authorizationHeaders,
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, context, user) {
|
||||
const { uid } = context.params;
|
||||
|
||||
const nachweis = await prisma.gEGNachweisGewerbe.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
},
|
||||
include: {
|
||||
benutzer: true,
|
||||
aufnahme: {
|
||||
include: {
|
||||
rechnungen: true,
|
||||
events: {
|
||||
include: {
|
||||
benutzer: {
|
||||
select: {
|
||||
uid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
date: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (
|
||||
!nachweis ||
|
||||
(nachweis.benutzer_id !== null && nachweis.benutzer_id !== user.id)
|
||||
) {
|
||||
// Falls wir den Ausweis nicht finden können, werfen wir einen Fehler
|
||||
throw new APIError({
|
||||
code: "NOT_FOUND",
|
||||
message: "GEG Nachweis konnte nicht gefunden werden.",
|
||||
});
|
||||
}
|
||||
|
||||
return nachweis;
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user