API & UI Menu Struktur bagian pegawai
This commit is contained in:
@@ -11,15 +11,23 @@ type FormCreatePegawai = {
|
||||
telepon?: string;
|
||||
alamat?: string;
|
||||
posisiId: string;
|
||||
isActive?: boolean;
|
||||
};
|
||||
|
||||
export default async function pegawaiCreate(context: Context) {
|
||||
const body = await context.body as FormCreatePegawai;
|
||||
|
||||
if (!body || !body.namaLengkap || !body.posisiId) {
|
||||
if (!body) {
|
||||
return {
|
||||
success: false,
|
||||
message: "namaLengkap dan posisiId wajib diisi",
|
||||
message: "Data tidak valid",
|
||||
};
|
||||
}
|
||||
|
||||
if (!body.namaLengkap || !body.posisiId || !body.imageId) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Nama lengkap, posisi, dan gambar wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,7 +42,8 @@ export default async function pegawaiCreate(context: Context) {
|
||||
telepon: body.telepon,
|
||||
alamat: body.alamat,
|
||||
posisiId: body.posisiId,
|
||||
// aktif, createdAt, updatedAt otomatis by Prisma default
|
||||
isActive: body.isActive !== false // default true kalau tidak dikirim
|
||||
|
||||
},
|
||||
});
|
||||
|
||||
@@ -44,10 +53,19 @@ export default async function pegawaiCreate(context: Context) {
|
||||
data: pegawai,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error creating pegawai:", error);
|
||||
|
||||
if (error.code === "P2002") {
|
||||
// Cek field mana yang duplicate
|
||||
const target = error.meta?.target?.[0];
|
||||
let message = "Data sudah ada";
|
||||
|
||||
if (target === 'email') message = "Email sudah digunakan";
|
||||
else if (target === 'telepon') message = "Nomor telepon sudah digunakan";
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: "Email sudah digunakan",
|
||||
message,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ export default async function pegawaiDelete(context: Context) {
|
||||
const deleted = await prisma.pegawai.update({
|
||||
where: { id },
|
||||
data: {
|
||||
aktif: false, // soft delete
|
||||
isActive: false, // soft delete
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@ import prisma from "@/lib/prisma";
|
||||
export default async function pegawaiFindMany() {
|
||||
try {
|
||||
const pegawaiList = await prisma.pegawai.findMany({
|
||||
where: { aktif: true }, // hanya yang aktif
|
||||
where: { isActive: true }, // Using the correct field name from Prisma model
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
posisi: true,
|
||||
|
||||
@@ -57,6 +57,6 @@ const Pegawai = new Elysia({
|
||||
)
|
||||
|
||||
// ✅ Delete
|
||||
.delete("/:id", pegawaiDelete);
|
||||
.delete("/del/:id", pegawaiDelete);
|
||||
|
||||
export default Pegawai;
|
||||
|
||||
@@ -12,7 +12,7 @@ type FormUpdatePegawai = {
|
||||
telepon?: string;
|
||||
alamat?: string;
|
||||
posisiId?: string;
|
||||
aktif?: boolean;
|
||||
isActive?: boolean;
|
||||
};
|
||||
|
||||
export default async function pegawaiUpdate(context: Context) {
|
||||
@@ -37,9 +37,13 @@ export default async function pegawaiUpdate(context: Context) {
|
||||
telepon: body.telepon,
|
||||
alamat: body.alamat,
|
||||
posisiId: body.posisiId,
|
||||
aktif: body.aktif,
|
||||
isActive: body.isActive === true, // Konversi ke boolean
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
posisi: true,
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user