From 42803f9b9217834a154979e6de78030981fb90e1 Mon Sep 17 00:00:00 2001 From: bagasbanuna Date: Tue, 3 Feb 2026 17:50:07 +0800 Subject: [PATCH] Fix api load data event dan notifikasi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit API – Event (Mobile) - src/app/api/mobile/event/route.ts - src/app/api/mobile/event/[id]/[status]/route.ts API – Notification (Mobile) - src/app/api/mobile/notification/[id]/route.ts Docs / Experiment - PROMPT-AI.md - zCoba.js ### No issue --- PROMPT-AI.md | 5 +- .../api/mobile/event/[id]/[status]/route.ts | 27 ++ src/app/api/mobile/event/route.ts | 296 ++++++++++-------- src/app/api/mobile/notification/[id]/route.ts | 22 +- zCoba.js | 48 ++- 5 files changed, 233 insertions(+), 165 deletions(-) diff --git a/PROMPT-AI.md b/PROMPT-AI.md index a807c539..512243a8 100644 --- a/PROMPT-AI.md +++ b/PROMPT-AI.md @@ -1,5 +1,6 @@ -File utama: src/app/api/mobile/job/route.ts +File utama: src/app/api/mobile/event/route.ts +File refrensi: src/app/api/mobile/job/[id]/[status]/route.ts Terapkan pagination pada file "File utama" pada method GET Analisa juga file "File utama", jika belum memiliki page dari seachParams maka terapkan. Juga pastikan take dan skip sudah sesuai dengan pagination. Buat default nya menjadi 10 untuk take data @@ -13,4 +14,6 @@ dan penerapannya pada query take: page ? takeData : undefined, skip: page ? skipData : undefined, +Anda bisa menggunakan refrensi dari "File refrensi" jika butuh pemahaman dengan tipe fitur yang sama + Gunakan bahasa indonesia pada cli agar saya mudah membacanya. diff --git a/src/app/api/mobile/event/[id]/[status]/route.ts b/src/app/api/mobile/event/[id]/[status]/route.ts index 53fd6609..11988199 100644 --- a/src/app/api/mobile/event/[id]/[status]/route.ts +++ b/src/app/api/mobile/event/[id]/[status]/route.ts @@ -12,6 +12,11 @@ async function GET( const { id, status } = params; const fixStatusName = _.startCase(status); + const { searchParams } = new URL(request.url); + const page = Number(searchParams.get("page")) || 1; + const takeData = 10; + const skipData = page * takeData - takeData; + const data = await prisma.event.findMany({ orderBy: { updatedAt: "desc", @@ -37,13 +42,35 @@ async function GET( }, authorId: true, }, + take: takeData, + skip: skipData, }); + // Get total count for pagination info + const totalCount = await prisma.event.count({ + where: { + active: true, + authorId: id, + isArsip: false, + EventMaster_Status: { + name: fixStatusName, + }, + }, + }); + + const totalPages = Math.ceil(totalCount / takeData); + return NextResponse.json( { success: true, message: "Success get event", data: data, + pagination: { + currentPage: page, + totalPages: totalPages, + totalData: totalCount, + dataPerPage: takeData, + }, }, { status: 200 } ); diff --git a/src/app/api/mobile/event/route.ts b/src/app/api/mobile/event/route.ts index 9dc25a2c..907b7d08 100644 --- a/src/app/api/mobile/event/route.ts +++ b/src/app/api/mobile/event/route.ts @@ -76,11 +76,15 @@ async function GET(request: Request) { const { searchParams } = new URL(request.url); const category = searchParams.get("category"); const userId = searchParams.get("userId"); + const page = Number(searchParams.get("page")) || 1; + const takeData = 5; + const skipData = page * takeData - takeData; console.log("[CAT]", category); console.log("[USER]", userId); let fixData; + let totalCount = 0; if (category === "beranda") { const allData = await prisma.event.findMany({ @@ -108,84 +112,95 @@ async function GET(request: Request) { } } - // const takeData = 10; - // const skipData = page * takeData - takeData; - - const data = await prisma.event.findMany({ - // take: takeData, - // skip: skipData, - orderBy: [ - { - tanggal: "asc", + const [data, count] = await Promise.all([ + prisma.event.findMany({ + take: takeData, + skip: skipData, + orderBy: [ + { + tanggal: "asc", + }, + ], + where: { + active: true, + eventMaster_StatusId: "1", + isArsip: false, }, - ], - where: { - active: true, - eventMaster_StatusId: "1", - isArsip: false, - }, - select: { - id: true, - title: true, - deskripsi: true, - tanggal: true, - tanggalSelesai: true, - EventMaster_Status: { - select: { - name: true, + select: { + id: true, + title: true, + deskripsi: true, + tanggal: true, + tanggalSelesai: true, + EventMaster_Status: { + select: { + name: true, + }, + }, + authorId: true, + Author: { + include: { + Profile: true, + }, }, }, - authorId: true, - Author: { - include: { - Profile: true, - }, + }), + prisma.event.count({ + where: { + active: true, + eventMaster_StatusId: "1", + isArsip: false, }, - }, - }); + }) + ]); fixData = data; + totalCount = count; } else if (category === "contribution") { - const data = await prisma.event_Peserta.findMany({ - where: { - userId: userId, - }, - select: { - eventId: true, - userId: true, - Event: { - select: { - id: true, - title: true, - tanggal: true, - Author: { - select: { - id: true, - username: true, - Profile: { - select: { - id: true, - name: true, - imageId: true, + const [data, count] = await Promise.all([ + prisma.event_Peserta.findMany({ + take: takeData, + skip: skipData, + where: { + userId: userId, + }, + select: { + eventId: true, + userId: true, + Event: { + select: { + id: true, + title: true, + tanggal: true, + Author: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, }, }, }, - }, - Event_Peserta: { - take: 4, - orderBy: { - createdAt: "desc", - }, - select: { - id: true, - userId: true, - User: { - select: { - Profile: { - select: { - id: true, - name: true, - imageId: true, + Event_Peserta: { + take: 4, + orderBy: { + createdAt: "desc", + }, + select: { + id: true, + userId: true, + User: { + select: { + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, }, }, }, @@ -194,86 +209,109 @@ async function GET(request: Request) { }, }, }, - - // User: { - // select: { - // id: true, - // username: true, - // Profile: { - // select: { - // id: true, - // name: true, - // imageId: true, - // }, - // }, - // }, - // }, - }, - }); + }), + prisma.event_Peserta.count({ + where: { + userId: userId, + }, + }) + ]); fixData = data; + totalCount = count; } else if (category === "all-history") { - const data = await prisma.event.findMany({ - orderBy: { - tanggal: "desc", - }, - where: { - eventMaster_StatusId: "1", - isArsip: true, - }, - select: { - id: true, - title: true, - tanggal: true, - deskripsi: true, - active: true, - authorId: true, - Author: { - select: { - id: true, - username: true, - Profile: true, + const [data, count] = await Promise.all([ + prisma.event.findMany({ + take: takeData, + skip: skipData, + orderBy: { + tanggal: "desc", + }, + where: { + eventMaster_StatusId: "1", + isArsip: true, + }, + select: { + id: true, + title: true, + tanggal: true, + deskripsi: true, + active: true, + authorId: true, + Author: { + select: { + id: true, + username: true, + Profile: true, + }, }, }, - }, - }); + }), + prisma.event.count({ + where: { + eventMaster_StatusId: "1", + isArsip: true, + }, + }) + ]); fixData = data; + totalCount = count; } else if (category === "my-history") { - const data = await prisma.event.findMany({ - orderBy: { - tanggal: "desc", - }, - where: { - authorId: userId, - eventMaster_StatusId: "1", - isArsip: true, - }, - select: { - id: true, - title: true, - tanggal: true, - deskripsi: true, - active: true, - authorId: true, - Author: { - select: { - id: true, - username: true, - Profile: true, + const [data, count] = await Promise.all([ + prisma.event.findMany({ + take: takeData, + skip: skipData, + orderBy: { + tanggal: "desc", + }, + where: { + authorId: userId, + eventMaster_StatusId: "1", + isArsip: true, + }, + select: { + id: true, + title: true, + tanggal: true, + deskripsi: true, + active: true, + authorId: true, + Author: { + select: { + id: true, + username: true, + Profile: true, + }, }, }, - }, - }); + }), + prisma.event.count({ + where: { + authorId: userId, + eventMaster_StatusId: "1", + isArsip: true, + }, + }) + ]); fixData = data; + totalCount = count; } + const totalPages = Math.ceil(totalCount / takeData); + return NextResponse.json( { success: true, message: "Success get event", data: fixData, + pagination: { + currentPage: page, + totalPages: totalPages, + totalData: totalCount, + dataPerPage: takeData, + }, }, { status: 200 } ); diff --git a/src/app/api/mobile/notification/[id]/route.ts b/src/app/api/mobile/notification/[id]/route.ts index 8844419d..e9af6ec6 100644 --- a/src/app/api/mobile/notification/[id]/route.ts +++ b/src/app/api/mobile/notification/[id]/route.ts @@ -6,20 +6,22 @@ import { adminMessaging } from "@/lib/firebase-admin"; export async function GET( request: NextRequest, - { params }: { params: { id: string } } + { params }: { params: { id: string } }, ) { const { id } = params; + console.log("ID", id); const { searchParams } = new URL(request.url); const category = searchParams.get("category"); - - let fixData; const fixCategory = _.upperCase(category || ""); - try { - const page = Number(searchParams.get("page")); - const takeData = 10; - const skipData = page ? page * takeData - takeData : 0; + const page = Number(searchParams.get("page")); + console.log("page", page); + const takeData = 10; + const skipData = page * takeData - takeData; + let fixData; + + try { const data = await prisma.notifikasi.findMany({ take: page ? takeData : undefined, skip: page ? skipData : undefined, @@ -69,14 +71,14 @@ export async function GET( } catch (error) { return NextResponse.json( { error: (error as Error).message }, - { status: 500 } + { status: 500 }, ); } } export async function PUT( request: NextRequest, - { params }: { params: { id: string } } + { params }: { params: { id: string } }, ) { const { id } = params; const { searchParams } = new URL(request.url); @@ -113,7 +115,7 @@ export async function PUT( console.error("Error marking notifications as read:", error); return NextResponse.json( { error: (error as Error).message }, - { status: 500 } + { status: 500 }, ); } } diff --git a/zCoba.js b/zCoba.js index cd3fd16f..30bcffc3 100644 --- a/zCoba.js +++ b/zCoba.js @@ -1,28 +1,26 @@ -// const data = [ -// { -// authorId: "clx8pl7r90005su4mldioo0v1", -// Donasi: { -// id: "clyr304q0000410ljvzms3mag", -// title: "Donasi Bencana Alam Aceh", -// }, -// }, -// { -// authorId: "clx8pl7r90005su4mldioo0v1", -// Donasi: { -// id: "clyr304q0000410ljvzms3mag", -// title: "Donasi Bencana Alam Aceh", -// }, -// }, -// { -// authorId: "clycka5eu0001ina3i1ssgze9", -// Donasi: { -// id: "clyr304q0000410ljvzms3mag", -// title: "Donasi Bencana Alam Aceh", -// }, -// }, -// ]; +const { PrismaClient } = require('@prisma/client') +const prisma = new PrismaClient() -console.error("errornya disini klik aja",import.meta.url); -console.log(new Set(data.map((d) => d.authorId))); +async function main() { + const result = await prisma.notifikasi.updateMany({ + where: { + recipientId: 'cmha7p6yc0000cfoe5w2e7gdr', + }, + data: { + isRead: false, + readAt: null, + }, + }) + console.log(`✅ Rows affected: ${result.count}`) +} + +main() + .catch((err) => { + console.error('❌ Error:', err) + process.exit(1) + }) + .finally(async () => { + await prisma.$disconnect() + })