spomeni/src/app/api/admin/codes/route.ts
dimitar 9ca66fc753 feat(admin): add admin panel with dashboard, users, and codes management
- Add admin layout with sidebar navigation and session guard
- Create AdminSidebar client component with role-based nav links
- Add dashboard page showing stats (admin count, code counts)
- Add users management page (SuperAdmin only): list, create, delete,
  and reset passwords for admin users
- Add codes management page: list all codes, generate new codes,
  delete unused codes
- Add API routes for admin user CRUD (GET, POST, DELETE, PUT)
- Add API routes for code management (GET, POST, DELETE)
- All UI in Macedonian
2026-07-29 18:56:49 +02:00

45 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from "next/server";
import { randomBytes } from "crypto";
import { prisma } from "@/lib/prisma";
import { getAdminSession } from "@/lib/admin-session";
export async function GET() {
const session = await getAdminSession();
if (!session) {
return NextResponse.json({ error: "Неавторизирано" }, { status: 401 });
}
const where = session.role === "SUPER_ADMIN" ? {} : { createdBy: { username: session.username } };
const codes = await prisma.code.findMany({
where,
orderBy: { createdAt: "desc" },
include: { createdBy: { select: { username: true } } },
});
return NextResponse.json(codes);
}
export async function POST() {
const session = await getAdminSession();
if (!session) {
return NextResponse.json({ error: "Неавторизирано" }, { status: 401 });
}
const admin = await prisma.adminUser.findUnique({ where: { username: session.username } });
if (!admin) {
return NextResponse.json({ error: "Администраторот не е пронајден" }, { status: 404 });
}
const code = randomBytes(6).toString("hex").toUpperCase();
const created = await prisma.code.create({
data: {
code,
createdById: admin.id,
},
});
return NextResponse.json(created, { status: 201 });
}