Mobile API: Donasi
Add: - /api/mobile/donation/[id]/[status] Fix: - api/mobile/donation/[id]/route.ts - api/mobile/master/donation/route.ts - api/mobile/donation/route.ts ### No Issue
This commit is contained in:
130
src/app/api/mobile/donation/[id]/[status]/route.ts
Normal file
130
src/app/api/mobile/donation/[id]/[status]/route.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export { GET, PUT };
|
||||
|
||||
async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string; status: string } }
|
||||
) {
|
||||
const { id, status } = params;
|
||||
const fixStatus = _.startCase(status);
|
||||
|
||||
let fixData;
|
||||
try {
|
||||
const checkStatus = await prisma.donasiMaster_StatusDonasi.findFirst({
|
||||
where: {
|
||||
name: fixStatus,
|
||||
},
|
||||
});
|
||||
|
||||
console.log("[CHECK STATUS]", checkStatus);
|
||||
|
||||
if (!checkStatus)
|
||||
return NextResponse.json({
|
||||
status: 400,
|
||||
success: false,
|
||||
reason: "Gagal mendapatkan data",
|
||||
});
|
||||
|
||||
const res = await prisma.donasi.findMany({
|
||||
where: {
|
||||
authorId: id,
|
||||
donasiMaster_StatusDonasiId: checkStatus.id,
|
||||
active: true,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
imagesId: true,
|
||||
target: true,
|
||||
progres: true,
|
||||
publishTime: true,
|
||||
DonasiMaster_Durasi: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
terkumpul: true,
|
||||
imageId: true,
|
||||
},
|
||||
orderBy: {
|
||||
updatedAt: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
fixData = res.map((v: any) => ({
|
||||
..._.omit(v, ["DonasiMaster_Durasi"]),
|
||||
nameDonasiDurasi: v.DonasiMaster_Durasi.name,
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
status: 200,
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
data: fixData,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("[ERROR]", error);
|
||||
return NextResponse.json({
|
||||
status: 500,
|
||||
success: false,
|
||||
message: "Error mendapatkan data",
|
||||
reason: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function PUT(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string; status: string } }
|
||||
) {
|
||||
const { id, status } = params;
|
||||
const fixStatus = _.startCase(status);
|
||||
|
||||
try {
|
||||
const checkStatus = await prisma.donasiMaster_StatusDonasi.findFirst({
|
||||
where: {
|
||||
name: fixStatus,
|
||||
},
|
||||
});
|
||||
|
||||
if (!checkStatus)
|
||||
return NextResponse.json({
|
||||
status: 400,
|
||||
success: false,
|
||||
reason: "Gagal mendapatkan data",
|
||||
});
|
||||
|
||||
const res = await prisma.donasi.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
donasiMaster_StatusDonasiId: checkStatus.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!res)
|
||||
return NextResponse.json({
|
||||
status: 400,
|
||||
success: false,
|
||||
reason: "Gagal mengubah status",
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
status: 200,
|
||||
success: true,
|
||||
message: "Berhasil mengubah status",
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("[ERROR]", error);
|
||||
return NextResponse.json({
|
||||
status: 500,
|
||||
success: false,
|
||||
message: "Error mendapatkan data",
|
||||
reason: (error as Error).message,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user