diff --git a/src/app/api/mobile/admin/investment/[id]/route.ts b/src/app/api/mobile/admin/investment/[id]/route.ts index 6f98a478..2b28cc11 100644 --- a/src/app/api/mobile/admin/investment/[id]/route.ts +++ b/src/app/api/mobile/admin/investment/[id]/route.ts @@ -1,11 +1,20 @@ import { NextResponse } from "next/server"; import { prisma } from "@/lib"; +import { + sendNotificationMobileToManyUser, + sendNotificationMobileToOneUser, +} from "@/lib/mobile/notification/send-notification"; +import { routeUserMobile } from "@/lib/mobile/route-page-mobile"; +import { + NotificationMobileBodyType, + NotificationMobileTitleType, +} from "../../../../../../../types/type-mobile-notification"; export { GET, PUT }; async function GET(request: Request, { params }: { params: { id: string } }) { const { id } = params; - + try { const data = await prisma.investasi.findUnique({ where: { @@ -78,14 +87,19 @@ async function GET(request: Request, { params }: { params: { id: string } }) { async function PUT(request: Request, { params }: { params: { id: string } }) { const { id } = params; const { data } = await request.json(); + const { catatan, senderId } = data; + + console.log("[DATA]", data); + console.log("[CATATAN]", catatan); + console.log("[SENDER ID]", senderId); + const { searchParams } = new URL(request.url); const status = searchParams.get("status"); - console.log("[=======Start Investment console=======]"); - console.log("[ID]", id); - console.log("[DATA]", data); - console.log("[STATUS]", status); - console.log("[=======End Investment console=======]"); + // console.log("[=======Start Investment console=======]"); + // console.log("[ID]", id); + // console.log("[STATUS]", status); + // console.log("[=======End Investment console=======]"); const publishTime = new Date(); @@ -96,9 +110,26 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { id: id, }, data: { - catatan: data, + catatan: catatan, masterStatusInvestasiId: "4", }, + select: { + authorId: true, + title: true, + }, + }); + + // SEND NOTIFICATION + await sendNotificationMobileToOneUser({ + recipientId: updatedData.authorId as any, + senderId: senderId, + payload: { + title: "Pengajuan Review Ditolak", + body: "Mohon perbaiki data sesuai catatan penolakan !", + type: "announcement", + kategoriApp: "INVESTASI", + deepLink: routeUserMobile.investmentByStatus({ status: "reject" }), + }, }); console.log("[UPDATE REJECT]", updatedData); @@ -114,6 +145,41 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { }, }); + // SEND NOTIFICAtION + await sendNotificationMobileToOneUser({ + recipientId: updatedData.authorId as any, + senderId: senderId, + payload: { + title: "Review Selesai", + body: ` + Investasi kamu telah dipublikasikan !\n + ${updatedData.title}` as NotificationMobileBodyType, + type: "announcement", + kategoriApp: "INVESTASI", + deepLink: routeUserMobile.investmentByStatus({ status: "publish" }), + }, + }); + + const allUsers = await prisma.user.findMany({ + where: { + NOT: { id: updatedData.authorId as any }, + active: true, + }, + select: { id: true }, + }); + + await sendNotificationMobileToManyUser({ + recipientIds: allUsers.map((user) => user.id), + senderId: senderId, + payload: { + title: "Ayo Cek Investasi Terbaru" as NotificationMobileTitleType, + body: `${updatedData.title}` as NotificationMobileBodyType, + type: "announcement", + kategoriApp: "INVESTASI", + deepLink: routeUserMobile.investasiDetailPublish({ id: id }), + }, + }); + console.log("[UPDATE PUBLISH]", updatedData); } diff --git a/src/app/api/mobile/investment/[id]/invoice/route.ts b/src/app/api/mobile/investment/[id]/invoice/route.ts index d9e08ccb..c8b9c027 100644 --- a/src/app/api/mobile/investment/[id]/invoice/route.ts +++ b/src/app/api/mobile/investment/[id]/invoice/route.ts @@ -1,6 +1,12 @@ 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 }; @@ -49,7 +55,6 @@ async function GET(request: Request, { params }: { params: { id: string } }) { const authorId = searchParams.get("authorId"); console.log("[ID INVOICE]", id); - let fixData; @@ -198,8 +203,45 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { 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 }, + }); + + 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.investmentByStatus({ status: "publish" }), + }, + }); + } + console.log("[UPDATE]", update); return NextResponse.json({ diff --git a/src/app/api/mobile/investment/route.ts b/src/app/api/mobile/investment/route.ts index bc3f26bd..5c519681 100644 --- a/src/app/api/mobile/investment/route.ts +++ b/src/app/api/mobile/investment/route.ts @@ -2,6 +2,9 @@ import _ from "lodash"; import { NextResponse } from "next/server"; import prisma from "@/lib/prisma"; import moment from "moment"; +import { sendNotificationMobileToManyUser } from "@/lib/mobile/notification/send-notification"; +import { NotificationMobileBodyType } from "../../../../../types/type-mobile-notification"; +import { routeAdminMobile } from "@/lib/mobile/route-page-mobile"; export { POST, GET }; @@ -9,12 +12,14 @@ async function POST(request: Request) { const { data } = await request.json(); console.log(["DATA INVESTASI"], data); + const fixTitle = _.startCase(data.title) + try { const create = await prisma.investasi.create({ data: { masterStatusInvestasiId: "2", authorId: data.authorId, - title: _.startCase(data.title), + title: fixTitle, targetDana: data.targetDana, hargaLembar: data.hargaLembar, totalLembar: data.totalLembar, @@ -30,6 +35,24 @@ async function POST(request: Request) { console.log("[CREATE INVESTASI]", create); + const adminUsers = await prisma.user.findMany({ + where: { masterUserRoleId: "2", NOT: { id: data.authorId } }, + select: { id: true }, + }); + + // SEND NOTIFICATION + await sendNotificationMobileToManyUser({ + recipientIds: adminUsers.map((user) => user.id), + senderId: data.authorId, + payload: { + title: "Pengajuan Review Baru", + body: fixTitle as NotificationMobileBodyType, + type: "announcement", + deepLink: routeAdminMobile.investmentByStatus({ status: "review" }), + kategoriApp: "INVESTASI", + }, + }); + return NextResponse.json({ status: 201, success: true, diff --git a/src/lib/mobile/route-page-mobile.ts b/src/lib/mobile/route-page-mobile.ts index bc1a7d06..a467fd77 100644 --- a/src/lib/mobile/route-page-mobile.ts +++ b/src/lib/mobile/route-page-mobile.ts @@ -19,6 +19,10 @@ const routeAdminMobile = { // FORUM forumPreviewReportPosting: `/admin/forum/report-posting`, forumPreviewReportComment: `/admin/forum/report-comment`, + + // INVESTMENT + investmentByStatus: ({ status }: { status: StatusApp }) => + `/admin/investment/${status}/status`, }; const routeUserMobile = { @@ -41,6 +45,13 @@ const routeUserMobile = { // FORUM forumBeranda: `/forum`, forumDetail: ({ id }: { id: string }) => `/forum/${id}`, - forumPreviewReportPosting: ({ id }: { id: string }) => `/forum/${id}/preview-report-posting`, - forumPreviewReportComment: ({ id }: { id: string }) => `/forum/${id}/preview-report-comment`, + forumPreviewReportPosting: ({ id }: { id: string }) => + `/forum/${id}/preview-report-posting`, + forumPreviewReportComment: ({ id }: { id: string }) => + `/forum/${id}/preview-report-comment`, + + // INVESTMENT + investmentByStatus: ({ status }: { status?: StatusApp }) => + `/investment/(tabs)/portofolio?status=${status}`, + investasiDetailPublish: ({ id }: { id: string }) => `/investment/${id}`, };