Files
hipmi/src/app/api/mobile/investment/[id]/invoice/route.ts
bagasbanuna 2c269db250 API – Investment (Admin & User)
- src/app/api/mobile/admin/investment/[id]/route.ts
- src/app/api/mobile/admin/investment/[id]/invoice/route.ts
- src/app/api/mobile/investment/[id]/invoice/route.ts
- src/app/api/mobile/investment/[id]/document/route.ts

Routing Helper
- src/lib/mobile/route-page-mobile.ts

### No Issue
2026-01-21 15:34:19 +08:00

267 lines
6.8 KiB
TypeScript

import { prisma } from "@/lib";
import { sendNotificationMobileToManyUser } from "@/lib/mobile/notification/send-notification";
import { routeAdminMobile } from "@/lib/mobile/route-page-mobile";
import _ from "lodash";
import { NextResponse } from "next/server";
import {
NotificationMobileTitleType,
NotificationMobileBodyType,
} from "../../../../../../../types/type-mobile-notification";
export { POST, GET, PUT };
async function POST(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
const { data } = await request.json();
try {
console.log("[ID INVOICE]", id);
console.log("[DATA INVOICE]", data);
const create = await prisma.investasi_Invoice.create({
data: {
investasiId: id,
authorId: data.authorId,
nominal: "" + data.total,
lembarTerbeli: "" + data.jumlah,
masterBankId: data.bankId,
statusInvoiceId: "3",
},
});
console.log("[CREATE INVOICE]", create);
return NextResponse.json({
status: 201,
success: true,
message: "Berhasil membuat invoice",
data: create,
});
} catch (error) {
console.error("[ERROR CREATE INVOICE]", error);
return NextResponse.json({
status: 500,
success: false,
message: "Gagal membuat invoice",
reason: (error as Error).message,
});
}
}
async function GET(request: Request, { params }: { params: { id: string } }) {
const { id } = params; // << AUTHOR ID
const { searchParams } = new URL(request.url);
const category = searchParams.get("category");
const authorId = searchParams.get("authorId");
console.log("[ID INVOICE]", id);
let fixData;
try {
if (category === "invoice") {
const data = await prisma.investasi_Invoice.findFirst({
where: {
id: id,
},
include: {
MasterBank: true,
StatusInvoice: true,
Investasi: {
include: {
MasterPembagianDeviden: true,
MasterPencarianInvestor: true,
MasterPeriodeDeviden: true,
ProspektusInvestasi: true,
Investasi_Invoice: {
where: {
statusInvoiceId: "1",
},
},
},
},
Author: {
include: {
Profile: true,
},
},
},
});
console.log("[DATA INVOICE]", data ? true : false);
const { ...allData } = data;
const Investor = data?.Investasi?.Investasi_Invoice;
fixData = { ...allData, Investor };
} else if (category == "my-invest") {
const data = await prisma.investasi_Invoice.findMany({
orderBy: {
updatedAt: "desc",
},
where: {
authorId: authorId,
statusInvoiceId: "1",
isActive: true,
},
select: {
id: true,
nominal: true,
lembarTerbeli: true,
Investasi: {
select: {
title: true,
progress: true,
},
},
},
});
fixData = data.map((v: any) => ({
..._.omit(v, ["Investasi"]),
title: v.Investasi.title,
progress: v.Investasi.progress,
}));
} else if (category == "transaction") {
const data = await prisma.investasi_Invoice.findMany({
orderBy: {
updatedAt: "desc",
},
where: {
authorId: authorId,
},
select: {
id: true,
statusInvoiceId: true,
nominal: true,
createdAt: true,
StatusInvoice: {
select: {
name: true,
},
},
Investasi: {
select: {
title: true,
},
},
},
});
fixData = data.map((v: any) => ({
..._.omit(v, ["Investasi", "StatusInvoice"]),
title: v.Investasi.title,
statusInvoice: v.StatusInvoice.name,
}));
}
return NextResponse.json({
status: 200,
success: true,
message: `Berhasil mendapatkan data ${category}`,
data: fixData,
});
} catch (error) {
console.error(`Error get ${category}`, error);
return NextResponse.json({
status: 500,
success: false,
message: `Gagal mendapatkan data ${category}`,
reason: (error as Error).message,
});
}
}
async function PUT(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
const { data } = await request.json();
const { searchParams } = new URL(request.url);
const status = searchParams.get("status");
const fixStatus = _.startCase(status || "");
console.log("[FIX STATUS]", fixStatus);
console.log("[DATA]", data);
console.log("[ID]", id);
try {
const checkStatus = await prisma.investasiMaster_StatusInvoice.findFirst({
where: {
name: fixStatus,
},
});
console.log("[CHECK STATUS]", checkStatus);
if (!checkStatus) {
throw new Error("Status invoice tidak ditemukan");
}
const update = await prisma.investasi_Invoice.update({
where: {
id: id,
},
data: {
statusInvoiceId: checkStatus.id,
imageId: data.imageId,
},
select: {
investasiId: true,
},
});
if (fixStatus === "Proses") {
// kirim notif ke author investasi
const findInvestasi = await prisma.investasi.findUnique({
where: {
id: update.investasiId as any,
},
select: {
title: true,
authorId: true,
},
});
const findUsers = await prisma.user.findMany({
where: {
masterUserRoleId: "2",
active: true,
NOT: { id: findInvestasi?.authorId as any },
},
select: { id: true },
});
// SEND NOTIFICATION
await sendNotificationMobileToManyUser({
recipientIds: findUsers.map((user) => user.id),
senderId: data.authorId,
payload: {
title: "Ada Investor Baru !" as NotificationMobileTitleType,
body: `Cek data investor pada ${findInvestasi?.title}` as NotificationMobileBodyType,
type: "announcement",
kategoriApp: "INVESTASI",
deepLink: routeAdminMobile.investmentDetailPublish({
id: update.investasiId as string,
status: "publish",
}),
},
});
}
console.log("[UPDATE]", update);
return NextResponse.json({
status: 200,
success: true,
message: "Berhasil mengubah status invoice",
// data: update,
});
} catch (error) {
console.error("Error update invoice", error);
return NextResponse.json({
status: 500,
success: false,
message: "Gagal mengubah status invoice",
reason: (error as Error).message,
});
}
}