Speichern verbessert
This commit is contained in:
@@ -1,16 +1,25 @@
|
||||
import { BedarfsausweisWohnenClient, OptionalNullable, UUidWithPrefix, VerbrauchsausweisWohnenClient, ZodOverlap } from "#components/Ausweis/types.js";
|
||||
import { VALID_UUID_PREFIXES } from "#lib/constants.js";
|
||||
import { generatePrefixedId } from "#lib/db.js";
|
||||
import { exclude } from "#lib/exclude.js";
|
||||
import { authorizationHeaders, authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { BedarfsausweisWohnenSchema, prisma, VerbrauchsausweisWohnenSchema } from "#lib/server/prisma";
|
||||
import { BedarfsausweisWohnen, prisma } from "#lib/server/prisma.js";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { BedarfsausweisWohnenSchema } from "src/generated/zod/bedarfsausweiswohnen.js";
|
||||
import { z } from "zod";
|
||||
|
||||
export const PATCH = defineApiRoute({
|
||||
input: VerbrauchsausweisWohnenSchema.omit({
|
||||
uid: true,
|
||||
input: BedarfsausweisWohnenSchema.omit({
|
||||
id: true,
|
||||
benutzer_id: true,
|
||||
aufnahme_id: true,
|
||||
rechnung_id: true,
|
||||
ausgestellt: true,
|
||||
bestellt: true,
|
||||
zurueckgestellt: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
storniert: true
|
||||
}),
|
||||
output: z.void(),
|
||||
headers: {
|
||||
@@ -18,9 +27,9 @@ export const PATCH = defineApiRoute({
|
||||
},
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, ctx, user) {
|
||||
const objekt = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
const objekt = await prisma.bedarfsausweisWohnen.findUnique({
|
||||
where: {
|
||||
uid: ctx.params.uid,
|
||||
id: ctx.params.id,
|
||||
benutzer: {
|
||||
id: user.id
|
||||
}
|
||||
@@ -34,9 +43,9 @@ export const PATCH = defineApiRoute({
|
||||
})
|
||||
}
|
||||
|
||||
await prisma.verbrauchsausweisWohnen.update({
|
||||
await prisma.bedarfsausweisWohnen.update({
|
||||
where: {
|
||||
uid: ctx.params.uid
|
||||
id: ctx.params.id
|
||||
},
|
||||
data: input
|
||||
})
|
||||
@@ -50,9 +59,9 @@ export const DELETE = defineApiRoute({
|
||||
headers: authorizationHeaders,
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, ctx, user) {
|
||||
const { uid } = ctx.params;
|
||||
const { id } = ctx.params;
|
||||
|
||||
if (!UUidWithPrefix.safeParse(uid).success) {
|
||||
if (!UUidWithPrefix.safeParse(id).success) {
|
||||
throw new APIError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "UID konnte nicht verifiziert werden."
|
||||
@@ -61,15 +70,11 @@ export const DELETE = defineApiRoute({
|
||||
|
||||
// Wir holen uns den Bedarfsausweis
|
||||
// Dieser MUSS mit dem Nutzer verknüpft sein.
|
||||
const ausweis = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
const ausweis = await prisma.bedarfsausweisWohnen.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
},
|
||||
include: {
|
||||
aufnahme: {
|
||||
select: {
|
||||
storniert: true
|
||||
}
|
||||
id,
|
||||
benutzer: {
|
||||
id: user.id
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -100,7 +105,7 @@ export const DELETE = defineApiRoute({
|
||||
// });
|
||||
// }
|
||||
|
||||
if (ausweis.aufnahme.storniert) {
|
||||
if (ausweis.storniert) {
|
||||
// Falls der Ausweis bereits storniert ist, werfen wir einen Fehler
|
||||
throw new APIError({
|
||||
code: "BAD_REQUEST",
|
||||
@@ -108,19 +113,22 @@ export const DELETE = defineApiRoute({
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.aufnahme.update({
|
||||
await prisma.bedarfsausweisWohnen.update({
|
||||
where: {
|
||||
id: ausweis.aufnahme_id
|
||||
id
|
||||
},
|
||||
data: {
|
||||
storniert: true
|
||||
}
|
||||
})
|
||||
|
||||
const event_id = generatePrefixedId(6, VALID_UUID_PREFIXES.Ticket)
|
||||
|
||||
// Wir erstellen ein Event, dass der Ausweis storniert wurde
|
||||
// Dann können wir das in der Historie anzeigen
|
||||
await prisma.event.create({
|
||||
data: {
|
||||
id: event_id,
|
||||
title: "Ausweis storniert",
|
||||
description: ((user.rolle === "ADMIN") && (ausweis.benutzer_id !== user.id)) ? "Ausweis wurde von einem Administrator storniert." : "Ausweis wurde vom Besitzer storniert.",
|
||||
benutzer: {
|
||||
@@ -155,10 +163,10 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
}
|
||||
},
|
||||
output: ZodOverlap<OptionalNullable<VerbrauchsausweisWohnenClient>>(VerbrauchsausweisWohnenSchema.merge(z.object({
|
||||
uid_aufnahme: UUidWithPrefix,
|
||||
uid_objekt: UUidWithPrefix,
|
||||
uid_benutzer: UUidWithPrefix.optional()
|
||||
output: ZodOverlap<OptionalNullable<BedarfsausweisWohnenClient>>(BedarfsausweisWohnenSchema.merge(z.object({
|
||||
aufnahme_id: UUidWithPrefix,
|
||||
objekt_id: UUidWithPrefix,
|
||||
benutzer_id: UUidWithPrefix.optional()
|
||||
})).omit({
|
||||
id: true,
|
||||
aufnahme_id: true,
|
||||
@@ -166,9 +174,9 @@ export const GET = defineApiRoute({
|
||||
})),
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, context, user) {
|
||||
const { uid } = context.params;
|
||||
const { id } = context.params;
|
||||
|
||||
if (!uid) {
|
||||
if (!id) {
|
||||
throw new APIError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Missing uid in request params"
|
||||
@@ -177,21 +185,21 @@ export const GET = defineApiRoute({
|
||||
|
||||
const ausweis = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
id,
|
||||
benutzer_id: user.id
|
||||
},
|
||||
include: {
|
||||
benutzer: {
|
||||
select: {
|
||||
uid: true
|
||||
id: true
|
||||
}
|
||||
},
|
||||
aufnahme: {
|
||||
select: {
|
||||
uid: true,
|
||||
id: true,
|
||||
objekt: {
|
||||
select: {
|
||||
uid: true
|
||||
id: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -208,9 +216,9 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
|
||||
return {
|
||||
uid_aufnahme: ausweis.aufnahme.uid,
|
||||
uid_objekt: ausweis.aufnahme.objekt.uid,
|
||||
uid_benutzer: ausweis.benutzer?.uid,
|
||||
aufnahme_id: ausweis.aufnahme.id,
|
||||
objekt_id: ausweis.aufnahme.objekt.id,
|
||||
benutzer_id: ausweis.benutzer?.id,
|
||||
...exclude(ausweis, ["id", "aufnahme_id", "benutzer_id", "aufnahme"])
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user