Ausweis erstellen

This commit is contained in:
Moritz Utcke
2025-04-29 10:39:11 -03:00
parent e24310bdb7
commit a8be6db8aa
42 changed files with 377 additions and 239 deletions

View File

@@ -1,7 +1,10 @@
import { UUidWithPrefix } from "#components/Ausweis/types.js";
import { IDWithPrefix } from "#components/Ausweis/types.js";
import { VALID_UUID_PREFIXES } from "#lib/constants.js";
import { generatePrefixedId } from "#lib/db.js";
import { authorizationHeaders, authorizationMiddleware } from "#lib/middleware/authorization.js";
import { generateIDWithPrefix } from "#lib/db.js";
import {
authorizationHeaders,
authorizationMiddleware,
} from "#lib/middleware/authorization.js";
import { sendAusweisGespeichertMail } from "#lib/server/mail/speichern-erfolgreich.js";
import { Enums, prisma } from "#lib/server/prisma.js";
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
@@ -20,87 +23,102 @@ export const PUT = defineApiRoute({
tags: ["Verbrauchsausweis Wohnen"],
},
input: z.object({
ausweis: VerbrauchsausweisWohnenSchema.merge(z.object({
startdatum: z.coerce.date().nullable()
})).omit({
ausweis: VerbrauchsausweisWohnenSchema.merge(
z.object({
startdatum: z.coerce.date().nullable(),
})
).omit({
id: true,
benutzer_id: true,
aufnahme_id: true,
rechnung_id: true,
created_at: true,
updated_at: true,
ausweisart: true
ausweisart: true,
alte_ausweis_id: true,
ausgestellt: true,
bestellt: true,
ausstellungsdatum: true,
kontrolldatei_angefragt: true,
registriernummer: true,
storniert: true,
zurueckgestellt: true,
}),
aufnahme_id: UUidWithPrefix
aufnahme_id: IDWithPrefix,
}),
output: z.object({
id: UUidWithPrefix,
objekt_id: UUidWithPrefix,
aufnahme_id: UUidWithPrefix,
id: IDWithPrefix,
objekt_id: IDWithPrefix,
aufnahme_id: IDWithPrefix,
}),
headers: authorizationHeaders,
middleware: authorizationMiddleware,
async fetch(input, ctx, user) {
const aufnahme = await prisma.aufnahme.findUnique({
where: {
id: input.aufnahme_id
}
})
id: input.aufnahme_id,
},
});
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."
})
message:
"Aufnahme konnte nicht gefunden werden oder gehört nicht zu diesem Benutzer.",
});
}
const id = generatePrefixedId(9, VALID_UUID_PREFIXES.VerbrauchsausweisWohnen);
const id = generateIDWithPrefix(
9,
VALID_UUID_PREFIXES.VerbrauchsausweisWohnen
);
const createdAusweis = await prisma.verbrauchsausweisWohnen.create({
data: {
id,
...input.ausweis,
benutzer: {
connect: {
id: user.id,
},
},
aufnahme: {
connect: {
id: aufnahme.id,
},
const ausweis = await prisma.verbrauchsausweisWohnen.create({
data: {
id,
...input.ausweis,
benutzer: {
connect: {
id: user.id,
},
},
select: {
id: true,
aufnahme: {
select: {
id: true,
objekt: {
select: {
id: true,
},
aufnahme: {
connect: {
id: aufnahme.id,
},
},
},
select: {
id: true,
aufnahme: {
select: {
id: true,
objekt: {
select: {
id: true,
},
},
},
},
});
if (user.rolle === Enums.BenutzerRolle.USER) {
await sendAusweisGespeichertMail(user, id)
}
return {
id: createdAusweis.id,
objekt_id: createdAusweis.aufnahme.objekt.id,
aufnahme_id: createdAusweis.aufnahme.id,
};
},
});
if (user.rolle === Enums.BenutzerRolle.USER) {
await sendAusweisGespeichertMail(user, id);
}
return {
id: ausweis.id,
objekt_id: ausweis.aufnahme.objekt.id,
aufnahme_id: ausweis.aufnahme.id,
};
},
});
export const GET = defineApiRoute({
meta: {
description: "Gibt ein spezifisches Gebäude des Benutzers zurück.",
tags: ["Gebäude"],
description:
"Gibt einen spezifischen Verbrauchsausweis Wohngebäude des Benutzers zurück.",
tags: ["Verbrauchsausweis Wohnen"],
headers: {
Authorization: {
description: "Ein gültiger Authentifizierungstoken",