Speichern verbessert
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { GEGNachweisWohnenClient, OptionalNullable, UUidWithPrefix, 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 { prisma } from "#lib/server/prisma.js";
|
||||
@@ -8,11 +10,16 @@ import { z } from "zod";
|
||||
|
||||
export const PATCH = defineApiRoute({
|
||||
input: BedarfsausweisGewerbeSchema.omit({
|
||||
uid: true,
|
||||
id: true,
|
||||
benutzer_id: true,
|
||||
geg_einpreisung_id: true,
|
||||
aufnahme_id: true,
|
||||
bestellt: true,
|
||||
created_at: true,
|
||||
rechnung_id: true,
|
||||
storniert: true,
|
||||
updated_at: true,
|
||||
zurueckgestellt: true
|
||||
}),
|
||||
output: z.void(),
|
||||
headers: {
|
||||
@@ -22,7 +29,7 @@ export const PATCH = defineApiRoute({
|
||||
async fetch(input, ctx, user) {
|
||||
const objekt = await prisma.bedarfsausweisGewerbe.findUnique({
|
||||
where: {
|
||||
uid: ctx.params.uid,
|
||||
id: ctx.params.id,
|
||||
benutzer: {
|
||||
id: user.id
|
||||
}
|
||||
@@ -38,7 +45,7 @@ export const PATCH = defineApiRoute({
|
||||
|
||||
await prisma.bedarfsausweisGewerbe.update({
|
||||
where: {
|
||||
uid: ctx.params.uid
|
||||
id: ctx.params.id
|
||||
},
|
||||
data: input
|
||||
})
|
||||
@@ -52,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."
|
||||
@@ -65,7 +72,7 @@ export const DELETE = defineApiRoute({
|
||||
// Dieser MUSS mit dem Nutzer verknüpft sein.
|
||||
const nachweis = await prisma.bedarfsausweisGewerbe.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
id,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -112,10 +119,13 @@ export const DELETE = defineApiRoute({
|
||||
}
|
||||
})
|
||||
|
||||
const event_id = generatePrefixedId(6, VALID_UUID_PREFIXES.Ticket)
|
||||
|
||||
// Wir erstellen ein Event, dass der Nachweis storniert wurde
|
||||
// Dann können wir das in der Historie anzeigen
|
||||
await prisma.event.create({
|
||||
data: {
|
||||
id: event_id,
|
||||
title: "Nachweis storniert",
|
||||
description: ((user.rolle === "ADMIN") && (nachweis.benutzer_id !== user.id)) ? "Nachweis wurde von einem Administrator storniert." : "Nachweis wurde vom Besitzer storniert.",
|
||||
benutzer: {
|
||||
@@ -151,9 +161,9 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
},
|
||||
output: BedarfsausweisGewerbeSchema.merge(z.object({
|
||||
uid_aufnahme: UUidWithPrefix,
|
||||
uid_objekt: UUidWithPrefix,
|
||||
uid_benutzer: UUidWithPrefix.optional()
|
||||
aufnahme_id: UUidWithPrefix,
|
||||
objekt_id: UUidWithPrefix,
|
||||
benutzer_id: UUidWithPrefix.optional()
|
||||
})).omit({
|
||||
id: true,
|
||||
aufnahme_id: true,
|
||||
@@ -161,9 +171,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"
|
||||
@@ -172,21 +182,21 @@ export const GET = defineApiRoute({
|
||||
|
||||
const nachweis = await prisma.bedarfsausweisGewerbe.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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,9 +213,9 @@ export const GET = defineApiRoute({
|
||||
}
|
||||
|
||||
return {
|
||||
uid_aufnahme: nachweis.aufnahme.uid,
|
||||
uid_objekt: nachweis.aufnahme.objekt.uid,
|
||||
uid_benutzer: nachweis.benutzer?.uid,
|
||||
aufnahme_id: nachweis.aufnahme.id,
|
||||
objekt_id: nachweis.aufnahme.objekt.id,
|
||||
benutzer_id: nachweis.benutzer?.id,
|
||||
...exclude(nachweis, ["id", "aufnahme_id", "benutzer_id", "aufnahme"])
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user