Initial commit: Kalendartool (Next.js, Prisma, Docker)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
86
app/api/calendars/route.ts
Normal file
86
app/api/calendars/route.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* POST /api/calendars - neuen Kalender erstellen (nur ADMIN).
|
||||
*/
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db/client';
|
||||
import { requireDbUser } from '@/lib/api/context';
|
||||
import { createCalendarSchema } from '@/lib/validation';
|
||||
import { errorResponse, forbiddenResponse } from '@/lib/api/responses';
|
||||
|
||||
export async function POST(request: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
const user = await requireDbUser();
|
||||
if (user.role !== 'ADMIN') {
|
||||
return forbiddenResponse();
|
||||
}
|
||||
|
||||
const body = createCalendarSchema.parse(await request.json());
|
||||
|
||||
const calendar = await prisma.calendar.create({
|
||||
data: {
|
||||
ownerId: user.id,
|
||||
title: body.title,
|
||||
description: body.description ?? null,
|
||||
timezone: body.timezone,
|
||||
},
|
||||
select: { id: true, title: true, description: true, timezone: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ calendar }, { status: 201 });
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/calendars - Kalender des angemeldeten Benutzers.
|
||||
*
|
||||
* Admin: alle Kalender (Verwaltungsrolle, konsistent mit canManageCalendar
|
||||
* und den Server-Komponenten in Dashboard/Verwaltung). User: genau der
|
||||
* eine Kalender, in dem er Mitglied ist (plan.md Abschnitt 14: Dashboard).
|
||||
*/
|
||||
export async function GET(): Promise<NextResponse> {
|
||||
try {
|
||||
const user = await requireDbUser();
|
||||
|
||||
if (user.role === 'ADMIN') {
|
||||
const calendars = await prisma.calendar.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
description: true,
|
||||
timezone: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
_count: { select: { reservations: true, members: true } },
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ calendars });
|
||||
}
|
||||
|
||||
// User: genau ein Kalender (durch unique auf userId garantiert).
|
||||
const membership = await prisma.calendarMember.findUnique({
|
||||
where: { userId: user.id },
|
||||
include: {
|
||||
calendar: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
description: true,
|
||||
timezone: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
_count: { select: { reservations: true, members: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
calendars: membership?.calendar ? [membership.calendar] : [],
|
||||
});
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user