From 1e971c15265e8c7add5ad4d6e42dd5b45dbc5a47 Mon Sep 17 00:00:00 2001 From: Bagasbanuna02 Date: Thu, 5 Dec 2024 12:11:40 +0800 Subject: [PATCH] Fix prisma Deskripsi: - Cek pemanggilan prisma di develop mode --- next.config.js | 2 +- package.json | 3 +- src/app/dev/layout.tsx | 24 +-- src/app/lib/new_fun_user_id.ts | 38 ++++ src/app/lib/prisma.ts | 33 ++-- src/app/lib/realtime_provider.tsx | 281 ++++++++++++++---------------- 6 files changed, 194 insertions(+), 187 deletions(-) create mode 100644 src/app/lib/new_fun_user_id.ts diff --git a/next.config.js b/next.config.js index 5c728631..b00f93a2 100644 --- a/next.config.js +++ b/next.config.js @@ -1,6 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { - reactStrictMode: false, + // reactStrictMode: false, experimental: { serverActions: true }, diff --git a/package.json b/package.json index fb9c76ac..bb7c07b7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "scripts": { "dev": "bun --bun run next dev --experimental-https", "build": "NODE_OPTIONS='--max-old-space-size=2048' bun --bun run next build", + "build:dev": "bun --bun run next build", "start": "bun --bun run next start", "lint": "bun --bun run next lint", "ver": "bunx commit-and-tag-version -- --prerelease" @@ -94,4 +95,4 @@ "wibu-pkg": "^1.0.3", "yaml": "^2.3.2" } -} +} \ No newline at end of file diff --git a/src/app/dev/layout.tsx b/src/app/dev/layout.tsx index aad4db0b..1ac8606a 100644 --- a/src/app/dev/layout.tsx +++ b/src/app/dev/layout.tsx @@ -1,42 +1,24 @@ -import { funGetUserIdByToken } from "@/app_modules/_global/fun/get"; -import { funGlobal_getUserById } from "@/app_modules/_global/fun/get/fun_get_user_by_id"; -import { redirect } from "next/navigation"; import { RealtimeProvider } from "../lib"; +import { newFunGetUserId } from "../lib/new_fun_user_id"; import { ServerEnv } from "../lib/server_env"; -import { RouterAdminDashboard } from "../lib/router_hipmi/router_admin"; -import { funGlobal_checkActivationUseById } from "@/app_modules/_global/fun/get/fun_check_activation_use_by_id"; export default async function Layout({ children, }: { children: React.ReactNode; }) { - // const userLoginId = await funGetUserIdByToken(); - // const dataUser = await funGlobal_getUserById({ - // userId: userLoginId as string, - // }); - - // if (dataUser?.masterUserRoleId != "1") return redirect("/dev/home"); - - // const activationUser = await funGlobal_checkActivationUseById({ - // userId: userLoginId as string, - // }); - - // if (activationUser == false) return redirect("/waiting-room"); + const userLoginId = await newFunGetUserId(); return ( <> {children} - {/* - {children} - */} ); } diff --git a/src/app/lib/new_fun_user_id.ts b/src/app/lib/new_fun_user_id.ts new file mode 100644 index 00000000..188afff5 --- /dev/null +++ b/src/app/lib/new_fun_user_id.ts @@ -0,0 +1,38 @@ +import { jwtVerify } from "jose"; +import _ from "lodash"; +import { cookies } from "next/headers"; + +export async function newFunGetUserId() { + const c = cookies().get(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!); + + if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) { + return null; + } + + const token = c.value; + const dataUser = await decrypt({ + token: token, + encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!, + }); + + return dataUser?.id; +} + +async function decrypt({ + token, + encodedKey, +}: { + token: string; + encodedKey: string; +}): Promise | null> { + try { + const enc = new TextEncoder().encode(encodedKey); + const { payload } = await jwtVerify(token, enc, { + algorithms: ["HS256"], + }); + return (payload.user as Record) || null; + } catch (error) { + console.error("Gagal verifikasi session", error); + return null; + } +} diff --git a/src/app/lib/prisma.ts b/src/app/lib/prisma.ts index 553b19ad..a0d1a749 100644 --- a/src/app/lib/prisma.ts +++ b/src/app/lib/prisma.ts @@ -1,17 +1,26 @@ -import { PrismaClient } from '@prisma/client' - -const prismaClientSingleton = () => { - return new PrismaClient() -} - -type PrismaClientSingleton = ReturnType +import { PrismaClient } from '@prisma/client'; +// Singleton PrismaClient untuk pengembangan const globalForPrisma = globalThis as unknown as { - prisma: PrismaClientSingleton | undefined + __prisma__: PrismaClient | undefined; +}; + +export const prisma = globalForPrisma.__prisma__ ?? new PrismaClient({ + // log: process.env.NODE_ENV === 'development' ? ['query', 'info', 'warn', 'error'] : [], +}); + +// Gunakan PrismaClient yang sama jika sudah ada +if (process.env.NODE_ENV !== 'production') { + if (!globalForPrisma.__prisma__) { + console.log('PrismaClient initialized in development mode'); + } + globalForPrisma.__prisma__ = prisma; } -const prisma = globalForPrisma.prisma ?? prismaClientSingleton() +process.on('SIGINT', async () => { + console.log('Disconnecting PrismaClient...'); + await prisma.$disconnect(); + process.exit(0); +}); -export default prisma - -if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma \ No newline at end of file +export default prisma; diff --git a/src/app/lib/realtime_provider.tsx b/src/app/lib/realtime_provider.tsx index ba302d45..42b32e7a 100644 --- a/src/app/lib/realtime_provider.tsx +++ b/src/app/lib/realtime_provider.tsx @@ -17,8 +17,6 @@ import { gs_votingTiggerBeranda, IRealtimeData, } from "./global_state"; -import { useState } from "react"; -import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global"; // const WIBU_REALTIME_TOKEN: string | undefined = // process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN; @@ -38,7 +36,6 @@ export default function RealtimeProvider({ userLoginId: string; WIBU_REALTIME_TOKEN: string; }) { - const [userId, setUserId] = useState(""); const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData); const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf); const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf); @@ -75,158 +72,138 @@ export default function RealtimeProvider({ ); useShallowEffect(() => { - onLoadUser({ - onSetUser(val: string) { - if (val !== "" || val !== undefined) { - try { - WibuRealtime.init({ - project: "hipmi", - WIBU_REALTIME_TOKEN: WIBU_REALTIME_TOKEN, - onData(data: TypeNotification) { - if ( - data.type == "notification" && - data.pushNotificationTo == "ADMIN" - ) { - setNewAdminNtf((e) => e + 1); - } - - // Notifikasi ke semua user , yang datanya di acc admin - if ( - data.type == "notification" && - data.pushNotificationTo == "USER" && - data.dataMessage?.userId == userId - ) { - setNewUserNtf((e) => e + 1); - setDataRealtime(data.dataMessage as any); - } - - // ---------------------- JOB ------------------------- // - if ( - data.type == "trigger" && - data.pushNotificationTo == "ADMIN" && - data.dataMessage?.kategoriApp == "JOB" - ) { - setIsAdminJob_TriggerReview(true); - } - - if ( - data.type == "trigger" && - data.pushNotificationTo == "USER" && - data.dataMessage?.kategoriApp == "JOB" && - data.dataMessage.status == "Publish" - ) { - setIsTriggerJobBeranda(true); - } - // ---------------------- JOB ------------------------- // - - // ---------------------- EVENT ------------------------- // - if ( - data.type == "trigger" && - data.pushNotificationTo == "ADMIN" && - data.dataMessage?.kategoriApp == "EVENT" - ) { - setIsAdminEvent_TriggerReview(true); - } - - if ( - data.type == "trigger" && - data.pushNotificationTo == "USER" && - data.dataMessage?.kategoriApp == "EVENT" && - data.dataMessage.status == "Publish" - ) { - setIsTriggerEventBeranda(true); - } - - if ( - data.type == "notification" && - data.pushNotificationTo == "USER" && - data.dataMessage?.status == "Peserta Event" && - userId !== data.dataMessage?.userId - ) { - setNewUserNtf((e) => e + 1); - } - // ---------------------- EVENT ------------------------- // - - // ---------------------- VOTING ------------------------- // - if ( - data.type == "trigger" && - data.pushNotificationTo == "ADMIN" && - data.dataMessage?.kategoriApp == "VOTING" - ) { - setIsAdminVoting_TriggerReview(true); - } - - if ( - data.type == "trigger" && - data.pushNotificationTo == "USER" && - data.dataMessage?.kategoriApp == "VOTING" && - data.dataMessage.status == "Publish" - ) { - setIsTriggerVotingBeranda(true); - } - - if ( - data.type == "notification" && - data.pushNotificationTo == "USER" && - data.dataMessage?.status == "Voting Masuk" && - userId !== data.dataMessage?.userId - ) { - setNewUserNtf((e) => e + 1); - } - // ---------------------- VOTING ------------------------- // - - // ---------------------- DONASI ------------------------- // - if ( - data.type == "trigger" && - data.pushNotificationTo == "ADMIN" && - data.dataMessage?.kategoriApp == "DONASI" - ) { - setIsAdminDonasi_TriggerReview(true); - } - - if ( - data.type == "trigger" && - data.pushNotificationTo == "USER" && - data.dataMessage?.kategoriApp == "DONASI" && - data.dataMessage.status == "Publish" - ) { - setIsTriggerDonasiBeranda(true); - } - - // if ( - // data.type == "notification" && - // data.pushNotificationTo == "ADMIN" && - // data.dataMessage?.status == "Menunggu" && - // userLoginId !== data.dataMessage?.userId - // ) { - // console.log("yes"); - // } - - // ---------------------- DONASI ------------------------- // - }, - }); - } catch (error) { - console.log(error); + try { + WibuRealtime.init({ + project: "hipmi", + WIBU_REALTIME_TOKEN: WIBU_REALTIME_TOKEN, + onData(data: TypeNotification) { + if ( + data.type == "notification" && + data.pushNotificationTo == "ADMIN" + ) { + setNewAdminNtf((e) => e + 1); } - } else { - return undefined; - } - }, - }); - }, [setUserId]); - async function onLoadUser({ - onSetUser, - }: { - onSetUser: (val: string) => void; - }) { - const res = await fetch("/api/user", { - method: "GET", - }); - const result = await res.json(); - onSetUser(result.data.id); - } + // Notifikasi ke semua user , yang datanya di acc admin + if ( + data.type == "notification" && + data.pushNotificationTo == "USER" && + data.dataMessage?.userId == userLoginId + ) { + setNewUserNtf((e) => e + 1); + setDataRealtime(data.dataMessage as any); + } + + // ---------------------- JOB ------------------------- // + if ( + data.type == "trigger" && + data.pushNotificationTo == "ADMIN" && + data.dataMessage?.kategoriApp == "JOB" + ) { + setIsAdminJob_TriggerReview(true); + } + + if ( + data.type == "trigger" && + data.pushNotificationTo == "USER" && + data.dataMessage?.kategoriApp == "JOB" && + data.dataMessage.status == "Publish" + ) { + setIsTriggerJobBeranda(true); + } + // ---------------------- JOB ------------------------- // + + // ---------------------- EVENT ------------------------- // + if ( + data.type == "trigger" && + data.pushNotificationTo == "ADMIN" && + data.dataMessage?.kategoriApp == "EVENT" + ) { + setIsAdminEvent_TriggerReview(true); + } + + if ( + data.type == "trigger" && + data.pushNotificationTo == "USER" && + data.dataMessage?.kategoriApp == "EVENT" && + data.dataMessage.status == "Publish" + ) { + setIsTriggerEventBeranda(true); + } + + if ( + data.type == "notification" && + data.pushNotificationTo == "USER" && + data.dataMessage?.status == "Peserta Event" && + userLoginId !== data.dataMessage?.userId + ) { + setNewUserNtf((e) => e + 1); + } + // ---------------------- EVENT ------------------------- // + + // ---------------------- VOTING ------------------------- // + if ( + data.type == "trigger" && + data.pushNotificationTo == "ADMIN" && + data.dataMessage?.kategoriApp == "VOTING" + ) { + setIsAdminVoting_TriggerReview(true); + } + + if ( + data.type == "trigger" && + data.pushNotificationTo == "USER" && + data.dataMessage?.kategoriApp == "VOTING" && + data.dataMessage.status == "Publish" + ) { + setIsTriggerVotingBeranda(true); + } + + if ( + data.type == "notification" && + data.pushNotificationTo == "USER" && + data.dataMessage?.status == "Voting Masuk" && + userLoginId !== data.dataMessage?.userId + ) { + setNewUserNtf((e) => e + 1); + } + // ---------------------- VOTING ------------------------- // + + // ---------------------- DONASI ------------------------- // + if ( + data.type == "trigger" && + data.pushNotificationTo == "ADMIN" && + data.dataMessage?.kategoriApp == "DONASI" + ) { + setIsAdminDonasi_TriggerReview(true); + } + + if ( + data.type == "trigger" && + data.pushNotificationTo == "USER" && + data.dataMessage?.kategoriApp == "DONASI" && + data.dataMessage.status == "Publish" + ) { + setIsTriggerDonasiBeranda(true); + } + + // if ( + // data.type == "notification" && + // data.pushNotificationTo == "ADMIN" && + // data.dataMessage?.status == "Menunggu" && + // userLoginId !== data.dataMessage?.userId + // ) { + // console.log("yes"); + // } + + // ---------------------- DONASI ------------------------- // + }, + }); + } catch (error) { + console.log("Error!:", error); + } + }, []); return null; }