fix admin event

deskripsi:
- fix server action to API
This commit is contained in:
2025-01-30 17:51:34 +08:00
parent 958a9e1be6
commit 5cc4728cad
5 changed files with 353 additions and 206 deletions

View File

@@ -0,0 +1,143 @@
import { prisma } from "@/app/lib";
import backendLogger from "@/util/backendLogger";
import _ from "lodash";
import { NextResponse } from "next/server";
export async function GET(
request: Request,
{ params }: { params: { status: string } }
) {
const method = request.method;
if (method !== "GET") {
return NextResponse.json(
{ success: false, message: "Method not allowed" },
{ status: 405 }
);
}
const { status } = params;
const { searchParams } = new URL(request.url);
const search = searchParams.get("search");
const page = searchParams.get("page");
const takeData = 1;
const skipData = Number(page) * takeData - takeData;
try {
let fixData;
const fixStatus = _.startCase(status);
if (!page && !search) {
fixData = await prisma.event.findMany({
orderBy: {
createdAt: "desc",
},
where: {
active: true,
isArsip: false,
EventMaster_Status: {
name: fixStatus,
},
},
});
} else if (!page && search) {
fixData = await prisma.event.findMany({
orderBy: {
createdAt: "desc",
},
where: {
active: true,
isArsip: false,
EventMaster_Status: {
name: fixStatus,
},
title: {
contains: search,
mode: "insensitive",
},
},
});
} else if (page && !search) {
const data = await prisma.event.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "desc",
},
where: {
active: true,
isArsip: false,
EventMaster_Status: {
name: fixStatus,
},
},
});
const nCount = await prisma.event.count({
where: {
active: true,
eventMaster_StatusId: "1",
isArsip: false,
},
});
fixData = {
data: data,
nPage: _.ceil(nCount / takeData),
};
} else if (page && search) {
const data = await prisma.event.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "desc",
},
where: {
active: true,
isArsip: false,
EventMaster_Status: {
name: fixStatus,
},
title: {
contains: search,
mode: "insensitive",
},
},
});
const nCount = await prisma.event.count({
where: {
active: true,
eventMaster_StatusId: "1",
isArsip: false,
title: {
contains: search,
mode: "insensitive",
},
},
});
fixData = {
data: data,
nPage: _.ceil(nCount / takeData),
};
}
return NextResponse.json({
success: true,
message: `Success get data table event ${status}`,
data: fixData,
});
} catch (error) {
backendLogger.error("Error get data table event dashboard >>", error);
return NextResponse.json(
{
success: false,
message: "Failed get data table event dashboard",
reason: (error as Error).message,
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -2,6 +2,7 @@ export {
apiGetEventStatusCountDashboard,
apiGetEventTipeAcara,
apiGetEventRiwayatCount,
apiGetDataEventByStatus,
};
const apiGetEventStatusCountDashboard = async ({
@@ -26,25 +27,25 @@ const apiGetEventStatusCountDashboard = async ({
};
const apiGetEventRiwayatCount = async () => {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const response = await fetch(`/api/admin/event/dashboard/riwayat`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
});
const response = await fetch(`/api/admin/event/dashboard/riwayat`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
});
return await response.json().catch(() => null);
}
return await response.json().catch(() => null);
};
const apiGetEventTipeAcara = async () => {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
if (!token) return await token.json().catch(() => null);
const response = await fetch(`/api/admin/event/dashboard/tipe-acara`, {
method: "GET",
@@ -54,7 +55,33 @@ const apiGetEventTipeAcara = async () => {
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
});
});
return await response.json().catch(() => null);
};
const apiGetDataEventByStatus = async ({
status,
page,
search,
}: {
status: "Publish" | "Review" | "Reject";
page: string;
search: string;
}) => {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) return await token.json().catch(() => null);
const isPage = page ? `?page=${page}` : "";
const isSearch = search ? `&search=${search}` : "";
const respone = await fetch(`/api/admin/event/${status}${isPage}${isSearch}`, {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
Authorization: `Bearer ${token}`,
},
});
return await respone.json().catch(() => null);
};

View File

@@ -2,11 +2,9 @@ import { AdminEvent_TablePublish } from "@/app_modules/admin/event";
import { adminEvent_funGetListPublish } from "@/app_modules/admin/event/fun";
async function Page() {
const listPublish = await adminEvent_funGetListPublish({ page: 1 });
return (
<>
<AdminEvent_TablePublish listPublish={listPublish as any} />
<AdminEvent_TablePublish />
</>
);
}