diff --git a/src/index.tsx b/src/index.tsx index cc5738b..d540fc4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -20,6 +20,7 @@ import SuratRoute from "./server/routes/surat_route"; import TestPengaduanRoute from "./server/routes/test_pengaduan"; import UserRoute from "./server/routes/user_route"; import WargaRoute from "./server/routes/warga_route"; +import NocRoute from "./server/routes/noc_route"; const Docs = new Elysia({ tags: ["docs"], @@ -47,7 +48,8 @@ const Api = new Elysia({ .use(UserRoute) .use(LayananRoute) .use(AduanRoute) - .use(SendWaRoute); + .use(SendWaRoute) + .use(NocRoute); const app = new Elysia() .use(Api) diff --git a/src/server/routes/noc_route.ts b/src/server/routes/noc_route.ts new file mode 100644 index 0000000..9fd3aa4 --- /dev/null +++ b/src/server/routes/noc_route.ts @@ -0,0 +1,215 @@ +import Elysia from "elysia"; +import { prisma } from "../lib/prisma"; + + +const NocRoute = new Elysia({ + prefix: "noc", + tags: ["noc"], +}) + + .get("/surat-perminggu", async () => { + const now = new Date(); + const startOfThisWeek = new Date(now); + const day = now.getDay(); + const diff = (day === 0 ? 6 : day - 1); // Adjust for Monday as start (Sunday=0 becomes 6, Monday=1 becomes 0) + startOfThisWeek.setDate(now.getDate() - diff); + startOfThisWeek.setHours(0, 0, 0, 0); + + const endOfThisWeek = new Date(startOfThisWeek); + endOfThisWeek.setDate(startOfThisWeek.getDate() + 7); + + const startOfLastWeek = new Date(startOfThisWeek); + startOfLastWeek.setDate(startOfThisWeek.getDate() - 7); + + const endOfLastWeek = new Date(startOfThisWeek); + + const [thisWeekCount, lastWeekCount] = await Promise.all([ + prisma.suratPelayanan.count({ + where: { + isActive: true, + createdAt: { + gte: startOfThisWeek, + lt: endOfThisWeek, + } + } + }), + prisma.suratPelayanan.count({ + where: { + isActive: true, + createdAt: { + gte: startOfLastWeek, + lt: endOfLastWeek, + } + } + }) + ]); + + let percentageIncrease = 0; + if (lastWeekCount > 0) { + percentageIncrease = ((thisWeekCount - lastWeekCount) / lastWeekCount) * 100; + } else if (thisWeekCount > 0) { + percentageIncrease = 100; + } + + return { + jumlah: thisWeekCount, // jumlah surat minggu ini + persentase_kenaikan: Number(percentageIncrease.toFixed(2)) // persentase kenaikan dari minggu lalu + }; + + }, { + detail: { + summary: "Get jumlah surat minggu ini dan kenaikan dari minggu lalu", + description: `tool untuk mendapatkan jumlah surat minggu ini dan persentase kenaikan dibandingkan minggu lalu`, + } + }) + + .get("/pengaduan-count", async () => { + const [antrian, diterima, dikerjakan] = await Promise.all([ + prisma.pengaduan.count({ + where: { + isActive: true, + status: "antrian", + } + }), + prisma.pengaduan.count({ + where: { + isActive: true, + status: "dikerjakan", + } + }), + prisma.pengaduan.count({ + where: { + isActive: true, + status: "diterima", + } + }) + ]); + + return { + antrian, + diterima, + dikerjakan, + total: antrian + diterima + dikerjakan + }; + }, { + detail: { + summary: "Get jumlah pengaduan antrian, diterima dan dikerjakan", + description: "Menghitung jumlah pengaduan yang berstatus antrian dan sedang dikerjakan (diproses)", + } + }) + + .get("/pelayanan-count", async () => { + const now = new Date(); + + // Bulan ini + const startOfThisMonth = new Date(now.getFullYear(), now.getMonth(), 1); + const endOfThisMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1); + + // Bulan lalu + const startOfLastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1); + const endOfLastMonth = new Date(now.getFullYear(), now.getMonth(), 1); + + const [thisMonthCount, lastMonthCount] = await Promise.all([ + prisma.pelayananAjuan.count({ + where: { + isActive: true, + status: "selesai", + createdAt: { + gte: startOfThisMonth, + lt: endOfThisMonth, + } + } + }), + prisma.pelayananAjuan.count({ + where: { + isActive: true, + status: "selesai", + createdAt: { + gte: startOfLastMonth, + lt: endOfLastMonth, + } + } + }) + ]); + + let percentageIncrease = 0; + if (lastMonthCount > 0) { + percentageIncrease = ((thisMonthCount - lastMonthCount) / lastMonthCount) * 100; + } else if (thisMonthCount > 0) { + percentageIncrease = 100; + } + + return { + total_bulan_ini: thisMonthCount, + persentase_kenaikan: Number(percentageIncrease.toFixed(2)) + }; + }, { + detail: { + summary: "Get total pelayananAjuan selesai bulan ini dan kenaikan dari bulan lalu", + description: "Menampilkan total pelayananAjuan yang telah berstatus selesai bulan ini dan persentase kenaikan dari bulan lalu", + } + }) + + .get("/pengajuan-history", async ({ query }) => { + const { period = "6months" } = query as { period?: string }; + const now = new Date(); + const results: { label: string; total: number }[] = []; + + if (period === "4weeks") { + // Get the most recent Monday + const currentDay = now.getDay(); + const diffToMonday = (currentDay === 0 ? 6 : currentDay - 1); + const startOfCurrentWeek = new Date(now); + startOfCurrentWeek.setDate(now.getDate() - diffToMonday); + startOfCurrentWeek.setHours(0, 0, 0, 0); + + for (let i = 3; i >= 0; i--) { + const startOfWeek = new Date(startOfCurrentWeek); + startOfWeek.setDate(startOfCurrentWeek.getDate() - (i * 7)); + + const endOfWeek = new Date(startOfWeek); + endOfWeek.setDate(startOfWeek.getDate() + 7); + + const count = await prisma.pelayananAjuan.count({ + where: { + isActive: true, + createdAt: { + gte: startOfWeek, + lt: endOfWeek, + } + } + }); + + const label = `Minggu ${4 - i}`; + results.push({ label, total: count }); + } + } else { + // Default 6 months + for (let i = 5; i >= 0; i--) { + const startOfMonth = new Date(now.getFullYear(), now.getMonth() - i, 1); + const endOfMonth = new Date(now.getFullYear(), now.getMonth() - i + 1, 1); + + const count = await prisma.pelayananAjuan.count({ + where: { + isActive: true, + createdAt: { + gte: startOfMonth, + lt: endOfMonth, + } + } + }); + + const monthName = startOfMonth.toLocaleString('id-ID', { month: 'long' }); + results.push({ label: monthName, total: count }); + } + } + + return results; + }, { + detail: { + summary: "Get history total pengajuan surat", + description: "Menampilkan total pengajuan surat selama 6 bulan terakhir atau 4 minggu terakhir", + } + }) + +export default NocRoute