Compare commits

...

38 Commits

Author SHA1 Message Date
a6c9182a01 Fix Server dengan penerapan Github build 2026-03-04 16:38:58 +08:00
453aa0a4ec chore(release): 1.6.7 2026-03-04 16:36:57 +08:00
fe37cce13e Fix publish.yml 2026-03-04 16:08:59 +08:00
ee05d0c71f Build Github 2026-03-04 15:18:01 +08:00
f8319b9ab5 Build with Github 2026-03-04 14:12:12 +08:00
2c1d74973b Fix bug
modified:   next.config.js
        modified:   src/app/api/mobile/forum/[id]/preview-report-comment/route.ts

### No Issue
2026-03-03 16:42:39 +08:00
04c2e0d580 chore(release): 1.6.6 2026-03-03 16:41:45 +08:00
bipproduction
6dba07baac fix: prisma connection exhaustion & firebase lazy init
- prisma/schema.prisma: tambah binaryTargets debian & linux-musl untuk Docker
- src/lib/prisma.ts: pakai global singleton di dev & prod, hapus eager $connect()
- src/lib/firebase-admin.ts: lazy initialization agar tidak crash saat build time
- .env.example: lengkap dengan semua env variable + connection_limit & pool_timeout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-03 16:26:48 +08:00
b76c7a4b1c Merge pull request 'bug-prisma 3' (#65) from bug-prisma/3-mar-26 into staging
Reviewed-on: #65
2026-03-03 15:36:43 +08:00
240f6eb7c2 Fix console 2026-03-03 15:34:36 +08:00
f64ae42825 Fix Prisma
1 fix: error koneksi Prisma dengan retry mechanism
      2
      3 Perubahan:
      4 - src/lib/prisma.ts: Tambah retry (3x) dengan exponential backoff saat connect
      5 - src/lib/prisma-retry.ts: NEW - Utility wrapper untuk retry operations
      6 - src/app/api/user-validate/route.ts: Improve error logging dengan detail
      7 - src/middleware.tsx: Clean up commented code
      8
      9 Fitur:
     10 - Auto retry saat database connection gagal
     11 - Explicit () di production
     12 - Better error logging untuk debugging
     13 - Reusable retry wrapper (withRetry, withTimeout)
     14
     15 Testing:
     16 - Build berhasil 
     17 - Type checking passed 
     18
     19 Fixes: Error in PostgreSQL connection: Error { kind: Closed, cause: None }

### No Issue
2026-03-03 15:30:34 +08:00
82e69309a1 Merge pull request 'bug-prisma 2' (#64) from bug-prisma/3-mar-26 into staging
Reviewed-on: #64
2026-03-03 14:19:10 +08:00
f65f9b7834 Merge pull request 'bug-prisma' (#63) from bug-prisma/3-mar-26 into staging
Reviewed-on: #63
2026-03-03 12:11:04 +08:00
36b9248ed7 Merge pull request 'fix-bug middle' (#62) from fix-bug/25-feb-26 into staging
Reviewed-on: #62
2026-02-25 15:35:20 +08:00
a2c5f053da Merge pull request 'fix-bug/25-feb-26' (#61) from fix-bug/25-feb-26 into staging
Reviewed-on: #61
2026-02-25 14:34:14 +08:00
bedc0cc88b Merge pull request 'Delete termsOfServiceAccepted on register' (#60) from mobile-api/24-feb-26 into staging
Reviewed-on: #60
2026-02-25 10:46:17 +08:00
a9fbd544e5 Merge pull request 'mobile-api/24-feb-26' (#59) from mobile-api/24-feb-26 into staging
Reviewed-on: #59
2026-02-24 07:42:44 +08:00
9afd741d4f Merge pull request 'Fix Admin API Mobile' (#58) from mobile-api/18-feb-26 into staging
Reviewed-on: #58
2026-02-18 17:33:11 +08:00
817919f8f7 Merge pull request '### Fitur: Penambahan Pagination pada Endpoint Admin Mobile' (#57) from mobile-api/14-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/57
2026-02-14 16:25:39 +08:00
90031e23ef Merge pull request 'Fix Api Mobile' (#56) from mobile-api/13-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/56
2026-02-13 17:41:31 +08:00
596ebd2ff4 Merge pull request 'mobile-api/12-feb-26' (#55) from mobile-api/12-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/55
2026-02-12 17:49:00 +08:00
aa700523ca Merge pull request 'feat: Implementasi pagination pada endpoint mobile donation' (#54) from mobile-api/10-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/54
2026-02-10 17:36:02 +08:00
236ab4d4a4 Merge pull request 'Mobile API' (#53) from mobile-api/9-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/53
2026-02-09 17:38:52 +08:00
eaa7692359 Merge pull request 'Fix API mobile Investment' (#52) from mobile-api/6-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/52
2026-02-06 17:39:36 +08:00
d51ce346e6 Merge pull request 'Fix API mobile' (#51) from mobile-api/5-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/51
2026-02-05 17:36:40 +08:00
91f4bb6c9e Merge pull request 'mobile-api/4-jan-26' (#50) from mobile-api/4-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/50
2026-02-05 10:11:03 +08:00
1fe0001994 Merge pull request 'Fix API Job untuk loaddata:' (#49) from mobile-api/2-feb-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/49
2026-02-02 17:11:32 +08:00
b82a283731 Merge pull request 'mobile-api for load data' (#48) from mobile-api/30-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/48
2026-01-30 17:20:09 +08:00
6d7d0fd07e Merge pull request 'mobile-notification done' (#46) from mobile-notification/27-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/46
2026-01-27 17:00:26 +08:00
bc80bb3441 Merge pull request 'Notification Donasi & EULA on login' (#45) from mobile-notification/23-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/45
2026-01-23 17:06:56 +08:00
8ab94b9c86 Merge pull request 'mobile-notification invesment' (#44) from mobile-notification/21-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/44
2026-01-21 15:41:18 +08:00
6e37b18e42 Merge pull request 'mobile-notification report comment' (#43) from mobile-notification/19-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/43
2026-01-19 17:54:21 +08:00
a6db03d0b4 Merge pull request 'mobile-notification event dan voting' (#42) from mobile-notification/15-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/42
2026-01-15 17:41:58 +08:00
c550a4e922 Merge pull request 'mobile-notification try to push to apple and android preview' (#41) from mobile-notification/12-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/41
2026-01-15 17:41:13 +08:00
2431a3fa3e Merge pull request 'Mobile notification & EULA route' (#40) from mobile-notification/9-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/40
2026-01-09 17:47:40 +08:00
1ed0da8c7d Merge pull request 'Fix API mobile notifikasi untuk job' (#39) from mobile-notification/7-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/39
2026-01-08 15:26:19 +08:00
e15a5d796d Merge pull request 'mobile notification' (#38) from mobile-notification/6-jan-26 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/38
2026-01-06 17:53:14 +08:00
836ebfaef0 Merge pull request 'mobile notification API' (#37) from mobile-notification/5-jan-25 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi/pulls/37
2026-01-05 14:06:58 +08:00
17 changed files with 464 additions and 350 deletions

53
.env.example Normal file
View File

@@ -0,0 +1,53 @@
# ==============================
# Database
# ==============================
# Tambahkan connection_limit dan pool_timeout untuk mencegah connection exhaustion
DATABASE_URL="postgresql://user:password@localhost:5432/dbname?connection_limit=10&pool_timeout=20"
# ==============================
# Auth / Session
# ==============================
WIBU_PWD="your_wibu_password"
NEXT_PUBLIC_BASE_TOKEN_KEY="your_token_key"
NEXT_PUBLIC_BASE_SESSION_KEY="your_session_key"
# ==============================
# Payment Gateway (Midtrans)
# ==============================
Client_KEY="your_midtrans_client_key"
Server_KEY="your_midtrans_server_key"
# ==============================
# Maps
# ==============================
MAPBOX_TOKEN="your_mapbox_token"
# ==============================
# Realtime (WebSocket)
# ==============================
WS_APIKEY="your_ws_api_key"
NEXT_PUBLIC_WIBU_REALTIME_TOKEN="your_realtime_token"
# ==============================
# Email (Resend)
# ==============================
RESEND_APIKEY="your_resend_api_key"
# ==============================
# WhatsApp
# ==============================
WA_SERVER_TOKEN="your_wa_server_token"
# ==============================
# Firebase Admin (Push Notification)
# ==============================
FIREBASE_ADMIN_PROJECT_ID="your_firebase_project_id"
FIREBASE_ADMIN_CLIENT_EMAIL="your_firebase_client_email@project.iam.gserviceaccount.com"
# Private key: salin dari service account JSON, ganti newline dengan \n
FIREBASE_ADMIN_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY_HERE\n-----END PRIVATE KEY-----\n"
# ==============================
# App
# ==============================
NEXT_PUBLIC_API_URL="http://localhost:3000"
LOG_LEVEL="info"

72
.github/workflows/publish.yml vendored Normal file
View File

@@ -0,0 +1,72 @@
name: Publish Docker to GHCR
on:
workflow_dispatch:
inputs:
environment:
description: "Target environment"
required: true
type: choice
options:
- production
- staging
tag:
description: "Image tag (e.g. v1.0.0)"
required: true
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
publish:
name: Build & Push to GHCR (${{ github.event.inputs.environment }})
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Free disk space
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /opt/hostedtoolcache/CodeQL
sudo docker image prune --all --force
df -h
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate image metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ github.event.inputs.environment }}-${{ github.event.inputs.tag }}
type=raw,value=${{ github.event.inputs.environment }}-latest
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
no-cache: true

View File

@@ -2,6 +2,15 @@
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
## [1.6.7](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.6.6...v1.6.7) (2026-03-04)
## [1.6.6](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.6.5...v1.6.6) (2026-03-03)
### Bug Fixes
* prisma connection exhaustion & firebase lazy init ([6dba07b](https://wibugit.wibudev.com/wibu/hipmi/commit/6dba07baac6a3ef7d264c13e378a905002401e1b))
## [1.6.5](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.6.4...v1.6.5) (2026-03-03)

72
Dockerfile Normal file
View File

@@ -0,0 +1,72 @@
# ==============================
# Stage 1: Builder
# ==============================
FROM node:20-bookworm-slim AS builder
WORKDIR /app
# Install system deps
RUN apt-get update && apt-get install -y --no-install-recommends \
libc6 \
git \
openssl \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Copy dependency files first (for better caching)
COPY package.json package-lock.json* bun.lockb* ./
ENV SHARP_IGNORE_GLOBAL_LIBVIPS=1
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_OPTIONS="--max-old-space-size=4096"
# 🔥 Skip postinstall scripts (fix onnxruntime error)
RUN npm install --legacy-peer-deps --ignore-scripts
# Copy full source
COPY . .
# Use .env.example as build env
# (Pastikan file ini ada di project)
RUN cp .env.example .env || true
# Generate Prisma Client
ENV PRISMA_CLI_BINARY_TARGETS=debian-openssl-3.0.x
RUN npx prisma generate
# Build Next.js
RUN npm run build
# ==============================
# Stage 2: Runner (Production)
# ==============================
FROM node:20-bookworm-slim AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN apt-get update && apt-get install -y --no-install-recommends \
openssl \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
RUN groupadd --system --gid 1001 nodejs \
&& useradd --system --uid 1001 --gid nodejs nextjs
# Copy standalone output
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
RUN chown -R nextjs:nodejs /app
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]

View File

@@ -1,16 +0,0 @@
import { fileURLToPath } from "url";
import { dirname } from "path"; // Pastikan `dirname` diimpor
import { FlatCompat } from "@eslint/eslintrc";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
});
const eslintConfig = [
...compat.extends("next/core-web-vitals", "next/typescript"),
];
export default eslintConfig;

View File

@@ -1,35 +1,21 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
output: "standalone",
eslint: { ignoreDuringBuilds: true },
typescript: { ignoreBuildErrors: true },
experimental: {
serverActions: true,
serverComponentsExternalPackages: ['@prisma/client'],
},
output: "standalone",
staticPageGenerationTimeout: 180, // tingkatkan menjadi 3 menit
eslint: {
ignoreDuringBuilds: true,
serverComponentsExternalPackages: ["@prisma/client", ".prisma/client"],
},
webpack: (config, { isServer }) => {
if (isServer) {
config.externals = config.externals || [];
config.externals.push('@prisma/client');
config.externals.push("@prisma/client");
config.externals.push(".prisma/client");
}
return config;
},
// async headers() {
// return [
// {
// source: "/(.*)",
// headers: [
// {
// key: "Cache-Control",
// value: "no-store, max-age=0",
// },
// ],
// },
// ];
// },
};
module.exports = nextConfig;
module.exports = nextConfig;

View File

@@ -1,6 +1,6 @@
{
"name": "hipmi",
"version": "1.6.5",
"version": "1.6.7",
"private": true,
"prisma": {
"seed": "bun prisma/seed.ts"

View File

@@ -4,7 +4,7 @@
generator client {
provider = "prisma-client-js"
engineType = "binary"
binaryTargets = ["native"]
binaryTargets = ["native", "debian-openssl-3.0.x", "linux-musl-openssl-3.0.x"]
}
datasource db {

View File

@@ -43,9 +43,9 @@ const envSrc = path.join(__dirname, '../.env');
const envDest = path.join(standaloneDir, '.env');
if (fs.existsSync(envSrc)) {
fs.copyFileSync(envSrc, envDest);
console.log('✓ .env file copied to standalone output');
// console.log('✓ .env file copied to standalone output');
} else {
console.warn('⚠ .env file not found, skipping...');
// console.warn('⚠ .env file not found, skipping...');
console.warn(' Pastikan DATABASE_URL di-set di system environment server!');
}
@@ -54,11 +54,7 @@ const envLocalSrc = path.join(__dirname, '../.env-local');
const envLocalDest = path.join(standaloneDir, '.env-local');
if (fs.existsSync(envLocalSrc)) {
fs.copyFileSync(envLocalSrc, envLocalDest);
console.log('✓ .env-local file copied to standalone output');
// console.log('✓ .env-local file copied to standalone output');
}
console.log('✅ Postbuild script completed!');
console.log('');
console.log('📋 Penting untuk Production:');
console.log(' - Pastikan DATABASE_URL tersedia di environment server, ATAU');
console.log(' - File .env sudah ter-copy ke /app/.env di server');
console.log('✅ Build script completed!');

View File

@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma"
export async function GET(
request: Request,

View File

@@ -27,18 +27,18 @@ export async function GET(request: Request) {
NOT: {
Profile: null,
},
OR: [
{
MasterUserRole: {
name: "User",
},
},
{
MasterUserRole: {
name: "Admin",
},
},
],
// OR: [
// {
// MasterUserRole: {
// name: "User",
// },
// },
// {
// MasterUserRole: {
// name: "Admin",
// },
// },
// ],
},
include: {
Profile: {

View File

@@ -76,15 +76,27 @@ export async function GET(req: Request) {
data: user,
});
} catch (error) {
console.error("Error in user validation:", error);
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
const errorStack = error instanceof Error ? error.stack : 'No stack';
// Log detailed error for debugging
console.error("❌ [USER-VALIDATE] Error:", errorMsg);
console.error("❌ [USER-VALIDATE] Stack:", errorStack);
console.error("❌ [USER-VALIDATE] Time:", new Date().toISOString());
// Check if it's a database connection error
if (errorMsg.includes("Prisma") || errorMsg.includes("database") || errorMsg.includes("connection")) {
console.error("❌ [USER-VALIDATE] Database connection error detected!");
console.error("❌ [USER-VALIDATE] DATABASE_URL exists:", !!process.env.DATABASE_URL);
}
return NextResponse.json(
{
success: false,
message: "Terjadi kesalahan pada server",
error: process.env.NODE_ENV === 'development' ? errorMsg : 'Internal server error',
},
{ status: 500 }
);
}
// Removed prisma.$disconnect() from here to prevent connection pool exhaustion
// Prisma connections are handled globally and shouldn't be disconnected on each request
}

View File

@@ -1,24 +1,25 @@
// lib/firebase-admin.ts
import { cert, getApp, getApps, initializeApp } from 'firebase-admin/app';
import { getMessaging } from 'firebase-admin/messaging';
import { getMessaging, Messaging } from 'firebase-admin/messaging';
// Ambil dari environment
const serviceAccount = {
projectId: process.env.FIREBASE_ADMIN_PROJECT_ID,
clientEmail: process.env.FIREBASE_ADMIN_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_ADMIN_PRIVATE_KEY?.replace(/\\n/g, '\n'),
};
function getAdminApp() {
if (getApps().length > 0) return getApp();
if (!serviceAccount.projectId || !serviceAccount.clientEmail || !serviceAccount.privateKey) {
throw new Error('Firebase Admin credentials are missing in environment variables');
const privateKey = process.env.FIREBASE_ADMIN_PRIVATE_KEY?.replace(/\\n/g, '\n');
const projectId = process.env.FIREBASE_ADMIN_PROJECT_ID;
const clientEmail = process.env.FIREBASE_ADMIN_CLIENT_EMAIL;
if (!projectId || !clientEmail || !privateKey) {
throw new Error('Firebase Admin credentials are missing in environment variables');
}
return initializeApp({
credential: cert({ projectId, clientEmail, privateKey }),
projectId,
});
}
// Inisialisasi hanya sekali
const app = !getApps().length
? initializeApp({
credential: cert(serviceAccount),
projectId: serviceAccount.projectId,
})
: getApp();
export const adminMessaging = getMessaging(app);
export const adminMessaging: Pick<Messaging, 'send' | 'sendEachForMulticast'> = {
send: (message) => getMessaging(getAdminApp()).send(message),
sendEachForMulticast: (message) => getMessaging(getAdminApp()).sendEachForMulticast(message),
};

188
src/lib/prisma-retry.ts Normal file
View File

@@ -0,0 +1,188 @@
import { prisma } from './prisma';
/**
* Retry configuration for database operations
*/
interface RetryConfig {
maxRetries: number;
initialDelay: number;
maxDelay: number;
factor: number;
}
const DEFAULT_RETRY_CONFIG: RetryConfig = {
maxRetries: 3,
initialDelay: 100,
maxDelay: 5000,
factor: 2,
};
/**
* Check if error is retryable (transient error)
*/
function isRetryableError(error: any): boolean {
const errorMsg = error instanceof Error ? error.message : '';
// Retry on connection-related errors
const retryablePatterns = [
'ECONNRESET',
'ECONNREFUSED',
'ETIMEDOUT',
'ENOTFOUND',
'connection closed',
'connection terminated',
'connection timeout',
'socket hang up',
'too many connections',
'pool is full',
'server login has been failing',
'FATAL:',
'PrismaClientUnknownRequestError',
];
return retryablePatterns.some(pattern =>
errorMsg.toLowerCase().includes(pattern.toLowerCase())
);
}
/**
* Execute database operation with retry mechanism
*
* @param operation - The database operation to execute
* @param config - Retry configuration (optional)
* @param operationName - Name of the operation for logging
*
* @example
* const user = await withRetry(
* () => prisma.user.findUnique({ where: { id: '123' } }),
* undefined,
* 'findUser'
* );
*/
export async function withRetry<T>(
operation: () => Promise<T>,
config?: Partial<RetryConfig>,
operationName?: string
): Promise<T> {
const retryConfig = { ...DEFAULT_RETRY_CONFIG, ...config };
let lastError: any;
for (let attempt = 1; attempt <= retryConfig.maxRetries; attempt++) {
try {
const result = await operation();
// Log success if it was a retry
if (attempt > 1 && operationName) {
console.log(`✅ [DB-RETRY] ${operationName} succeeded after ${attempt} attempts`);
}
return result;
} catch (error) {
lastError = error;
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
// Check if we should retry
if (attempt < retryConfig.maxRetries && isRetryableError(error)) {
// Calculate delay with exponential backoff + jitter
const delay = Math.min(
retryConfig.initialDelay * Math.pow(retryConfig.factor, attempt - 1),
retryConfig.maxDelay
);
const jitter = Math.random() * 0.3 * delay; // Add 30% jitter
if (operationName) {
console.warn(
`⚠️ [DB-RETRY] ${operationName} failed (attempt ${attempt}/${retryConfig.maxRetries}): ${errorMsg}`
);
console.log(`⏳ [DB-RETRY] Retrying in ${Math.round(delay + jitter)}ms...`);
}
await new Promise(resolve => setTimeout(resolve, delay + jitter));
} else {
// Don't retry - either max retries reached or not a retryable error
if (operationName) {
console.error(
`❌ [DB-RETRY] ${operationName} failed after ${attempt} attempts: ${errorMsg}`
);
}
break;
}
}
}
// All retries exhausted, throw the last error
throw lastError;
}
/**
* Execute database operation with timeout
*
* @param operation - The database operation to execute
* @param timeout - Timeout in milliseconds (default: 30000)
* @param operationName - Name of the operation for logging
*/
export async function withTimeout<T>(
operation: () => Promise<T>,
timeout: number = 30000,
operationName?: string
): Promise<T> {
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => {
reject(new Error(`Operation timed out after ${timeout}ms`));
}, timeout);
});
try {
return await Promise.race([operation(), timeoutPromise]);
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
if (errorMsg.includes('timed out')) {
if (operationName) {
console.error(`⏱️ [DB-TIMEOUT] ${operationName} timed out after ${timeout}ms`);
}
}
throw error;
}
}
/**
* Combine retry and timeout for robust database operations
*
* @param operation - The database operation to execute
* @param options - Retry and timeout options
* @param operationName - Name of the operation for logging
*/
export async function withRetryAndTimeout<T>(
operation: () => Promise<T>,
options?: {
retry?: Partial<RetryConfig>;
timeout?: number;
},
operationName?: string
): Promise<T> {
return withRetry(
() => withTimeout(operation, options?.timeout, operationName),
options?.retry,
operationName
);
}
/**
* Health check for database connection
*/
export async function checkDatabaseConnection(): Promise<boolean> {
try {
await withTimeout(
() => prisma.$queryRaw`SELECT 1`,
5000,
'healthCheck'
);
return true;
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
console.error('❌ [DB-HEALTH] Database connection check failed:', errorMsg);
return false;
}
}
export { prisma };

View File

@@ -1,73 +1,21 @@
import { PrismaClient } from "@prisma/client";
// Deklarasikan variabel global untuk menandai apakah listener sudah ditambahkan
declare global {
var prisma: PrismaClient;
var prismaListenersAdded: boolean; // Flag untuk menandai listener
var prisma: PrismaClient | undefined;
}
let prisma: PrismaClient;
// if (!process.env.DATABASE_URL) {
// throw new Error("DATABASE_URL is required but not found in environment variables");
// }
// Validasi DATABASE_URL sebelum inisialisasi
if (!process.env.DATABASE_URL) {
console.error('❌ ERROR: DATABASE_URL tidak ditemukan di environment variables!');
console.error('');
console.error('Current working directory:', process.cwd());
console.error('Available environment variables:', Object.keys(process.env).sort().join(', '));
console.error('');
console.error('Solusi:');
console.error(' 1. Pastikan file .env ada dan berisi DATABASE_URL, ATAU');
console.error(' 2. Set DATABASE_URL di system environment:');
console.error(' export DATABASE_URL="postgresql://user:pass@host:port/dbname"');
console.error(' 3. Jika menggunakan systemd service, tambahkan di file service:');
console.error(' [Service]');
console.error(' Environment=DATABASE_URL=postgresql://...');
console.error('');
throw new Error('DATABASE_URL is required but not found in environment variables');
}
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient({
// Reduce logging in production to improve performance
log: ['error', 'warn'],
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
});
} else {
if (!global.prisma) {
global.prisma = new PrismaClient({
log: ['error', 'warn', 'info', 'query'], // More verbose logging in development
datasources: {
db: {
url: process.env.DATABASE_URL,
},
},
});
}
prisma = global.prisma;
}
// Tambahkan listener hanya jika belum ditambahkan sebelumnya
if (!global.prismaListenersAdded) {
// Handle graceful shutdown
process.on("SIGINT", async () => {
console.log("Received SIGINT signal. Closing database connections...");
await prisma.$disconnect();
process.exit(0);
const prisma =
global.prisma ??
new PrismaClient({
log: process.env.NODE_ENV === "development" ? ["error", "warn", "query"] : ["error", "warn"],
});
process.on("SIGTERM", async () => {
console.log("Received SIGTERM signal. Closing database connections...");
await prisma.$disconnect();
process.exit(0);
});
// Tandai bahwa listener sudah ditambahkan
global.prismaListenersAdded = true;
}
// Selalu assign ke global agar hanya ada 1 instance (dev: cegah hot-reload, prod: cegah multiple instances)
global.prisma = prisma;
export default prisma;
export { prisma };

View File

@@ -66,10 +66,6 @@ export const middleware = async (req: NextRequest) => {
const { pathname } = req.nextUrl;
const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || new URL(req.url).origin;
// Removed excessive logging that was causing high CPU usage
// const dbUrl = process.env.DATABASE_URL;
// console.log("DATABASE_URL >>", dbUrl);
// console.log("URL Access >>", req.url);
// Handle CORS preflight
const corsResponse = handleCors(req);

204
zCoba.js
View File

@@ -1,204 +0,0 @@
const { PrismaClient } = require('@prisma/client')
const axios = require('axios')
const prisma = new PrismaClient()
// Daftar contoh data
const donationDataList = [
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Bantuan Pendidikan Anak-anak Kurang Mampu",
"target": 50000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Central Asia",
"rekening": "1234567890",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Kami ingin membantu anak-anak kurang mampu mendapatkan pendidikan yang layak.",
"cerita": "Pendidikan adalah hak dasar setiap anak. Namun, banyak anak-anak di pelosok negeri yang tidak bisa menikmati pendidikan karena keterbatasan ekonomi. Melalui kampanye ini, kami ingin mengumpulkan dana untuk membantu biaya pendidikan mereka.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj0"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Pembangunan Masjid di Desa Terpencil",
"target": 100000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Mandiri",
"rekening": "0987654321",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Membangun masjid untuk masyarakat di daerah terpencil yang belum memiliki tempat ibadah.",
"cerita": "Di sebuah desa terpencil, masyarakat setiap hari harus berjalan jauh untuk bisa melaksanakan sholat berjamaah. Kami ingin membantu membangun masjid di tengah-tengah mereka agar ibadah bisa dilakukan dengan lebih tenang dan khusyuk.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj1"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Bantuan Korban Bencana Alam",
"target": 75000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Rakyat Indonesia",
"rekening": "5678901234",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Membantu meringankan beban korban bencana alam berupa kebutuhan pokok dan kebutuhan darurat.",
"cerita": "Beberapa wilayah dilanda bencana banjir dan tanah longsor. Masyarakat kehilangan harta benda dan membutuhkan bantuan segera. Dana yang terkumpul akan digunakan untuk menyediakan makanan, obat-obatan, dan kebutuhan pokok lainnya.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj2"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Pengadaan Alat Medis Rumah Sakit",
"target": 150000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Negara Indonesia",
"rekening": "4321098765",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Meningkatkan kualitas pelayanan kesehatan dengan menyediakan alat medis yang lebih baik.",
"cerita": "Rumah sakit daerah kekurangan alat medis untuk melayani pasien. Melalui donasi ini, kami ingin membantu pengadaan alat-alat medis penting seperti ventilator, USG, dan alat laboratorium untuk meningkatkan kualitas pelayanan kesehatan.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj3"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Program Beasiswa Mahasiswa Berprestasi",
"target": 80000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Danamon",
"rekening": "1122334455",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Memberikan kesempatan kepada mahasiswa berprestasi untuk melanjutkan pendidikan tanpa beban biaya.",
"cerita": "Banyak mahasiswa berprestasi yang tidak mampu melanjutkan pendidikan karena keterbatasan biaya. Program beasiswa ini akan membantu mereka menyelesaikan kuliah hingga sarjana.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj4"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Pengadaan Air Bersih untuk Desa Kekeringan",
"target": 60000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Permata",
"rekening": "6677889900",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Menyediakan akses air bersih bagi masyarakat yang tinggal di daerah rawan kekeringan.",
"cerita": "Beberapa desa mengalami kekeringan setiap tahunnya, membuat warga kesulitan mendapatkan air bersih. Kami ingin membangun sumur bor dan sistem distribusi air untuk membantu masyarakat setempat.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj5"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Pengobatan Gratis untuk Warga Tidak Mampu",
"target": 40000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Panin",
"rekening": "9988776655",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Memberikan layanan kesehatan gratis bagi warga yang tidak mampu membayar biaya pengobatan.",
"cerita": "Banyak warga yang menunda pengobatan karena keterbatasan biaya. Melalui program ini, kami akan menyelenggarakan pengobatan gratis secara berkala di berbagai wilayah.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj6"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Pembangunan Taman Bacaan Masyarakat",
"target": 35000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank Mega",
"rekening": "1357924680",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Membangun taman bacaan untuk meningkatkan minat baca masyarakat di wilayah pedesaan.",
"cerita": "Minat baca masyarakat di pedesaan masih rendah karena keterbatasan akses buku. Taman bacaan ini akan menyediakan ribuan buku gratis dan ruang baca yang nyaman untuk semua usia.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj7"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Pelatihan Keterampilan untuk Pengangguran",
"target": 55000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank CIMB Niaga",
"rekening": "2468135790",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Memberikan pelatihan keterampilan untuk membantu pengangguran mendapatkan pekerjaan atau usaha mandiri.",
"cerita": "Angka pengangguran masih tinggi di beberapa wilayah. Program pelatihan ini akan memberikan keterampilan yang dibutuhkan pasar kerja, seperti menjahit, memasak, teknologi informasi, dan lainnya.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj8"
}
},
{
"data": {
"authorId": "cmha6wb9w0001cfndwl9fcse6",
"title": "Renovasi Gedung Sekolah Rusak",
"target": 90000000,
"donasiMaster_DurasiId": 3,
"donasiMaster_KategoriId": 3,
"namaBank": "Bank OCBC NISP",
"rekening": "1029384756",
"imageId": "cm60j9q3m000xc9dc584v8rh8",
"pembukaan": "Merestrukturasi gedung sekolah yang rusak agar siswa bisa belajar dengan aman dan nyaman.",
"cerita": "Banyak gedung sekolah yang rusak parah dan membahayakan keselamatan siswa. Dana dari kampanye ini akan digunakan untuk renovasi dan perbaikan gedung sekolah yang membutuhkan.",
"imageCeritaId": "cm60j9q3m000xc9dc584v8rj9"
}
}
];
async function sendDonationData() {
const baseUrl = 'http://localhost:3000/api/mobile/donation'; // Sesuaikan dengan URL server Anda
const headers = {
'Content-Type': 'application/json',
};
for (let i = 0; i < donationDataList.length; i++) {
try {
console.log(`Mengirim data ke-${i + 1}...`);
const response = await axios.post(`${baseUrl}?category=permanent`, donationDataList[i], {
headers: headers
});
console.log(`Data ke-${i + 1} berhasil dikirim:`, response.data);
} catch (error) {
console.error(`Error saat mengirim data ke-${i + 1}:`, error.response?.data || error.message);
}
}
}
async function main() {
// Menjalankan fungsi untuk mengirim data donasi
await sendDonationData();
// Fungsi asli untuk update notifikasi
const result = await prisma.notifikasi.updateMany({
where: {
recipientId: 'cmha7p6yc0000cfoe5w2e7gdr',
},
data: {
isRead: false,
readAt: null,
},
})
console.log(`✅ Rows affected: ${result.count}`)
}
main()
.catch((err) => {
console.error('❌ Error:', err)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})