fix investasi api

This commit is contained in:
2025-02-28 13:55:56 +08:00
parent 88a8bff917
commit f7a5395ca9
4 changed files with 219 additions and 19 deletions

View File

@@ -1,21 +1,182 @@
import backendLogger from "@/util/backendLogger";
import { prisma } from "@/lib";
import _ from "lodash";
import moment from "moment";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export { GET };
async function GET(request: Request) {
// GET ALL DATA INVESTASI
export async function GET(request: Request) {
try {
return NextResponse.json({
success: true,
message: "Get data invetsasi",
data: "",
});
let dataFix;
const { searchParams } = new URL(request.url);
const kategori = searchParams.get("cat");
const status = searchParams.get("status");
const page = searchParams.get("page");
const dataTake = 10;
const dataSkip = Number(page) * dataTake - dataTake;
if (!page) {
const data = await prisma.investasi.findMany({
where: {
masterStatusInvestasiId: "1",
masterProgresInvestasiId: "1",
},
select: {
id: true,
MasterPencarianInvestor: true,
countDown: true,
progress: true,
},
});
for (let a of data) {
if (
(a.MasterPencarianInvestor?.name as any) -
moment(new Date()).diff(new Date(a.countDown as any), "days") <=
0
) {
await prisma.investasi.update({
where: {
id: a.id,
},
data: {
masterProgresInvestasiId: "3",
},
});
}
if (a.progress === "100") {
await prisma.investasi.update({
where: {
id: a.id,
},
data: {
masterProgresInvestasiId: "2",
},
});
}
}
// cek data yang lewat
// klo ada, update status
const dataAwal = await prisma.investasi.findMany({
orderBy: [
{
masterProgresInvestasiId: "asc",
},
{
countDown: "desc",
},
],
where: {
masterStatusInvestasiId: "1",
},
select: {
id: true,
imageId: true,
title: true,
progress: true,
countDown: true,
MasterPencarianInvestor: {
select: {
name: true,
},
},
},
});
dataFix = dataAwal.map((v: any) => ({
..._.omit(v, ["MasterPencarianInvestor"]),
pencarianInvestor: v.MasterPencarianInvestor.name,
}));
} else {
const data = await prisma.investasi.findMany({
where: {
masterStatusInvestasiId: "1",
masterProgresInvestasiId: "1",
},
select: {
id: true,
MasterPencarianInvestor: true,
countDown: true,
progress: true,
},
});
for (let a of data) {
if (
(a.MasterPencarianInvestor?.name as any) -
moment(new Date()).diff(new Date(a.countDown as any), "days") <=
0
) {
await prisma.investasi.update({
where: {
id: a.id,
},
data: {
masterProgresInvestasiId: "3",
},
});
}
if (a.progress === "100") {
await prisma.investasi.update({
where: {
id: a.id,
},
data: {
masterProgresInvestasiId: "2",
},
});
}
}
// cek data yang lewat
// klo ada, update status
const dataAwal = await prisma.investasi.findMany({
take: dataTake,
skip: dataSkip,
orderBy: [
{
masterProgresInvestasiId: "asc",
},
{
countDown: "desc",
},
],
where: {
masterStatusInvestasiId: "1",
},
select: {
id: true,
imageId: true,
title: true,
progress: true,
countDown: true,
MasterPencarianInvestor: {
select: {
name: true,
},
},
},
});
dataFix = dataAwal.map((v: any) => ({
..._.omit(v, ["MasterPencarianInvestor"]),
pencarianInvestor: v.MasterPencarianInvestor.name,
}));
}
return NextResponse.json(
{ success: true, message: "Berhasil mendapatkan data", data: dataFix },
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error get data investasi", error);
console.error(error);
return NextResponse.json(
{
success: false,
message: "Error get data from API ",
message: "Gagal mendapatkan data, coba lagi nanti ",
reason: (error as Error).message,
},
{ status: 500 }

View File

@@ -13,7 +13,9 @@ export function Investasi_ComponentButtonUpdateBeranda({
async function onLoaded() {
try {
setLoading(true);
const response = await apiGetAllInvestasi(`?cat=bursa&page=1`);
const response = await apiFetchGetAllInvestasi({
page: "1",
});
if (response.success) {
onLoadData(response.data);
}

View File

@@ -0,0 +1,33 @@
const apiFetchGetAllInvestasi = async ({ page }: { page: string }) => {
try {
// Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const nextPage = `?page=${page}`;
const response = await fetch(`/api/investasi${nextPage}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error("Failed to get all forum:", response.statusText, errorData);
throw new Error(errorData?.message || "Failed to get all forum");
}
// Return the JSON response
return await response.json();
} catch (error) {
console.error("Error get all forum", error);
throw error; // Re-throw the error to handle it in the calling function
}
};

View File

@@ -38,13 +38,15 @@ export function Investasi_ViewBerandaNew() {
useShallowEffect(() => {
setIsTriggerReload(false);
setIsShowUpdate(false);
getDataInvestasi();
handleLoadData();
}, []);
async function getDataInvestasi() {
const handleLoadData = async () => {
try {
setLoading(true);
const response = await apiGetAllInvestasi(`?cat=bursa&page=1`);
const response = await apiFetchGetAllInvestasi({
page: `${activePage}`,
});
if (response.success) {
setData(response.data);
}
@@ -53,14 +55,16 @@ export function Investasi_ViewBerandaNew() {
} finally {
setLoading(false);
}
}
};
const handleMoreData = async () => {
try {
const pageNew = activePage + 1;
const loadData = await apiGetAllInvestasi(`?cat=bursa&page=${pageNew}`);
const nextPage = activePage + 1;
const loadData = await apiFetchGetAllInvestasi({
page: `${nextPage}`,
});
if (loadData.success) {
setActivePage(pageNew);
setActivePage(nextPage);
return loadData.data;
} else {
return [];