feat(kesehatan): tambah relasi banjar ke penderita penyakit dan CRUD banjar admin
- Tambah field banjarId (optional) ke GrafikKepuasan + migration - API CRUD banjar baru di /api/desa/banjar/* - Update API grafik_kepuasan: create, find-many (filter by banjar), findUnique, updt - semua include banjar - State: tambah banjarId ke form, banjarList proxy, filter by banjarId - UI list: kolom Banjar di tabel desktop, kartu mobile, Select filter by banjar - UI create/edit: Select banjar (opsional), load banjarList on mount - UI detail: tampilkan field Banjar - Admin banjar: halaman list, create, detail - Sidebar: menu Banjar di domain Desa Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
24
src/app/api/[[...slugs]]/_lib/desa/banjar/create.ts
Normal file
24
src/app/api/[[...slugs]]/_lib/desa/banjar/create.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormCreate = {
|
||||
name: string;
|
||||
};
|
||||
|
||||
export default async function banjarCreate(context: Context) {
|
||||
const body = (await context.body) as FormCreate;
|
||||
|
||||
try {
|
||||
const result = await prisma.banjar.create({
|
||||
data: { name: body.name },
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil membuat banjar",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating banjar:", error);
|
||||
throw new Error("Gagal membuat banjar: " + (error as Error).message);
|
||||
}
|
||||
}
|
||||
42
src/app/api/[[...slugs]]/_lib/desa/banjar/del.ts
Normal file
42
src/app/api/[[...slugs]]/_lib/desa/banjar/del.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function banjarDelete(context: Context) {
|
||||
try {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return Response.json({ success: false, message: "ID tidak boleh kosong" }, { status: 400 });
|
||||
}
|
||||
|
||||
const posyanduCount = await prisma.posyandu.count({
|
||||
where: { banjarId: id, isActive: true },
|
||||
});
|
||||
|
||||
if (posyanduCount > 0) {
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
message: `Banjar tidak dapat dihapus karena masih digunakan oleh ${posyanduCount} posyandu`,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.banjar.update({
|
||||
where: { id },
|
||||
data: { isActive: false },
|
||||
});
|
||||
|
||||
return { success: true, message: "Banjar berhasil dihapus" };
|
||||
} catch (error) {
|
||||
console.error("Delete banjar error:", error);
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal menghapus banjar: " + (error instanceof Error ? error.message : "Unknown error"),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
43
src/app/api/[[...slugs]]/_lib/desa/banjar/findMany.ts
Normal file
43
src/app/api/[[...slugs]]/_lib/desa/banjar/findMany.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
async function banjarFindMany(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;
|
||||
|
||||
const where: any = { isActive: true };
|
||||
|
||||
if (search) {
|
||||
where.OR = [{ name: { contains: search, mode: "insensitive" } }];
|
||||
}
|
||||
|
||||
try {
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.banjar.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { name: "asc" },
|
||||
}),
|
||||
prisma.banjar.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil ambil data banjar",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error di banjar findMany:", e);
|
||||
return { success: false, message: "Gagal mengambil data banjar" };
|
||||
}
|
||||
}
|
||||
|
||||
export default banjarFindMany;
|
||||
21
src/app/api/[[...slugs]]/_lib/desa/banjar/findUnique.ts
Normal file
21
src/app/api/[[...slugs]]/_lib/desa/banjar/findUnique.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function banjarFindUnique(context: Context) {
|
||||
const id = context.params?.id as string;
|
||||
|
||||
if (!id) {
|
||||
return Response.json({ success: false, message: "ID wajib diisi" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await prisma.banjar.findUnique({ where: { id } });
|
||||
if (!data) {
|
||||
return Response.json({ success: false, message: "Banjar tidak ditemukan" }, { status: 404 });
|
||||
}
|
||||
return { success: true, data };
|
||||
} catch (error) {
|
||||
console.error("Error fetching banjar:", error);
|
||||
throw new Error("Gagal mengambil data banjar");
|
||||
}
|
||||
}
|
||||
22
src/app/api/[[...slugs]]/_lib/desa/banjar/index.ts
Normal file
22
src/app/api/[[...slugs]]/_lib/desa/banjar/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import banjarCreate from "./create";
|
||||
import banjarDelete from "./del";
|
||||
import banjarFindMany from "./findMany";
|
||||
import banjarFindUnique from "./findUnique";
|
||||
import banjarUpdate from "./updt";
|
||||
|
||||
const Banjar = new Elysia({
|
||||
prefix: "/banjar",
|
||||
tags: ["Desa / Banjar"],
|
||||
})
|
||||
.post("/create", banjarCreate, {
|
||||
body: t.Object({ name: t.String() }),
|
||||
})
|
||||
.get("/findMany", banjarFindMany)
|
||||
.get("/:id", banjarFindUnique)
|
||||
.put("/:id", banjarUpdate, {
|
||||
body: t.Object({ name: t.String() }),
|
||||
})
|
||||
.delete("/del/:id", banjarDelete);
|
||||
|
||||
export default Banjar;
|
||||
26
src/app/api/[[...slugs]]/_lib/desa/banjar/updt.ts
Normal file
26
src/app/api/[[...slugs]]/_lib/desa/banjar/updt.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
type FormUpdate = {
|
||||
name: string;
|
||||
};
|
||||
|
||||
export default async function banjarUpdate(context: Context) {
|
||||
const body = (await context.body) as FormUpdate;
|
||||
const id = context.params.id as string;
|
||||
|
||||
try {
|
||||
const result = await prisma.banjar.update({
|
||||
where: { id },
|
||||
data: { name: body.name },
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mengupdate banjar",
|
||||
data: result,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error updating banjar:", error);
|
||||
throw new Error("Gagal mengupdate banjar: " + (error as Error).message);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import Musik from "./musik";
|
||||
import KegiatanDesa from "./kegiatan-desa";
|
||||
import KategoriKegiatan from "./kegiatan-desa/kategori-kegiatan";
|
||||
import EventBudaya from "./event-budaya";
|
||||
import Banjar from "./banjar";
|
||||
|
||||
|
||||
const Desa = new Elysia({ prefix: "/desa", tags: ["Desa"] })
|
||||
@@ -36,6 +37,7 @@ const Desa = new Elysia({ prefix: "/desa", tags: ["Desa"] })
|
||||
.use(KegiatanDesa)
|
||||
.use(KategoriKegiatan)
|
||||
.use(EventBudaya)
|
||||
.use(Banjar)
|
||||
|
||||
|
||||
export default Desa;
|
||||
|
||||
@@ -7,6 +7,7 @@ type FormCreate = {
|
||||
jenisKelamin: string;
|
||||
alamat: string;
|
||||
penyakit: string;
|
||||
banjarId?: string;
|
||||
};
|
||||
|
||||
export default async function grafikKepuasanCreate(context: Context) {
|
||||
@@ -19,14 +20,9 @@ export default async function grafikKepuasanCreate(context: Context) {
|
||||
jenisKelamin: body.jenisKelamin,
|
||||
alamat: body.alamat,
|
||||
penyakit: body.penyakit,
|
||||
...(body.banjarId ? { banjarId: body.banjarId } : {}),
|
||||
},
|
||||
select: {
|
||||
nama: true,
|
||||
tanggal: true,
|
||||
jenisKelamin: true,
|
||||
alamat: true,
|
||||
penyakit: true,
|
||||
}
|
||||
include: { banjar: { select: { id: true, name: true } } },
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
|
||||
@@ -23,6 +23,12 @@ async function grafikKepuasanFindMany(context: Context) {
|
||||
];
|
||||
}
|
||||
|
||||
// Filter berdasarkan banjarId jika ada
|
||||
const banjarId = (context.query.banjarId as string) || '';
|
||||
if (banjarId) {
|
||||
where.banjarId = banjarId;
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil data dan total count secara paralel
|
||||
const [data, total] = await Promise.all([
|
||||
@@ -31,6 +37,7 @@ async function grafikKepuasanFindMany(context: Context) {
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { nama: 'asc' },
|
||||
include: { banjar: { select: { id: true, name: true } } },
|
||||
}),
|
||||
prisma.grafikKepuasan.count({ where }),
|
||||
]);
|
||||
|
||||
@@ -23,6 +23,7 @@ export default async function grafikKepuasanFindUnique(request: Request) {
|
||||
|
||||
const data = await prisma.grafikKepuasan.findUnique({
|
||||
where: { id },
|
||||
include: { banjar: { select: { id: true, name: true } } },
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
|
||||
@@ -21,6 +21,7 @@ const GrafikKepuasan = new Elysia({
|
||||
jenisKelamin: t.String(),
|
||||
alamat: t.String(),
|
||||
penyakit: t.String(),
|
||||
banjarId: t.Optional(t.String()),
|
||||
}),
|
||||
})
|
||||
.put("/:id", grafikKepuasanUpdate, {
|
||||
@@ -30,6 +31,7 @@ const GrafikKepuasan = new Elysia({
|
||||
jenisKelamin: t.String(),
|
||||
alamat: t.String(),
|
||||
penyakit: t.String(),
|
||||
banjarId: t.Optional(t.String()),
|
||||
}),
|
||||
})
|
||||
.delete("/del/:id", grafikKepuasanDelete)
|
||||
|
||||
@@ -7,6 +7,7 @@ type FormUpdate = {
|
||||
jenisKelamin: string;
|
||||
alamat: string;
|
||||
penyakit: string;
|
||||
banjarId?: string;
|
||||
}
|
||||
|
||||
export default async function grafikKepuasanUpdate(context: Context) {
|
||||
@@ -22,7 +23,9 @@ export default async function grafikKepuasanUpdate(context: Context) {
|
||||
jenisKelamin: body.jenisKelamin,
|
||||
alamat: body.alamat,
|
||||
penyakit: body.penyakit,
|
||||
banjarId: body.banjarId || null,
|
||||
},
|
||||
include: { banjar: { select: { id: true, name: true } } },
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user