From e7530a093b6d650abbec81d6fff80c1a690b735c Mon Sep 17 00:00:00 2001 From: nabillah Date: Thu, 9 Oct 2025 16:46:30 +0800 Subject: [PATCH] Donation Mobile API Fix: - api/mobile/donation/[id]/[status]/route.ts - api/mobile/donation/[id]/invoice/route.ts - api/mobile/donation/[id]/news/route.ts - api/mobile/donation/route.ts ### No Issue --- .../mobile/donation/[id]/[status]/route.ts | 2 - .../api/mobile/donation/[id]/invoice/route.ts | 8 -- .../api/mobile/donation/[id]/news/route.ts | 115 ++++++++++++++++-- src/app/api/mobile/donation/route.ts | 3 - 4 files changed, 107 insertions(+), 21 deletions(-) diff --git a/src/app/api/mobile/donation/[id]/[status]/route.ts b/src/app/api/mobile/donation/[id]/[status]/route.ts index 9b67d81a..94fd76f3 100644 --- a/src/app/api/mobile/donation/[id]/[status]/route.ts +++ b/src/app/api/mobile/donation/[id]/[status]/route.ts @@ -19,8 +19,6 @@ async function GET( }, }); - console.log("[CHECK STATUS]", checkStatus); - if (!checkStatus) return NextResponse.json({ status: 400, diff --git a/src/app/api/mobile/donation/[id]/invoice/route.ts b/src/app/api/mobile/donation/[id]/invoice/route.ts index 548edcc2..b9be4fbb 100644 --- a/src/app/api/mobile/donation/[id]/invoice/route.ts +++ b/src/app/api/mobile/donation/[id]/invoice/route.ts @@ -7,8 +7,6 @@ export { POST, GET, PUT }; async function POST(request: Request, { params }: { params: { id: string } }) { const { id } = params; const { data } = await request.json(); - console.log("[ID]", id); - console.log("[DATA]", data); try { const create = await prisma.donasi_Invoice.create({ @@ -126,10 +124,6 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { const status = searchParams.get("status"); const fixStatus = _.startCase(status as string); - console.log("[ID]", id); - console.log("[DATA]", data); - console.log("[STATUS]", fixStatus); - try { const checkStatus = await prisma.donasiMaster_StatusInvoice.findFirst({ where: { @@ -168,8 +162,6 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { }, }); - console.log("[UPDATE]", update); - return NextResponse.json({ status: 200, success: true, diff --git a/src/app/api/mobile/donation/[id]/news/route.ts b/src/app/api/mobile/donation/[id]/news/route.ts index 60c3928f..94f8b5d9 100644 --- a/src/app/api/mobile/donation/[id]/news/route.ts +++ b/src/app/api/mobile/donation/[id]/news/route.ts @@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib"; import _ from "lodash"; -export { POST, GET }; +export { POST, GET, PUT, DELETE }; async function POST( request: NextRequest, @@ -10,8 +10,7 @@ async function POST( ) { const { id } = params; const { data } = await request.json(); - console.log("[ID]", id); - console.log("[DATA]", data); + try { if (data && data?.imageId) { @@ -35,8 +34,6 @@ async function POST( }, }); - console.log("[CREATE]", create); - if (!create) return NextResponse.json({ status: 400, message: "Gagal disimpan" }); } @@ -66,12 +63,12 @@ async function GET( const category = searchParams.get("category"); let fixData; - console.log("[CATEGORY]", category); - console.log("[ID]", id); - try { if (category === "get-all") { fixData = await prisma.donasi_Kabar.findMany({ + orderBy: { + updatedAt: "desc", + }, where: { donasiId: id, active: true, @@ -121,3 +118,105 @@ async function GET( }); } } + +async function PUT(request: Request, { params }: { params: { id: string } }) { + const { id } = params; + const { data } = await request.json(); + + try { + if (data && data.newImageId) { + const updateWithImage = await prisma.donasi_Kabar.update({ + where: { + id: id, + }, + data: { + title: data.title, + deskripsi: data.deskripsi, + imageId: data.newImageId, + }, + }); + + if (!updateWithImage) + return NextResponse.json({ + status: 400, + success: false, + message: "Gagal Update", + }); + } else { + const updateData = await prisma.donasi_Kabar.update({ + where: { + id: id, + }, + data: { + title: data.title, + deskripsi: data.deskripsi, + }, + }); + + if (!updateData) + return NextResponse.json({ + status: 400, + success: false, + message: "Gagal Update", + }); + } + return NextResponse.json({ + status: 200, + success: true, + message: "Berhasil Update", + }); + } catch (error) { + console.error("[ERROR UPDATE NEWS]", error); + return NextResponse.json({ + status: 500, + success: false, + message: "Error Update Donation News", + reason: (error as Error).message, + }); + } +} + +async function DELETE( + request: Request, + { params }: { params: { id: string } } +) { + const { id } = params; + try { + const deleteData = await prisma.donasi_Kabar.delete({ + where: { + id: id, + }, + select: { + imageId: true, + }, + }); + + const deleteImage = await fetch( + `https://wibu-storage.wibudev.com/api/files/${deleteData?.imageId}/delete`, + { + method: "DELETE", + headers: { + Authorization: `Bearer ${process.env.WS_APIKEY}`, + }, + } + ); + + if (!deleteImage) { + console.log("[FAILED DELETE IMAGE]", deleteImage); + } + + return NextResponse.json({ + status: 200, + success: true, + message: "Berhasil Delete", + }); + } catch (error) { + console.error("[ERROR DELETE NEWS]", error); + return NextResponse.json({ + status: 500, + success: false, + message: "Error Delete Donation News", + reason: (error as Error).message, + }); + } +} diff --git a/src/app/api/mobile/donation/route.ts b/src/app/api/mobile/donation/route.ts index 7a7a631e..5aa9c781 100644 --- a/src/app/api/mobile/donation/route.ts +++ b/src/app/api/mobile/donation/route.ts @@ -100,9 +100,6 @@ export async function GET(request: Request) { const authorId = searchParams.get("authorId"); let fixData; - console.log("[CATEGORY]", category); - console.log("[AUTHOR ID]", authorId); - try { if (category === "beranda") { const data = await prisma.donasi.findMany({