Fix UI Admin Menu Pendidikam, Add Menu User & Role

This commit is contained in:
2025-09-02 18:08:53 +08:00
parent 7ae83788b4
commit fa9601e126
86 changed files with 5349 additions and 2649 deletions

View File

@@ -1,51 +0,0 @@
import { Context } from "elysia";
import prisma from "@/lib/prisma";
import bcrypt from "bcryptjs";
type FormCreateUser = {
nama: string;
email: string;
password: string;
roleId: string;
isActive?: boolean;
};
export default async function userCreate(context: Context) {
const body = (await context.body) as FormCreateUser;
if (!body.nama || !body.email || !body.password || !body.roleId) {
throw new Error("Semua field wajib diisi");
}
try {
// Cek apakah email sudah terdaftar
const existing = await prisma.user.findUnique({
where: { email: body.email },
});
if (existing) {
throw new Error("Email sudah terdaftar");
}
// Hash password sebelum simpan
const hashedPassword = await bcrypt.hash(body.password, 10);
const result = await prisma.user.create({
data: {
nama: body.nama,
email: body.email,
password: hashedPassword,
roleId: body.roleId,
isActive: body.isActive ?? true,
},
});
return {
success: true,
message: "User berhasil dibuat",
data: result,
};
} catch (error) {
console.error("Error creating user:", error);
throw new Error("Gagal membuat user: " + (error as Error).message);
}
}

View File

