Fix UI & API Admin Menu Kesehatan, Submenu Data Kesehatan Warga Bagian ChartBar

This commit is contained in:
2025-08-14 20:47:07 +08:00
parent 5e137ba658
commit d7a592c635
34 changed files with 2285 additions and 445 deletions

View File

@@ -1,32 +1,22 @@
import prisma from "@/lib/prisma";
import { Prisma } from "@prisma/client";
import { Context } from "elysia";
type FormCreate = Prisma.DataKematian_KelahiranGetPayload<{
select: {
tahun: true;
kematianKasar: true;
kematianBayi: true;
kelahiranKasar: true;
};
}>;
type FormCreate = {
kematianId: string;
kelahiranId: string;
};
export default async function persentaseKelahiranKematianCreate(context: Context) {
const body = context.body as FormCreate
const created = await prisma.dataKematian_Kelahiran.create({
data: {
tahun: body.tahun,
kematianKasar: body.kematianKasar,
kematianBayi: body.kematianBayi,
kelahiranKasar: body.kelahiranKasar,
kematianId: body.kematianId,
kelahiranId: body.kelahiranId,
},
select: {
id: true,
tahun: true,
kematianKasar: true,
kematianBayi: true,
kelahiranKasar: true,
kematianId: true,
kelahiranId: true,
}
})
return{

View File

@@ -1,9 +1,58 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function persentaseKelahiranKematianFindMany(context: Context) {
// Ambil parameter dari query
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 = [
{ kematian: { nama: { contains: search, mode: 'insensitive' } } },
{ kelahiran: { nama: { contains: search, mode: 'insensitive' } } },
];
}
try {
// Ambil data dan total count secara paralel
const [data, total] = await Promise.all([
prisma.dataKematian_Kelahiran.findMany({
where,
include: {
kematian: true,
kelahiran: true,
},
skip,
take: limit,
orderBy: { createdAt: 'desc' },
}),
prisma.dataKematian_Kelahiran.count({ where }),
]);
export default async function persentaseKelahiranKematianFindMany() {
const res = await prisma.dataKematian_Kelahiran.findMany();
return {
data: res
}
success: true,
message: "Berhasil ambil data persentase kelahiran kematian dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (e) {
console.error("Error di findMany paginated:", e);
return {
success: false,
message: "Gagal mengambil data persentase kelahiran kematian",
};
}
}
export default persentaseKelahiranKematianFindMany;

View File

@@ -22,6 +22,10 @@ export default async function persentaseKelahiranKematianFindUnique(request: Req
const data = await prisma.dataKematian_Kelahiran.findUnique({
where: { id },
include: {
kematian: true,
kelahiran: true,
},
});
if (!data) {

View File

@@ -16,10 +16,8 @@ const PersentaseKelahiranKematian = new Elysia({
.get("/find-many", persentaseKelahiranKematianFindMany)
.post("/create", persentaseKelahiranKematianCreate, {
body: t.Object({
tahun: t.String(),
kematianKasar: t.String(),
kematianBayi: t.String(),
kelahiranKasar: t.String(),
kematianId: t.String(),
kelahiranId: t.String(),
}),
})
.put("/:id", persentaseKelahiranKematianUpdate, {
@@ -27,10 +25,8 @@ const PersentaseKelahiranKematian = new Elysia({
id: t.String(),
}),
body: t.Object({
tahun: t.String(),
kematianKasar: t.String(),
kematianBayi: t.String(),
kelahiranKasar: t.String(),
kematianId: t.String(),
kelahiranId: t.String(),
}),
})
.delete("/del/:id", persentaseKelahiranKematianDelete, {

View File

@@ -0,0 +1,34 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormCreate = {
nama: string;
tanggal: string;
jenisKelamin: string;
alamat: string;
}
export default async function kelahiranCreate(context: Context) {
const body = context.body as FormCreate;
const created = await prisma.kelahiran.create({
data: {
nama: body.nama,
tanggal: new Date(body.tanggal),
jenisKelamin: body.jenisKelamin,
alamat: body.alamat,
},
select: {
nama: true,
tanggal: true,
jenisKelamin: true,
alamat: true,
},
});
return {
success: true,
message: "Success create kelahiran",
data: created,
};
}

View File

@@ -0,0 +1,36 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function kelahiranDelete(context: Context) {
const id = context.params?.id;
if (!id) {
return {
success: false,
message: "ID tidak ditemukan",
}
}
const existing = await prisma.kelahiran.findUnique({
where: {
id: id,
},
})
if (!existing) {
return {
success: false,
message: "Data tidak ditemukan",
}
}
const deleted = await prisma.kelahiran.delete({
where: { id },
})
return {
success: true,
message: "Data berhasil dihapus",
data: deleted,
}
}

View File

@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function kelahiranFindMany(context: Context) {
// Ambil parameter dari query
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' } },
{ alamat: { contains: search, mode: 'insensitive' } },
];
}
try {
// Ambil data dan total count secara paralel
const [data, total] = await Promise.all([
prisma.kelahiran.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: 'desc' },
}),
prisma.kelahiran.count({ where }),
]);
return {
success: true,
message: "Berhasil ambil data kelahiran dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (e) {
console.error("Error di findMany paginated:", e);
return {
success: false,
message: "Gagal mengambil data kelahiran",
};
}
}
export default kelahiranFindMany;

View File

@@ -0,0 +1,46 @@
import prisma from "@/lib/prisma";
export default async function kelahiranFindUnique(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.kelahiran.findUnique({
where: { id },
});
if (!data) {
return {
success: false,
message: "Data not found",
}
}
return {
success: true,
message: "Success get data kelahiran",
data,
}
} catch (error) {
console.error("Find by ID error:", error);
return {
success: false,
message: "Gagal mengambil data: " + (error instanceof Error ? error.message : 'Unknown error'),
}
}
}

View File

@@ -0,0 +1,39 @@
import Elysia, { t } from "elysia";
import kelahiranCreate from "./create";
import kelahiranDelete from "./del";
import kelahiranFindMany from "./findMany";
import kelahiranFindUnique from "./findUnique";
import kelahiranUpdate from "./updt";
const Kelahiran = new Elysia({
prefix: "/kelahiran",
tags: ["Kesehatan / Data Kesehatan Warga / Persentase Kelahiran Kematian / Kelahiran"],
})
.post("/create", kelahiranCreate, {
body: t.Object({
nama: t.String(),
tanggal: t.String(),
jenisKelamin: t.String(),
alamat: t.String(),
}),
})
.get("/findMany", kelahiranFindMany)
.get("/:id", async (context) => {
const response = await kelahiranFindUnique(
new Request(context.request)
);
return response;
})
.put("/:id", kelahiranUpdate, {
body: t.Object({
nama: t.String(),
tanggal: t.String(),
jenisKelamin: t.String(),
alamat: t.String(),
}),
})
.delete("/del/:id", kelahiranDelete);
export default Kelahiran;

View File

@@ -0,0 +1,34 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormUpdate = {
nama: string;
tanggal: string;
jenisKelamin: string;
alamat: string;
}
export default async function kelahiranUpdate(context: Context) {
const body = (await context.body) as FormUpdate;
const id = context.params.id as string;
try {
const result = await prisma.kelahiran.update({
where: { id },
data: {
nama: body.nama,
tanggal: new Date(body.tanggal),
jenisKelamin: body.jenisKelamin,
alamat: body.alamat,
},
});
return {
success: true,
message: "Berhasil mengupdate data kelahiran",
data: result,
};
} catch (error) {
console.error("Error updating data kelahiran:", error);
throw new Error("Gagal mengupdate data kelahiran: " + (error as Error).message);
}
}

View File

@@ -0,0 +1,36 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormCreate = {
nama: string;
tanggal: string;
jenisKelamin: string;
alamat: string;
penyebab: string;
};
export default async function kematianCreate(context: Context) {
const body = context.body as FormCreate;
const created = await prisma.kematian.create({
data: {
nama: body.nama,
tanggal: new Date(body.tanggal),
jenisKelamin: body.jenisKelamin,
alamat: body.alamat,
penyebab: body.penyebab,
},
select: {
nama: true,
tanggal: true,
jenisKelamin: true,
alamat: true,
penyebab: true,
},
});
return {
success: true,
message: "Success create kematian",
data: created,
};
}

View File

@@ -0,0 +1,36 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
export default async function kematianDelete(context: Context) {
const id = context.params?.id;
if (!id) {
return {
success: false,
message: "ID tidak ditemukan",
}
}
const existing = await prisma.kematian.findUnique({
where: {
id: id,
},
})
if (!existing) {
return {
success: false,
message: "Data tidak ditemukan",
}
}
const deleted = await prisma.kematian.delete({
where: { id },
})
return {
success: true,
message: "Data berhasil dihapus",
data: deleted,
}
}

View File

@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// /api/berita/findManyPaginated.ts
import prisma from "@/lib/prisma";
import { Context } from "elysia";
async function kematianFindMany(context: Context) {
// Ambil parameter dari query
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' } },
{ alamat: { contains: search, mode: 'insensitive' } },
];
}
try {
// Ambil data dan total count secara paralel
const [data, total] = await Promise.all([
prisma.kematian.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: 'desc' },
}),
prisma.kematian.count({ where }),
]);
return {
success: true,
message: "Berhasil ambil data kematian dengan pagination",
data,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
} catch (e) {
console.error("Error di findMany paginated:", e);
return {
success: false,
message: "Gagal mengambil data kematian",
};
}
}
export default kematianFindMany;

