API größtenteils umgezogen und Funktionen angepasst
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { prisma } from "@ibcornelsen/database/server";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
|
||||
export const PATCH = defineApiRoute({
|
||||
fetch(input, context) {},
|
||||
});
|
||||
|
||||
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..."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, context, user) {
|
||||
const { uid } = context.params;
|
||||
|
||||
const ausweis = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
where: {
|
||||
uid: input.uid,
|
||||
},
|
||||
include: {
|
||||
benutzer: true,
|
||||
aufnahme: {
|
||||
include: {
|
||||
objekt: {
|
||||
include: {
|
||||
gebaeude_bilder: true
|
||||
},
|
||||
},
|
||||
rechnungen: true,
|
||||
events: {
|
||||
include: {
|
||||
benutzer: {
|
||||
select: {
|
||||
uid: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
date: "asc"
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (!ausweis || (ausweis.benutzer_id !== null && ausweis.benutzer_id !== user.id)) {
|
||||
// Falls wir den Ausweis nicht finden können, werfen wir einen Fehler
|
||||
throw new APIError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Ausweis konnte nicht gefunden werden.",
|
||||
});
|
||||
}
|
||||
|
||||
return ausweis
|
||||
},
|
||||
});
|
||||
117
src/pages/api/verbrauchsausweis-wohnen/[uid].ts
Normal file
117
src/pages/api/verbrauchsausweis-wohnen/[uid].ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { exclude } from "#lib/exclude.js";
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { prisma, VerbrauchsausweisWohnenSchema } from "@ibcornelsen/database/server";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const PATCH = defineApiRoute({
|
||||
input: VerbrauchsausweisWohnenSchema.omit({
|
||||
uid: true,
|
||||
id: true,
|
||||
benutzer_id: true,
|
||||
aufnahme_id: true,
|
||||
}),
|
||||
output: z.void(),
|
||||
headers: {
|
||||
"Authorization": z.string()
|
||||
},
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, ctx, user) {
|
||||
const objekt = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
where: {
|
||||
uid: ctx.params.uid,
|
||||
benutzer: {
|
||||
id: user.id
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (!objekt) {
|
||||
throw new APIError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Ausweis konnte nicht gefunden werden oder gehört einem anderen Benutzer."
|
||||
})
|
||||
}
|
||||
|
||||
await prisma.verbrauchsausweisWohnen.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: VerbrauchsausweisWohnenSchema.merge(z.object({
|
||||
uid_aufnahme: z.string().uuid(),
|
||||
uid_objekt: z.string().uuid(),
|
||||
uid_benutzer: z.string().uuid().optional()
|
||||
})).omit({
|
||||
id: true,
|
||||
aufnahme_id: true,
|
||||
benutzer_id: true,
|
||||
}),
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, context, user) {
|
||||
const { uid } = context.params;
|
||||
|
||||
console.log(uid);
|
||||
|
||||
|
||||
const ausweis = await prisma.verbrauchsausweisWohnen.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
benutzer_id: user.id
|
||||
},
|
||||
include: {
|
||||
benutzer: {
|
||||
select: {
|
||||
uid: true
|
||||
}
|
||||
},
|
||||
aufnahme: {
|
||||
select: {
|
||||
uid: true,
|
||||
objekt: {
|
||||
select: {
|
||||
uid: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!ausweis) {
|
||||
// Falls wir den Ausweis nicht finden können, werfen wir einen Fehler
|
||||
throw new APIError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Ausweis konnte nicht gefunden werden.",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
uid_aufnahme: ausweis.aufnahme.uid,
|
||||
uid_objekt: ausweis.aufnahme.objekt.uid,
|
||||
uid_benutzer: ausweis.benutzer?.uid,
|
||||
...exclude(ausweis, ["id", "aufnahme_id", "benutzer_id", "aufnahme"])
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -1,27 +1,101 @@
|
||||
import { authorizationMiddleware } from "#lib/middleware/authorization.js";
|
||||
import { prisma } from "@ibcornelsen/database/server";
|
||||
import { prisma, VerbrauchsausweisWohnenSchema } from "@ibcornelsen/database/server";
|
||||
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const PATCH = defineApiRoute({
|
||||
fetch(input, context) {},
|
||||
});
|
||||
|
||||
export const PUT = defineApiRoute({
|
||||
meta: {
|
||||
contentTypes: ["application/json"],
|
||||
description:
|
||||
"Erstellt einen neuen Verbrauchsausweis für Wohngebäude nach dem Schema der EnEV von 2016. Als Input wird ein bestehendes Gebäude benötigt. Falls keine UID einer bestehenden Gebäudeaufnahme mitgegeben wird, wird automatisch eine erstellt.",
|
||||
tags: ["Verbrauchsausweis Wohnen"],
|
||||
},
|
||||
input: z.object({
|
||||
ausweis: VerbrauchsausweisWohnenSchema.omit({
|
||||
id: true,
|
||||
benutzer_id: true,
|
||||
uid: true,
|
||||
aufnahme_id: true
|
||||
}),
|
||||
uid_aufnahme: z.string().uuid()
|
||||
}),
|
||||
output: z.object({
|
||||
uid: z.string().uuid(),
|
||||
objekt_uid: z.string().uuid(),
|
||||
aufnahme_uid: z.string().uuid(),
|
||||
}),
|
||||
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 createdAusweis = await prisma.verbrauchsausweisWohnen.create({
|
||||
data: {
|
||||
...input.ausweis,
|
||||
benutzer: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
aufnahme: {
|
||||
connect: {
|
||||
uid: aufnahme.uid,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
uid: true,
|
||||
aufnahme: {
|
||||
select: {
|
||||
uid: true,
|
||||
objekt: {
|
||||
select: {
|
||||
uid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
uid: createdAusweis.uid,
|
||||
objekt_uid: createdAusweis.aufnahme.objekt.uid,
|
||||
aufnahme_uid: createdAusweis.aufnahme.uid,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const GET = defineApiRoute({
|
||||
meta: {
|
||||
description: "Gibt ein spezifisches Gebäude des Benutzers zurück.",
|
||||
tags: ["Gebäude"],
|
||||
headers: {
|
||||
"Authorization": {
|
||||
Authorization: {
|
||||
description: "Ein gültiger Authentifizierungstoken",
|
||||
required: true,
|
||||
allowEmptyValue: false,
|
||||
examples: {
|
||||
Bearer: {
|
||||
value: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
value: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
middleware: authorizationMiddleware,
|
||||
async fetch(input, context, user) {
|
||||
@@ -37,7 +111,7 @@ export const GET = defineApiRoute({
|
||||
include: {
|
||||
objekt: {
|
||||
include: {
|
||||
gebaeude_bilder: true
|
||||
gebaeude_bilder: true,
|
||||
},
|
||||
},
|
||||
rechnungen: true,
|
||||
@@ -45,20 +119,23 @@ export const GET = defineApiRoute({
|
||||
include: {
|
||||
benutzer: {
|
||||
select: {
|
||||
uid: true
|
||||
}
|
||||
}
|
||||
uid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
date: "asc"
|
||||
}
|
||||
}
|
||||
date: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!ausweis || (ausweis.benutzer_id !== null && ausweis.benutzer_id !== user.id)) {
|
||||
if (
|
||||
!ausweis ||
|
||||
(ausweis.benutzer_id !== null && ausweis.benutzer_id !== user.id)
|
||||
) {
|
||||
// Falls wir den Ausweis nicht finden können, werfen wir einen Fehler
|
||||
throw new APIError({
|
||||
code: "NOT_FOUND",
|
||||
@@ -66,6 +143,6 @@ export const GET = defineApiRoute({
|
||||
});
|
||||
}
|
||||
|
||||
return ausweis
|
||||
return ausweis;
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user