@@ -1,28 +1,52 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function userFindMany(context: Context) {
const page = Number(context.query.page) || 1;
const limit = Number(context.query.limit) || 10;
const search = (context.query.search as string) || '';
const skip = (page - 1) * limit;
// Buat where clause
const where: any = { isActive: true };
// Tambahkan pencarian (jika ada)
if (search) {
where.OR = [
{ nama: { contains: search, mode: 'insensitive' } },
];
}
export default async function userFindMany() {
try {
const data = await prisma.user.findMany({
const [data, total] = await Promise.all([
prisma.user.findMany({
include: {
role: true,
},
orderBy: {
createdAt: "desc",
},
});
skip,
take: limit,
}),
prisma.user.count({ where }),
]);
return {
success: true,
message: "Success get all user",
message: "Berhasil ambil user dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (error) {
console.error("Find many error:", error);
} catch (e) {
console.error("Error di findMany paginated:", e);
return {
success: false,
message:
"Gagal mengambil data: " +
(error instanceof Error ? error.message : "Unknown error"),
message: "Gagal mengambil data user",
};
}
}

View File

@@ -1,53 +1,13 @@
import { Elysia, t } from "elysia";
// Import semua handler
import userCreate from "./create";
import userFindMany from "./findMany";
import userFindUnique from "./findUnique";
import userUpdate from "./updt";
import userDelete from "./del"; // `delete` nggak boleh jadi nama file JS langsung, jadi biasanya `del.ts`
import userLogin from "./login";
import userRegister from "./register";
const User = new Elysia({ prefix: "/api/user" })
.post("/register", userRegister, {
body: t.Object({
nama: t.String(),
email: t.String(),
password: t.String(),
}),
})
.post("/login", userLogin, {
body: t.Object({
email: t.String(),
password: t.String(),
}),
})
.post("/create", userCreate, {
body: t.Object({
nama: t.String(),
email: t.String(),
password: t.String(),
roleId: t.String(),
}),
})
.get("/findMany", userFindMany)
.get("/findUnique/:id", userFindUnique)
.put(
"/update/:id",
async (context) => {
const response = await userUpdate(context);
return response;
},
{
body: t.Object({
nama: t.String(),
email: t.String(),
password: t.String(),
roleId: t.String(),
}),
}
)
.put("/del/:id", userDelete, {
params: t.Object({
id: t.String(),

View File

@@ -1,81 +0,0 @@
import { Context } from "elysia";
import prisma from "@/lib/prisma";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
// ENV atau secret key untuk token
const JWT_SECRET = process.env.JWT_SECRET || "super-secret-key"; // ganti di env production
type LoginForm = {
email: string;
password: string;
};
export default async function userLogin(context: Context) {
const body = (await context.body) as LoginForm;
try {
// 1. Cari user berdasarkan email
const user = await prisma.user.findUnique({
where: { email: body.email },
include: { role: true }, // include role untuk otorisasi
});
// 2. Jika tidak ada user
if (!user) {
return {
success: false,
message: "Email tidak ditemukan",
};
}
// 3. Cek apakah user aktif
if (!user.isActive) {
return {
success: false,
message: "Akun tidak aktif",
};
}
// 4. Verifikasi password
const isMatch = await bcrypt.compare(body.password, user.password);
if (!isMatch) {
return {
success: false,
message: "Password salah",
};
}
// 5. Buat JWT token
const token = jwt.sign(
{
id: user.id,
email: user.email,
role: user.role.name,
},
JWT_SECRET,
{ expiresIn: "7d" } // expire 7 hari
);
// 6. Kirim response
return {
success: true,
message: "Login berhasil",
data: {
user: {
id: user.id,
nama: user.nama,
email: user.email,
role: user.role.name,
},
token,
},
};
} catch (error) {
console.error("Login error:", error);
return {
success: false,
message: "Terjadi kesalahan saat login",
};
}
}

View File

@@ -1,88 +0,0 @@
import prisma from "@/lib/prisma";
import bcrypt from "bcryptjs";
import { Context } from "elysia";
interface RegisterBody {
nama: string;
email: string;
password: string;
}
export default async function userRegister(context: Context) {
try {
const body = (await context.body) as RegisterBody;
// Validasi input
if (!body.nama || !body.email || !body.password) {
context.set.status = 400;
return {
success: false,
message: "Semua field harus diisi",
data: null
};
}
// Cek email sudah terdaftar
const existingUser = await prisma.user.findUnique({
where: { email: body.email },
});
if (existingUser) {
context.set.status = 400;
return {
success: false,
message: "Email sudah terdaftar",
data: null
};
}
// Dapatkan role warga
const role = await prisma.role.findFirst({
where: { name: "warga" }
});
if (!role) {
context.set.status = 500;
return {
success: false,
message: "Role warga tidak ditemukan",
data: null
};
}
// Hash password
const hashedPassword = await bcrypt.hash(body.password, 10);
// Buat user baru
const user = await prisma.user.create({
data: {
nama: body.nama,
email: body.email,
password: hashedPassword,
roleId: role.id,
},
select: {
id: true,
nama: true,
email: true,
roleId: true,
createdAt: true,
updatedAt: true
}
});
return {
success: true,
message: "Berhasil mendaftar",
data: user,
};
} catch (error) {
console.error("Registration error:", error);
context.set.status = 500;
return {
success: false,
message: "Terjadi kesalahan saat mendaftar",
data: null
};
}
}

View File

@@ -3,6 +3,7 @@ import { Context } from "elysia";
type FormCreate = {
name: string;
permissions: string[];
}
export default async function roleCreate(context: Context) {
@@ -12,6 +13,7 @@ export default async function roleCreate(context: Context) {
const result = await prisma.role.create({
data: {
name: body.name,
permissions: body.permissions,
},
});
return {

View File

@@ -13,6 +13,7 @@ const Role = new Elysia({
.post("/create", roleCreate, {
body: t.Object({
name: t.String(),
permissions: t.Array(t.String()),
}),
})
@@ -26,6 +27,7 @@ const Role = new Elysia({
.put("/:id", roleUpdate, {
body: t.Object({
name: t.String(),
permissions: t.Array(t.String()),
}),
})
.delete("/del/:id", roleDelete);

View File

@@ -3,6 +3,7 @@ import { Context } from "elysia";
type FormUpdate = {
name: string;
permissions: string[];
}
export default async function roleUpdate(context: Context) {
@@ -14,6 +15,7 @@ export default async function roleUpdate(context: Context) {
where: { id },
data: {
name: body.name,
permissions: body.permissions,
},
});
return {

View File

@@ -1,35 +0,0 @@
// /api/user/update.ts
import prisma from '@/lib/prisma';
import { Context } from 'elysia';
export default async function userUpdate(context: Context) {
const { id } = context.params as { id: string };
const body = await context.body as {
nama?: string;
email?: string;
password?: string;
roleId?: string;
isActive?: boolean;
};
try {
const updated = await prisma.user.update({
where: { id },
data: {
...body,
},
});
return {
success: true,
message: 'User berhasil diupdate',
data: updated,
};
} catch (error) {
console.error(error);
return {
success: false,
message: 'Gagal mengupdate user',
};
}
}