From 836d12bf2e932a30376ea520146c593c6b1eb046 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Mon, 15 Sep 2025 17:22:37 +0800 Subject: [PATCH] API Mobile Event Add: - api/mobile/event/[id]/check-participants : untuk cek keikut sertaan peserta Fix: - api/mobile/event/route : tambah get berdasarkan kategory - mobile/event/[id]/participants/route: clear code ### No Issue --- .../event/[id]/check-participants/route.ts | 48 ++++ .../mobile/event/[id]/participants/route.ts | 7 +- src/app/api/mobile/event/route.ts | 244 +++++++++++++----- 3 files changed, 234 insertions(+), 65 deletions(-) create mode 100644 src/app/api/mobile/event/[id]/check-participants/route.ts diff --git a/src/app/api/mobile/event/[id]/check-participants/route.ts b/src/app/api/mobile/event/[id]/check-participants/route.ts new file mode 100644 index 00000000..3121be58 --- /dev/null +++ b/src/app/api/mobile/event/[id]/check-participants/route.ts @@ -0,0 +1,48 @@ +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export {GET} + +async function GET(request: Request, { params }: { params: { id: string } }) { + try { + const { id } = params; + const { searchParams } = new URL(request.url); + const userId = searchParams.get("userId"); + + let fixData + + const checkParticipant = await prisma.event_Peserta.findFirst({ + where: { + userId: userId as any, + eventId: id, + }, + }); + + if (checkParticipant !== null) { + fixData = true; + } else { + fixData = false; + } + + return NextResponse.json( + { + success: true, + message: "Success get participants", + data: fixData, + }, + { status: 200 } + ); + + } catch (error) { + return NextResponse.json( + { + success: false, + message: "Error get participants", + reason: (error as Error).message, + }, + { status: 500 } + ); + + } + +} \ No newline at end of file diff --git a/src/app/api/mobile/event/[id]/participants/route.ts b/src/app/api/mobile/event/[id]/participants/route.ts index 7438df10..062a0ab5 100644 --- a/src/app/api/mobile/event/[id]/participants/route.ts +++ b/src/app/api/mobile/event/[id]/participants/route.ts @@ -1,14 +1,13 @@ -import { NextResponse } from "next/server"; import prisma from "@/lib/prisma"; +import { NextResponse } from "next/server"; -export { POST , GET}; +export { GET, POST }; async function POST(request: Request, { params }: { params: { id: string } }) { try { const { id } = params; const { userId } = await request.json(); - const createJoin = await prisma.event_Peserta.create({ data: { eventId: id, @@ -49,6 +48,7 @@ async function POST(request: Request, { params }: { params: { id: string } }) { async function GET(request: Request, { params }: { params: { id: string } }) { try { const { id } = params; + const data = await prisma.event_Peserta.findMany({ where: { eventId: id, @@ -63,6 +63,7 @@ async function GET(request: Request, { params }: { params: { id: string } }) { username: true, Profile: { select: { + id: true, name: true, imageId: true, }, diff --git a/src/app/api/mobile/event/route.ts b/src/app/api/mobile/event/route.ts index a81d0a7d..3046e0a9 100644 --- a/src/app/api/mobile/event/route.ts +++ b/src/app/api/mobile/event/route.ts @@ -1,9 +1,9 @@ -import _ from "lodash"; -import { NextResponse } from "next/server"; import prisma from "@/lib/prisma"; +import _ from "lodash"; import moment from "moment"; +import { NextResponse } from "next/server"; -export { POST, GET }; +export { GET, POST }; async function POST(request: Request) { try { @@ -52,74 +52,194 @@ async function POST(request: Request) { async function GET(request: Request) { try { - const allData = await prisma.event.findMany({ - where: { - active: true, - eventMaster_StatusId: "1", - isArsip: false, - }, - }); + const { searchParams } = new URL(request.url); + const category = searchParams.get("category"); + const userId = searchParams.get("userId"); - for (let i of allData) { - if (moment(i.tanggalSelesai).diff(moment(), "minutes") < 0) { - const updateArsip = await prisma.event.update({ - where: { - id: i.id, - }, - data: { - isArsip: true, - }, - }); + console.log("[CAT]", category); + console.log("[USER]", userId); - if (!updateArsip) { - console.log("gagal update arsip"); - return []; + let fixData; + + if (category === "beranda") { + const allData = await prisma.event.findMany({ + where: { + active: true, + eventMaster_StatusId: "1", + isArsip: false, + }, + }); + + for (let i of allData) { + if (moment(i.tanggalSelesai).diff(moment(), "minutes") < 0) { + const updateArsip = await prisma.event.update({ + where: { + id: i.id, + }, + data: { + isArsip: true, + }, + }); + + if (!updateArsip) { + console.log("gagal update arsip"); + return []; + } } } + + // const takeData = 10; + // const skipData = page * takeData - takeData; + + const data = await prisma.event.findMany({ + // take: takeData, + // skip: skipData, + orderBy: [ + { + tanggal: "asc", + }, + ], + where: { + active: true, + eventMaster_StatusId: "1", + isArsip: false, + }, + select: { + id: true, + title: true, + deskripsi: true, + tanggal: true, + tanggalSelesai: true, + EventMaster_Status: { + select: { + name: true, + }, + }, + authorId: true, + Author: { + include: { + Profile: true, + }, + }, + }, + }); + + fixData = data; + } 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, + Event_Peserta: { + take: 4, + orderBy: { + createdAt: "desc", + }, + select: { + id: true, + userId: true, + User: { + select: { + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }, + }, + }, + + User: { + select: { + id: true, + username: true, + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, + }, + }, + }, + }); + + fixData = data; + } else if (category === "all-history") { + fixData = 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, + }, + }, + }, + }); + } else if (category === "my-history") { + fixData = 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 takeData = 10; - // const skipData = page * takeData - takeData; - - const data = await prisma.event.findMany({ - // take: takeData, - // skip: skipData, - - orderBy: [ - { - tanggal: "asc", - }, - ], - where: { - active: true, - eventMaster_StatusId: "1", - isArsip: false, - }, - select: { - id: true, - title: true, - deskripsi: true, - tanggal: true, - tanggalSelesai: true, - EventMaster_Status: { - select: { - name: true, - }, - }, - authorId: true, - Author: { - include: { - Profile: true, - }, - }, - }, - }); - return NextResponse.json( { success: true, - message: "Success get event", - data: data, + message: + category === "" + ? "Success get event, but category is empty" + : "Success get event", + data: fixData, }, { status: 200 } );