Merge pull request #306 from bipproduction/bagas/12-feb-25

fix API portofolio
This commit is contained in:
Bagasbanuna02
2025-02-12 17:41:25 +08:00
committed by GitHub
18 changed files with 462 additions and 567 deletions

View File

@@ -1,71 +0,0 @@
// app/api/logs/view/route.ts
import { NextRequest, NextResponse } from "next/server";
import fs from "fs/promises";
import path from "path";
interface LogEntry {
timestamp: string;
level: string;
message: string;
metadata?: any;
}
async function readLogFiles(directory: string): Promise<LogEntry[]> {
try {
const logPath = path.join(process.cwd(), directory);
const files = await fs.readdir(logPath);
const logFiles = files.filter((file) => file.endsWith(".log"));
const allLogs: LogEntry[] = [];
for (const file of logFiles) {
const filePath = path.join(logPath, file);
const content = await fs.readFile(filePath, "utf-8");
// Parse setiap baris log
const logs = content
.split("\n")
.filter(Boolean)
.map((line) => {
try {
return JSON.parse(line);
} catch (e) {
return null;
}
})
.filter(Boolean);
allLogs.push(...logs);
}
// Sort berdasarkan timestamp, terbaru di atas
return allLogs.sort(
(a, b) =>
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
);
} catch (error) {
console.error("Error reading log files:", error);
return [];
}
}
export async function GET(request: NextRequest) {
try {
// Baca logs dari frontend dan backend
const frontendLogs = await readLogFiles("logs/frontend");
const backendLogs = await readLogFiles("logs/backend");
// Gabungkan dan sort semua logs
const allLogs = [...frontendLogs, ...backendLogs].sort(
(a, b) =>
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
);
return NextResponse.json({ logs: allLogs });
} catch (error) {
return NextResponse.json(
{ error: "Failed to fetch logs" },
{ status: 500 }
);
}
}

View File

@@ -1,7 +1,131 @@
import { prisma } from "@/lib";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export { POST };
export { GET, POST, PUT };
async function GET(request: Request, { params }: { params: { id: string } }) {
try {
const { id } = params;
const data = await prisma.portofolio.findUnique({
where: {
id: id,
},
include: {
MasterBidangBisnis: {
select: {
id: true,
name: true,
active: true,
},
},
Portofolio_MediaSosial: true,
Profile: {
select: {
userId: true,
User: {
select: {
id: true,
},
},
},
},
BusinessMaps: {
include: {
Author: true,
},
},
},
});
if (!data)
return NextResponse.json(
{
success: false,
message: "Data tidak ditemukan",
},
{ status: 404 }
);
return NextResponse.json(
{
success: true,
message: "Berhasil mendapatkan data",
data: data,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error("API Error Get Data Portofolio", error);
return NextResponse.json(
{
success: false,
message: "API Error Get Data Potofolio",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}
async function PUT(request: Request, { params }: { params: { id: string } }) {
if (request.method !== "PUT") {
return NextResponse.json(
{
success: false,
message: "Method not allowed",
},
{ status: 405 }
);
}
try {
const { id } = params;
const { data } = await request.json();
const udpateData = await prisma.portofolio.update({
where: {
id: id,
},
data: {
namaBisnis: data.namaBisnis,
alamatKantor: data.alamatKantor,
tlpn: data.tlpn,
deskripsi: data.deskripsi,
masterBidangBisnisId: data.masterBidangBisnisId,
},
});
if (!udpateData)
return NextResponse.json(
{
success: false,
message: "Gagal update data",
},
{ status: 400 }
);
return NextResponse.json(
{
success: true,
message: "Berhasil mendapatkan data",
data: udpateData,
},
{ status: 200 }
);
} catch (error) {
return NextResponse.json(
{
success: false,
message: "Error update data",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}
async function POST(request: Request, { params }: { params: { id: string } }) {
if (request.method !== "POST") {