UI & API Menu PPID, Submenu Struktur PPID
This commit is contained in:
@@ -45,7 +45,7 @@ const Pegawai = new Elysia({
|
||||
id: t.String(),
|
||||
namaLengkap: t.Optional(t.String()),
|
||||
gelarAkademik: t.Optional(t.String()),
|
||||
imageId: t.String(),
|
||||
imageId: t.Optional(t.String()),
|
||||
tanggalMasuk: t.Optional(t.String()),
|
||||
email: t.Optional(t.String()),
|
||||
telepon: t.Optional(t.String()),
|
||||
|
||||
@@ -6,7 +6,7 @@ type FormUpdatePegawai = {
|
||||
id: string;
|
||||
namaLengkap?: string;
|
||||
gelarAkademik?: string;
|
||||
imageId: string;
|
||||
imageId?: string;
|
||||
tanggalMasuk?: string;
|
||||
email?: string;
|
||||
telepon?: string;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import strukturPPIDFindById from "./find-by-id";
|
||||
import strukturPPIDUpdate from "./update";
|
||||
import Pegawai from "./pegawai";
|
||||
import PosisiOrganisasi from "./posisi-organisasi";
|
||||
|
||||
const StrukturPPID = new Elysia({
|
||||
prefix: "/strukturppid",
|
||||
@@ -10,6 +12,8 @@ const StrukturPPID = new Elysia({
|
||||
const response = await strukturPPIDFindById(new Request(context.request))
|
||||
return response
|
||||
})
|
||||
.use(Pegawai)
|
||||
.use(PosisiOrganisasi)
|
||||
.put("/:id", async (context) => {
|
||||
const response = await strukturPPIDUpdate(context)
|
||||
return response
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreatePegawai = {
|
||||
namaLengkap: string;
|
||||
gelarAkademik?: string;
|
||||
imageId: string;
|
||||
tanggalMasuk?: string; // Kirim dari frontend dalam format ISO (ex: '2025-07-04')
|
||||
email?: string;
|
||||
telepon?: string;
|
||||
alamat?: string;
|
||||
posisiId: string;
|
||||
isActive?: boolean;
|
||||
};
|
||||
|
||||
export default async function pegawaiCreate(context: Context) {
|
||||
const body = await context.body as FormCreatePegawai;
|
||||
|
||||
if (!body || typeof body !== 'object') {
|
||||
return {
|
||||
success: false,
|
||||
message: "Data tidak valid",
|
||||
};
|
||||
}
|
||||
|
||||
if (!body.namaLengkap || !body.posisiId || !body.imageId) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Nama lengkap, posisi, dan gambar wajib diisi",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const pegawai = await prisma.pegawaiPPID.create({
|
||||
data: {
|
||||
namaLengkap: body.namaLengkap,
|
||||
gelarAkademik: body.gelarAkademik,
|
||||
imageId: body.imageId,
|
||||
tanggalMasuk: body.tanggalMasuk ? new Date(body.tanggalMasuk) : undefined,
|
||||
email: body.email,
|
||||
telepon: body.telepon,
|
||||
alamat: body.alamat,
|
||||
posisiId: body.posisiId,
|
||||
isActive: body.isActive ?? true, // default true kalau tidak dikirim
|
||||
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil menambahkan pegawai",
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
console.error("Gagal menambahkan pegawai:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat membuat pegawai",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function pegawaiDelete(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID pegawai tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = await prisma.pegawaiPPID.update({
|
||||
where: { id },
|
||||
data: {
|
||||
isActive: false, // soft delete
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Pegawai berhasil di-nonaktifkan",
|
||||
data: deleted,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error delete pegawai:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal menghapus pegawai",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
// Di findMany.ts
|
||||
export default async function pegawaiFindMany(context: Context) {
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
try {
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.pegawaiPPID.findMany({
|
||||
where: { isActive: true },
|
||||
include: {
|
||||
posisi: true,
|
||||
image: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.pegawaiPPID.count({
|
||||
where: { isActive: true }
|
||||
})
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch pegawai with pagination",
|
||||
data,
|
||||
page,
|
||||
totalPages,
|
||||
total,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Find many paginated error:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch pegawai with pagination",
|
||||
data: [],
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function pegawaiFindUnique(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID pegawai diperlukan",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const pegawai = await prisma.pegawaiPPID.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
posisi: true,
|
||||
image: true
|
||||
},
|
||||
});
|
||||
|
||||
if (!pegawai) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Pegawai tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: pegawai,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error findUnique pegawai:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data pegawai",
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import pegawaiFindMany from "./findMany";
|
||||
import pegawaiFindUnique from "./findUnique";
|
||||
import pegawaiCreate from "./create";
|
||||
import pegawaiDelete from "./del";
|
||||
import pegawaiUpdate from "./updt";
|
||||
|
||||
|
||||
const Pegawai = new Elysia({
|
||||
prefix: "/pegawai",
|
||||
tags: ["PPID/Struktur PPID/Pegawai"],
|
||||
})
|
||||
|
||||
// ✅ Find all
|
||||
.get("/find-many", pegawaiFindMany)
|
||||
|
||||
// ✅ Find by ID
|
||||
.get("/:id", async (context) => {
|
||||
const response = await pegawaiFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
|
||||
// ✅ Create
|
||||
.post("/create", pegawaiCreate, {
|
||||
body: t.Object({
|
||||
namaLengkap: t.String(),
|
||||
gelarAkademik: t.Optional(t.String()),
|
||||
imageId: t.String(),
|
||||
tanggalMasuk: t.Optional(t.String()), // ISO string (YYYY-MM-DD)
|
||||
email: t.Optional(t.String()),
|
||||
telepon: t.Optional(t.String()),
|
||||
alamat: t.Optional(t.String()),
|
||||
posisiId: t.String(),
|
||||
}),
|
||||
})
|
||||
|
||||
// ✅ Update
|
||||
.put(
|
||||
"/:id",
|
||||
async (context) => {
|
||||
const response = await pegawaiUpdate(context);
|
||||
return response;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
id: t.String(),
|
||||
namaLengkap: t.String(),
|
||||
gelarAkademik: t.String(),
|
||||
imageId: t.String(),
|
||||
tanggalMasuk: t.String(),
|
||||
email: t.String(),
|
||||
telepon: t.String(),
|
||||
alamat: t.String(),
|
||||
posisiId: t.String(),
|
||||
isActive: t.Boolean(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
|
||||
// ✅ Delete
|
||||
.delete("/del/:id", pegawaiDelete);
|
||||
|
||||
export default Pegawai;
|
||||
141
src/app/api/[[...slugs]]/_lib/ppid/struktur_ppid/pegawai/updt.ts
Normal file
141
src/app/api/[[...slugs]]/_lib/ppid/struktur_ppid/pegawai/updt.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
type FormUpdate = Prisma.PegawaiPPIDGetPayload<{
|
||||
select: {
|
||||
id: true;
|
||||
namaLengkap: true;
|
||||
gelarAkademik: true;
|
||||
tanggalMasuk: true;
|
||||
email: true;
|
||||
imageId: true;
|
||||
telepon: true;
|
||||
alamat: true;
|
||||
posisiId: true;
|
||||
isActive: boolean;
|
||||
};
|
||||
}>;
|
||||
export default async function pegawaiUpdate(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
const body = (await context.body) as Omit<FormUpdate, "id">;
|
||||
|
||||
body.isActive = String(body.isActive).toLowerCase() === 'true';
|
||||
|
||||
const {
|
||||
namaLengkap,
|
||||
gelarAkademik,
|
||||
tanggalMasuk,
|
||||
email,
|
||||
telepon,
|
||||
imageId,
|
||||
alamat,
|
||||
posisiId,
|
||||
} = body;
|
||||
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "ID tidak boleh kosong",
|
||||
}),
|
||||
{
|
||||
status: 400,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const existing = await prisma.pegawaiPPID.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Data tidak ditemukan",
|
||||
}),
|
||||
{
|
||||
status: 404,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (existing.imageId !== imageId) {
|
||||
const oldImage = existing.image;
|
||||
if (oldImage) {
|
||||
try {
|
||||
const filePath = path.join(oldImage.path, oldImage.name);
|
||||
await fs.unlink(filePath);
|
||||
await prisma.fileStorage.delete({
|
||||
where: { id: oldImage.id },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Gagal hapus gambar lama:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.pegawaiPPID.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
namaLengkap,
|
||||
gelarAkademik,
|
||||
tanggalMasuk,
|
||||
email,
|
||||
imageId,
|
||||
telepon,
|
||||
alamat,
|
||||
posisiId
|
||||
},
|
||||
include: {
|
||||
image: true,
|
||||
posisi: true
|
||||
}
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Profile PPID Berhasil Dibuat",
|
||||
data: updated,
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error updating profile PPID:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mengupdate profile PPID",
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = {
|
||||
nama: string;
|
||||
deskripsi: string;
|
||||
hierarki: number;
|
||||
}
|
||||
|
||||
export default async function posisiOrganisasiCreate(context: Context) {
|
||||
const body = context.body as FormCreate;
|
||||
|
||||
if(!body) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Body is required",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const posisiOrganisasi = await prisma.posisiOrganisasiPPID.create({
|
||||
data: {
|
||||
nama: body.nama,
|
||||
deskripsi: body.deskripsi,
|
||||
hierarki: body.hierarki,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Success create posisi organisasi",
|
||||
data: posisiOrganisasi
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating PosisiOrganisasi:", error);
|
||||
throw new Error("Failed to create PosisiOrganisasi: " + (error as Error).message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function posisiOrganisasiDelete(context: Context) {
|
||||
const { id } = context.params as { id: string };
|
||||
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "ID wajib diisi",
|
||||
}),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if the position exists first
|
||||
const existing = await prisma.posisiOrganisasiPPID.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Posisi organisasi tidak ditemukan",
|
||||
}),
|
||||
{ status: 404, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if there are any pegawai associated with this position
|
||||
const pegawaiCount = await prisma.pegawaiPPID.count({
|
||||
where: { posisiId: id },
|
||||
});
|
||||
|
||||
if (pegawaiCount > 0) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Tidak dapat menghapus posisi yang masih memiliki pegawai",
|
||||
}),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
// If all checks pass, delete the position
|
||||
const deleted = await prisma.posisiOrganisasiPPID.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
message: "Posisi organisasi berhasil dihapus",
|
||||
data: deleted,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error delete posisi organisasi:", error);
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat menghapus posisi organisasi",
|
||||
}),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function posisiOrganisasiFindMany() {
|
||||
const data = await prisma.posisiOrganisasiPPID.findMany();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mengambil semua data posisi organisasi",
|
||||
data: data.map((item: any) => ({
|
||||
id: item.id,
|
||||
nama: item.nama,
|
||||
deskripsi: item.deskripsi,
|
||||
hierarki: item.hierarki,
|
||||
})),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function posisiOrganisasiFindUnique(context: Context) {
|
||||
const url = new URL(context.request.url);
|
||||
const pathSegments = url.pathname.split('/');
|
||||
const id = pathSegments[pathSegments.length - 1];
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof id !== 'string') {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
const data = await prisma.posisiOrganisasiPPID.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Posisi organisasi tidak ditemukan",
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success find posisi organisasi",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil posisi organisasi: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import posisiOrganisasiFindMany from "./findMany";
|
||||
import posisiOrganisasiFindUnique from "./findUnique";
|
||||
import posisiOrganisasiCreate from "./create";
|
||||
import posisiOrganisasiUpdate from "./updt";
|
||||
import posisiOrganisasiDelete from "./del";
|
||||
|
||||
const PosisiOrganisasi = new Elysia({
|
||||
prefix: "/posisiorganisasi",
|
||||
tags: ["PPID/Struktur PPID/Posisi Organisasi"],
|
||||
})
|
||||
|
||||
.get("/find-many", posisiOrganisasiFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await posisiOrganisasiFindUnique(context);
|
||||
return response;
|
||||
})
|
||||
.post("/create", posisiOrganisasiCreate, {
|
||||
body: t.Object({
|
||||
nama: t.String(),
|
||||
deskripsi: t.String(),
|
||||
hierarki: t.Number(),
|
||||
}),
|
||||
})
|
||||
.put("/:id", async (context) => {
|
||||
const response = await posisiOrganisasiUpdate(context);
|
||||
return response;
|
||||
}, {
|
||||
body: t.Object({
|
||||
nama: t.String(),
|
||||
deskripsi: t.String(),
|
||||
hierarki: t.Number(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", posisiOrganisasiDelete);
|
||||
|
||||
export default PosisiOrganisasi;
|
||||
@@ -0,0 +1,49 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdate = {
|
||||
id: string;
|
||||
nama: string;
|
||||
deskripsi: string;
|
||||
hierarki: number;
|
||||
};
|
||||
|
||||
export default async function posisiOrganisasiUpdate(context: Context) {
|
||||
const body = context.body as FormUpdate;
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.posisiOrganisasiPPID.update({
|
||||
where: { id },
|
||||
data: {
|
||||
nama: body.nama,
|
||||
deskripsi: body.deskripsi,
|
||||
hierarki: body.hierarki,
|
||||
},
|
||||
});
|
||||
|
||||
const updated = await prisma.posisiOrganisasiPPID.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success update posisi organisasi",
|
||||
data: updated,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Update error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal update posisi organisasi",
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,42 +2,87 @@ 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) {
|
||||
const body = (await context.body) as {
|
||||
nama: string;
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
||||
try {
|
||||
const body = (await context.body) as RegisterBody;
|
||||
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: body.email },
|
||||
});
|
||||
// Validasi input
|
||||
if (!body.nama || !body.email || !body.password) {
|
||||
context.set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
message: "Semua field harus diisi",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
if (existingUser) {
|
||||
// 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: "Email sudah terdaftar",
|
||||
message: "Terjadi kesalahan saat mendaftar",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
const role = await prisma.role.findFirst({ where: { name: "warga" } });
|
||||
|
||||
if (!role) throw new Error("Role warga tidak ditemukan");
|
||||
|
||||
const hashedPassword = await bcrypt.hash(body.password, 10);
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
nama: body.nama,
|
||||
email: body.email,
|
||||
password: hashedPassword,
|
||||
roleId: role.id,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil daftar sebagai warga",
|
||||
data: user,
|
||||
};
|
||||
}
|
||||
|
||||
26
src/app/api/[[...slugs]]/_lib/user/role/create.ts
Normal file
26
src/app/api/[[...slugs]]/_lib/user/role/create.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export default async function roleCreate(context: Context) {
|
||||
const body = (await context.body) as FormCreate;
|
||||
|
||||
try {
|
||||
const result = await prisma.role.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil membuat role",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating role:", error);
|
||||
throw new Error("Gagal membuat role: " + (error as Error).message);
|
||||
}
|
||||
}
|
||||
16
src/app/api/[[...slugs]]/_lib/user/role/del.ts
Normal file
16
src/app/api/[[...slugs]]/_lib/user/role/del.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function roleDelete(context: Context) {
|
||||
const id = context.params.id as string;
|
||||
|
||||
await prisma.role.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
success: true,
|
||||
message: "Success delete role",
|
||||
};
|
||||
}
|
||||
11
src/app/api/[[...slugs]]/_lib/user/role/findMany.ts
Normal file
11
src/app/api/[[...slugs]]/_lib/user/role/findMany.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function roleFindMany() {
|
||||
const data = await prisma.role.findMany();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success get all role",
|
||||
data,
|
||||
};
|
||||
}
|
||||
46
src/app/api/[[...slugs]]/_lib/user/role/findUnique.ts
Normal file
46
src/app/api/[[...slugs]]/_lib/user/role/findUnique.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export default async function roleFindUnique(request: Request) {
|
||||
const url = new URL(request.url);
|
||||
const pathSegments = url.pathname.split('/');
|
||||
const id = pathSegments[pathSegments.length - 1];
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof id !== 'string') {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID is required",
|
||||
}
|
||||
}
|
||||
|
||||
const data = await prisma.role.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Data not found",
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Success get role",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find by ID error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data: " + (error instanceof Error ? error.message : 'Unknown error'),
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/app/api/[[...slugs]]/_lib/user/role/index.ts
Normal file
33
src/app/api/[[...slugs]]/_lib/user/role/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import roleCreate from "./create";
|
||||
import roleDelete from "./del";
|
||||
import roleFindMany from "./findMany";
|
||||
import roleFindUnique from "./findUnique";
|
||||
import roleUpdate from "./updt";
|
||||
|
||||
const Role = new Elysia({
|
||||
prefix: "/api/role",
|
||||
tags: ["User / Role"],
|
||||
})
|
||||
|
||||
.post("/create", roleCreate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
})
|
||||
|
||||
.get("/findMany", roleFindMany)
|
||||
.get("/:id", async (context) => {
|
||||
const response = await roleFindUnique(
|
||||
new Request(context.request)
|
||||
);
|
||||
return response;
|
||||
})
|
||||
.put("/:id", roleUpdate, {
|
||||
body: t.Object({
|
||||
name: t.String(),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", roleDelete);
|
||||
|
||||
export default Role;
|
||||
28
src/app/api/[[...slugs]]/_lib/user/role/updt.ts
Normal file
28
src/app/api/[[...slugs]]/_lib/user/role/updt.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdate = {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export default async function roleUpdate(context: Context) {
|
||||
const body = (await context.body) as FormUpdate;
|
||||
const id = context.params.id as string;
|
||||
|
||||
try {
|
||||
const result = await prisma.role.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mengupdate role",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error updating role:", error);
|
||||
throw new Error("Gagal mengupdate role: " + (error as Error).message);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import Lingkungan from "./_lib/lingkungan";
|
||||
import LandingPage from "./_lib/landing_page";
|
||||
import Pendidikan from "./_lib/pendidikan";
|
||||
import User from "./_lib/user";
|
||||
import Role from "./_lib/user/role";
|
||||
|
||||
|
||||
|
||||
@@ -92,6 +93,7 @@ const ApiServer = new Elysia()
|
||||
.use(Lingkungan)
|
||||
.use(Pendidikan)
|
||||
.use(User)
|
||||
.use(Role)
|
||||
|
||||
.onError(({ code }) => {
|
||||
if (code === "NOT_FOUND") {
|
||||
|
||||
Reference in New Issue
Block a user