From 06feeae9a5aba9bc5a558483afe0d55576bbeb38 Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Wed, 29 Oct 2025 14:41:40 +0800 Subject: [PATCH] upd: api pengaduan Deskripsi: - update seeder kategori pengaduan - list pengaduan warga NO Issues --- prisma/seed.ts | 37 ++++++- src/server/routes/pengaduan_route.ts | 150 +++++++++++++++++++++++++-- 2 files changed, 174 insertions(+), 13 deletions(-) diff --git a/prisma/seed.ts b/prisma/seed.ts index f534a4c..f5d4e0b 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,5 +1,28 @@ import { prisma } from "@/server/lib/prisma"; +const category = [ + { + id: "lainnya", + name: "Lainnya" + }, + { + id: "kebersihan", + name: "Kebersihan" + }, + { + id: "keamanan", + name: "Keamanan" + }, + { + id: "pelayanan", + name: "Pelayanan" + }, + { + id: "infrastruktur", + name: "Infrastruktur" + }, +] + const role = [ { id: "developer", @@ -36,7 +59,7 @@ const user = [ console.log(`✅ Role ${r.name} seeded successfully`) } - + for (const u of user) { await prisma.user.upsert({ where: { email: u.email }, @@ -47,7 +70,17 @@ const user = [ console.log(`✅ User ${u.email} seeded successfully`) } - + for (const c of category) { + await prisma.categoryPengaduan.upsert({ + where: { id: c.id }, + create: c, + update: c + }) + + console.log(`✅ Category ${c.name} seeded successfully`) + } + + })().catch((e) => { diff --git a/src/server/routes/pengaduan_route.ts b/src/server/routes/pengaduan_route.ts index f8cce8c..e172687 100644 --- a/src/server/routes/pengaduan_route.ts +++ b/src/server/routes/pengaduan_route.ts @@ -18,8 +18,9 @@ const PengaduanRoute = new Elysia({ return data }, { detail: { - summary: "get kategori pengaduan", - description: `tool untuk mendapatkan kategori pengaduan` + summary: "List Kategori Pengaduan", + description: `tool untuk mendapatkan list kategori pengaduan`, + tags: ["mcp"] } }) .post("/category/create", async ({ body }) => { @@ -100,15 +101,67 @@ const PengaduanRoute = new Elysia({ // --- PENGADUAN --- .post("/create", async ({ body }) => { - const { title, detail, location, image, idCategory, idWarga } = body + const { title, detail, location, image, idCategory, idWarga, phone } = body const noPengaduan = await generateNoPengaduan() + let idCategoryFix = idCategory + let idWargaFix = idWarga + const category = await prisma.categoryPengaduan.findUnique({ + where: { + id: idCategory, + } + }) + + if (!category) { + const cariCategory = await prisma.categoryPengaduan.findFirst({ + where: { + name: idCategory, + } + }) + + if (!cariCategory) { + idCategoryFix = "lainnya" + } else { + idCategoryFix = cariCategory.id + } + + } + + const warga = await prisma.warga.findUnique({ + where: { + id: idWarga, + } + }) + + if (!warga) { + const cariWarga = await prisma.warga.findFirst({ + where: { + phone, + } + }) + + if (!cariWarga) { + const wargaCreate = await prisma.warga.create({ + data: { + name: idWarga, + phone, + }, + select: { + id: true + } + }) + idWargaFix = wargaCreate.id + } else { + idWargaFix = cariWarga.id + } + + } const pengaduan = await prisma.pengaduan.create({ data: { title, detail, - idCategory, - idWarga, + idCategory: idCategoryFix, + idWarga: idWargaFix, location, image, noPengaduan, @@ -138,13 +191,15 @@ const PengaduanRoute = new Elysia({ title: t.String({ minLength: 1, error: "title harus diisi" }), detail: t.String({ minLength: 1, error: "detail harus diisi" }), location: t.String({ minLength: 1, error: "location harus diisi" }), - image: t.String({ minLength: 1, error: "image harus diisi" }), + image: t.Any(), idCategory: t.String({ minLength: 1, error: "idCategory harus diisi" }), idWarga: t.String({ minLength: 1, error: "idWarga harus diisi" }), + phone: t.String({ minLength: 1, error: "phone harus diisi" }), }), detail: { - summary: "buat pengaduan", - description: `tool untuk membuat pengaduan` + summary: "Create Pengaduan Warga", + description: `tool untuk membuat pengaduan warga`, + tags: ["mcp"] } }) .post("/update-status", async ({ body }) => { @@ -197,7 +252,7 @@ const PengaduanRoute = new Elysia({ }), detail: { - summary: "update status pengaduan", + summary: "Update status pengaduan", description: `tool untuk update status pengaduan` } }) @@ -282,8 +337,81 @@ const PengaduanRoute = new Elysia({ return datafix }, { detail: { - summary: "get detail pengaduan", - description: `tool untuk mendapatkan detail pengaduan` + summary: "Detail Pengaduan Warga", + description: `tool untuk mendapatkan detail pengaduan warga / history pengaduan / mengecek status pengaduan`, + tags: ["mcp"] + } + }) + .get("/", async ({ query }) => { + const { take, page, search } = query + const skip = !page ? 0 : (Number(page) - 1) * (!take ? 10 : Number(take)) + + const data = await prisma.pengaduan.findMany({ + skip, + take: !take ? 10 : Number(take), + orderBy: { + createdAt: "asc" + }, + where: { + isActive: true, + OR: [ + { + title: { + contains: search ?? "", + mode: "insensitive" + }, + }, + { + noPengaduan: { + contains: search ?? "", + mode: "insensitive" + }, + }, + { + detail: { + contains: search ?? "", + mode: "insensitive" + }, + } + ], + }, + select: { + id: true, + noPengaduan: true, + title: true, + detail: true, + location: true, + status: true, + createdAt: true, + CategoryPengaduan: { + select: { + name: true + } + }, + Warga: { + select: { + name: true, + } + } + } + }) + + const dataFix = data.map((item) => { + return { + noPengaduan: item.noPengaduan, + title: item.title, + detail: item.detail, + status: item.status, + createdAt: item.createdAt.toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric" }), + } + }) + + return dataFix + }, { + detail: { + summary: "List Pengaduan Warga", + description: `tool untuk mendapatkan list pengaduan warga`, + tags: ["mcp"] } }) export default PengaduanRoute -- 2.49.1