Fix prisma

Deskripsi:
- Cek pemanggilan prisma di develop mode
This commit is contained in:
2024-12-05 12:11:40 +08:00
parent ac3c1569d0
commit 1e971c1526
6 changed files with 194 additions and 187 deletions

View File

@@ -1,6 +1,6 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
reactStrictMode: false, // reactStrictMode: false,
experimental: { experimental: {
serverActions: true serverActions: true
}, },

View File

@@ -8,6 +8,7 @@
"scripts": { "scripts": {
"dev": "bun --bun run next dev --experimental-https", "dev": "bun --bun run next dev --experimental-https",
"build": "NODE_OPTIONS='--max-old-space-size=2048' bun --bun run next build", "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", "start": "bun --bun run next start",
"lint": "bun --bun run next lint", "lint": "bun --bun run next lint",
"ver": "bunx commit-and-tag-version -- --prerelease" "ver": "bunx commit-and-tag-version -- --prerelease"

View File

@@ -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 { RealtimeProvider } from "../lib";
import { newFunGetUserId } from "../lib/new_fun_user_id";
import { ServerEnv } from "../lib/server_env"; 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({ export default async function Layout({
children, children,
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) { }) {
// const userLoginId = await funGetUserIdByToken(); const userLoginId = await newFunGetUserId();
// 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");
return ( return (
<> <>
<RealtimeProvider <RealtimeProvider
userLoginId={"" as string} userLoginId={userLoginId as string}
WIBU_REALTIME_TOKEN={ WIBU_REALTIME_TOKEN={
ServerEnv.value?.NEXT_PUBLIC_WIBU_REALTIME_TOKEN as string ServerEnv.value?.NEXT_PUBLIC_WIBU_REALTIME_TOKEN as string
} }
/> />
{children} {children}
{/* <CheckCookies_UiLayout dataUser={dataUser as any}>
{children}
</CheckCookies_UiLayout> */}
</> </>
); );
} }

View File

@@ -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<Record<string, any> | null> {
try {
const enc = new TextEncoder().encode(encodedKey);
const { payload } = await jwtVerify(token, enc, {
algorithms: ["HS256"],
});
return (payload.user as Record<string, any>) || null;
} catch (error) {
console.error("Gagal verifikasi session", error);
return null;
}
}

View File

@@ -1,17 +1,26 @@
import { PrismaClient } from '@prisma/client' import { PrismaClient } from '@prisma/client';
const prismaClientSingleton = () => {
return new PrismaClient()
}
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>
// Singleton PrismaClient untuk pengembangan
const globalForPrisma = globalThis as unknown as { 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 export default prisma;
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

View File

@@ -17,8 +17,6 @@ import {
gs_votingTiggerBeranda, gs_votingTiggerBeranda,
IRealtimeData, IRealtimeData,
} from "./global_state"; } from "./global_state";
import { useState } from "react";
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global";
// const WIBU_REALTIME_TOKEN: string | undefined = // const WIBU_REALTIME_TOKEN: string | undefined =
// process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN; // process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN;
@@ -38,7 +36,6 @@ export default function RealtimeProvider({
userLoginId: string; userLoginId: string;
WIBU_REALTIME_TOKEN: string; WIBU_REALTIME_TOKEN: string;
}) { }) {
const [userId, setUserId] = useState("");
const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData); const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData);
const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf); const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf);
const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf); const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf);
@@ -75,10 +72,7 @@ export default function RealtimeProvider({
); );
useShallowEffect(() => { useShallowEffect(() => {
onLoadUser({
onSetUser(val: string) {
if (val !== "" || val !== undefined) {
try { try {
WibuRealtime.init({ WibuRealtime.init({
project: "hipmi", project: "hipmi",
@@ -95,7 +89,7 @@ export default function RealtimeProvider({
if ( if (
data.type == "notification" && data.type == "notification" &&
data.pushNotificationTo == "USER" && data.pushNotificationTo == "USER" &&
data.dataMessage?.userId == userId data.dataMessage?.userId == userLoginId
) { ) {
setNewUserNtf((e) => e + 1); setNewUserNtf((e) => e + 1);
setDataRealtime(data.dataMessage as any); setDataRealtime(data.dataMessage as any);
@@ -142,7 +136,7 @@ export default function RealtimeProvider({
data.type == "notification" && data.type == "notification" &&
data.pushNotificationTo == "USER" && data.pushNotificationTo == "USER" &&
data.dataMessage?.status == "Peserta Event" && data.dataMessage?.status == "Peserta Event" &&
userId !== data.dataMessage?.userId userLoginId !== data.dataMessage?.userId
) { ) {
setNewUserNtf((e) => e + 1); setNewUserNtf((e) => e + 1);
} }
@@ -170,7 +164,7 @@ export default function RealtimeProvider({
data.type == "notification" && data.type == "notification" &&
data.pushNotificationTo == "USER" && data.pushNotificationTo == "USER" &&
data.dataMessage?.status == "Voting Masuk" && data.dataMessage?.status == "Voting Masuk" &&
userId !== data.dataMessage?.userId userLoginId !== data.dataMessage?.userId
) { ) {
setNewUserNtf((e) => e + 1); setNewUserNtf((e) => e + 1);
} }
@@ -207,26 +201,9 @@ export default function RealtimeProvider({
}, },
}); });
} catch (error) { } catch (error) {
console.log(error); console.log("Error!:", error);
}
} 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);
} }
}, []);
return null; return null;
} }