88 lines
1.9 KiB
TypeScript
88 lines
1.9 KiB
TypeScript
import { ObjektClient, OptionalNullable, ZodOverlap } from "#components/Ausweis/types.js";
|
|
import { exclude } from "#lib/exclude.js";
|
|
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
|
import { Enums, ObjektSchema, prisma } from "#lib/server/prisma.js";
|
|
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
|
import { z } from "zod";
|
|
|
|
export const PATCH = defineApiRoute({
|
|
input: ObjektSchema.omit({
|
|
uid: true,
|
|
id: true,
|
|
benutzer_id: true,
|
|
erstellungsdatum: true
|
|
}),
|
|
output: z.void(),
|
|
headers: {
|
|
"Authorization": z.string()
|
|
},
|
|
middleware: authorizationMiddleware,
|
|
async fetch(input, ctx, user) {
|
|
const objekt = await prisma.objekt.findUnique({
|
|
where: {
|
|
uid: ctx.params.uid,
|
|
benutzer: {
|
|
id: user.id
|
|
}
|
|
}
|
|
})
|
|
|
|
if (!objekt) {
|
|
throw new APIError({
|
|
code: "NOT_FOUND",
|
|
message: "Objekt konnte nicht gefunden werden."
|
|
})
|
|
}
|
|
|
|
await prisma.objekt.update({
|
|
where: {
|
|
uid: ctx.params.uid
|
|
},
|
|
data: input
|
|
})
|
|
},
|
|
})
|
|
|
|
export const GET = defineApiRoute({
|
|
meta: {
|
|
description: "Gibt ein spezifisches Gebäude des Benutzers zurück.",
|
|
tags: ["Gebäude"],
|
|
headers: {
|
|
"Authorization": {
|
|
description: "Ein gültiger Authentifizierungstoken",
|
|
required: true,
|
|
allowEmptyValue: false,
|
|
examples: {
|
|
Bearer: {
|
|
value: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
output: ObjektSchema.omit({
|
|
benutzer_id: true,
|
|
id: true
|
|
}),
|
|
middleware: authorizationMiddleware,
|
|
async fetch(input, ctx, user) {
|
|
const { uid } = ctx.params;
|
|
|
|
const objekt = await prisma.objekt.findUnique({
|
|
where: user.rolle === Enums.BenutzerRolle.USER ? {
|
|
uid,
|
|
benutzer_id: user.id
|
|
} : { uid },
|
|
});
|
|
|
|
if (!objekt) {
|
|
throw new APIError({
|
|
code: "NOT_FOUND",
|
|
message: "Objekt mit dieser UID existiert nicht oder gehört nicht dem aktuellen Benutzer."
|
|
})
|
|
}
|
|
|
|
return exclude(objekt, ["benutzer_id", "id"])
|
|
},
|
|
});
|