View File

@@ -0,0 +1,46 @@
import prisma from "@/lib/prisma";
export default async function kematianFindUnique(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.kematian.findUnique({
where: { id },
});
if (!data) {
return {
success: false,
message: "Data not found",
}
}
return {
success: true,
message: "Success get data kematian",
data,
}
} catch (error) {
console.error("Find by ID error:", error);
return {
success: false,
message: "Gagal mengambil data: " + (error instanceof Error ? error.message : 'Unknown error'),
}
}
}

View File

@@ -0,0 +1,41 @@
import Elysia, { t } from "elysia";
import kematianCreate from "./create";
import kematianDelete from "./del";
import kematianFindMany from "./findMany";
import kematianFindUnique from "./findUnique";
import kematianUpdate from "./updt";
const Kematian = new Elysia({
prefix: "/kematian",
tags: ["Kesehatan / Data Kesehatan Warga / Persentase Kelahiran Kematian / Kematian"],
})
.post("/create", kematianCreate, {
body: t.Object({
nama: t.String(),
tanggal: t.String(),
jenisKelamin: t.String(),
alamat: t.String(),
penyebab: t.String(),
}),
})
.get("/findMany", kematianFindMany)
.get("/:id", async (context) => {
const response = await kematianFindUnique(
new Request(context.request)
);
return response;
})
.put("/:id", kematianUpdate, {
body: t.Object({
nama: t.String(),
tanggal: t.String(),
jenisKelamin: t.String(),
alamat: t.String(),
penyebab: t.String(),
}),
})
.delete("/del/:id", kematianDelete);
export default Kematian;

