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
This commit is contained in:
2025-09-15 17:22:37 +08:00
parent c89ad88d2d
commit 836d12bf2e
3 changed files with 234 additions and 65 deletions

View File

@@ -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 }
);