View File

@@ -0,0 +1,36 @@
import prisma from "@/lib/prisma";
import { Context } from "elysia";
type FormUpdate = {
nama: string;
tanggal: string;
jenisKelamin: string;
alamat: string;
penyebab: string;
}
export default async function kematianUpdate(context: Context) {
const body = (await context.body) as FormUpdate;
const id = context.params.id as string;
try {
const result = await prisma.kematian.update({
where: { id },
data: {
nama: body.nama,
tanggal: new Date(body.tanggal),
jenisKelamin: body.jenisKelamin,
alamat: body.alamat,
penyebab: body.penyebab,
},
});
return {
success: true,
message: "Berhasil mengupdate data kematian",
data: result,
};
} catch (error) {
console.error("Error updating data kematian:", error);
throw new Error("Gagal mengupdate data kematian: " + (error as Error).message);
}
}

View File

@@ -11,11 +11,9 @@ export default async function persentaseKelahiranKematianUpdate(context: Context
}
}
const {tahun, kematianKasar, kematianBayi, kelahiranKasar} = context.body as {
tahun: string;
kematianKasar: string;
kematianBayi: string;
kelahiranKasar: string;
const {kematianId, kelahiranId} = context.body as {
kematianId: string;
kelahiranId: string;
}
const existing = await prisma.dataKematian_Kelahiran.findUnique({
@@ -34,10 +32,8 @@ export default async function persentaseKelahiranKematianUpdate(context: Context
const updated = await prisma.dataKematian_Kelahiran.update({
where: { id },
data: {
tahun,
kematianKasar,
kematianBayi,
kelahiranKasar,
kematianId,
kelahiranId,
},
})

View File

@@ -16,6 +16,8 @@ import Puskesmas from "./puskesmas";
import FasilitasKesehatan from "./data_kesehatan_warga/fasilitas_kesehatan";
import JadwalKegiatan from "./data_kesehatan_warga/jadwal_kegiatan";
import ArtikelKesehatan from "./data_kesehatan_warga/artikel_kesehatan";
import Kelahiran from "./data_kesehatan_warga/persentase_kelahiran_kematian/kelahiran";
import Kematian from "./data_kesehatan_warga/persentase_kelahiran_kematian/kematian";
const Kesehatan = new Elysia({
@@ -38,5 +40,7 @@ const Kesehatan = new Elysia({
.use(InfoWabahPenyakit)
.use(FasilitasKesehatan)
.use(JadwalKegiatan)
.use(ArtikelKesehatan);
.use(ArtikelKesehatan)
.use(Kelahiran)
.use(Kematian)
export default Kesehatan;