From 8b54f5ca6572cc29a9ec52b15939141b8347fd82 Mon Sep 17 00:00:00 2001 From: bagasbanuna Date: Thu, 29 Jan 2026 15:04:40 +0800 Subject: [PATCH] Fix send whatsapp Auth API - src/app/api/auth/login/route.ts - src/app/api/auth/mobile-login/route.ts - src/app/api/auth/mobile-register/route.ts - src/app/api/auth/resend/route.ts User API (Mobile) - src/app/api/mobile/user/route.ts - src/app/api/mobile/admin/user/[id]/route.ts Utility - src/lib/code-otp-sender.ts ### No issue --- src/app/api/auth/login/route.ts | 4 ++-- src/app/api/auth/mobile-login/route.ts | 4 ++-- src/app/api/auth/mobile-register/route.ts | 4 ++-- src/app/api/auth/resend/route.ts | 4 ++-- src/app/api/mobile/admin/user/[id]/route.ts | 18 ++++++++++++++ src/app/api/mobile/user/route.ts | 26 ++++++++++++--------- src/lib/code-otp-sender.ts | 11 +++++---- 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts index a53bc710..c9e26835 100644 --- a/src/app/api/auth/login/route.ts +++ b/src/app/api/auth/login/route.ts @@ -2,7 +2,7 @@ import { prisma } from "@/lib"; import { randomOTP } from "@/app_modules/auth/fun/rondom_otp"; import backendLogger from "@/util/backendLogger"; import { NextResponse } from "next/server"; -import { sendCodeOtp } from "@/lib/code-otp-sender"; +import { funSendToWhatsApp } from "@/lib/code-otp-sender"; export async function POST(req: Request) { if (req.method !== "POST") { @@ -30,7 +30,7 @@ export async function POST(req: Request) { { status: 400 }, ); - const resSendCode = await sendCodeOtp({ + const resSendCode = await funSendToWhatsApp({ nomor, codeOtp: codeOtp.toString(), }); diff --git a/src/app/api/auth/mobile-login/route.ts b/src/app/api/auth/mobile-login/route.ts index 7b1c4847..ea49ad24 100644 --- a/src/app/api/auth/mobile-login/route.ts +++ b/src/app/api/auth/mobile-login/route.ts @@ -1,7 +1,7 @@ import { prisma } from "@/lib"; import { randomOTP } from "@/app_modules/auth/fun/rondom_otp"; import { NextResponse } from "next/server"; -import { sendCodeOtp } from "@/lib/code-otp-sender"; +import { funSendToWhatsApp } from "@/lib/code-otp-sender"; export async function POST(req: Request) { try { @@ -35,7 +35,7 @@ export async function POST(req: Request) { { status: 400 }, ); - const resSendCode = await sendCodeOtp({ + const resSendCode = await funSendToWhatsApp({ nomor, codeOtp: codeOtp.toString(), }); diff --git a/src/app/api/auth/mobile-register/route.ts b/src/app/api/auth/mobile-register/route.ts index 9f3b74e0..2580fd9d 100644 --- a/src/app/api/auth/mobile-register/route.ts +++ b/src/app/api/auth/mobile-register/route.ts @@ -7,7 +7,7 @@ import { NotificationMobileBodyType, NotificationMobileTitleType, } from "../../../../../types/type-mobile-notification"; -import { sendCodeOtp } from "@/lib/code-otp-sender"; +import { funSendToWhatsApp } from "@/lib/code-otp-sender"; export async function POST(req: Request) { if (req.method !== "POST") { @@ -70,7 +70,7 @@ export async function POST(req: Request) { { status: 400 } ); - const resSendCode = await sendCodeOtp({ + const resSendCode = await funSendToWhatsApp({ nomor: data.nomor, codeOtp: codeOtp.toString(), }); diff --git a/src/app/api/auth/resend/route.ts b/src/app/api/auth/resend/route.ts index eb64a8f5..adc6491c 100644 --- a/src/app/api/auth/resend/route.ts +++ b/src/app/api/auth/resend/route.ts @@ -2,7 +2,7 @@ import { prisma } from "@/lib"; import { randomOTP } from "@/app_modules/auth/fun/rondom_otp"; import backendLogger from "@/util/backendLogger"; import { NextResponse } from "next/server"; -import { sendCodeOtp } from "@/lib/code-otp-sender"; +import { funSendToWhatsApp } from "@/lib/code-otp-sender"; export async function POST(req: Request) { if (req.method !== "POST") { @@ -17,7 +17,7 @@ export async function POST(req: Request) { const body = await req.json(); const { nomor } = body; - const resSendCode = await sendCodeOtp({ + const resSendCode = await funSendToWhatsApp({ nomor, codeOtp: codeOtp.toString(), }); diff --git a/src/app/api/mobile/admin/user/[id]/route.ts b/src/app/api/mobile/admin/user/[id]/route.ts index 5c58248d..b2b03824 100644 --- a/src/app/api/mobile/admin/user/[id]/route.ts +++ b/src/app/api/mobile/admin/user/[id]/route.ts @@ -1,4 +1,5 @@ import { prisma } from "@/lib"; +import { funSendToWhatsApp } from "@/lib/code-otp-sender"; import _ from "lodash"; import { NextResponse } from "next/server"; @@ -50,8 +51,25 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { data: { active: data.active, }, + select: { + nomor: true, + }, }); + if (data.active) { + const resSendCode = await funSendToWhatsApp({ + nomor: updateData.nomor, + newMessage: + "Halo sahabat HIConnect, \nSelamat akun anda telah aktif ! \n\n*Pesan ini di kirim secara otomatis, tidak perlu di balas.", + }); + } else { + const resSendCode = await funSendToWhatsApp({ + nomor: updateData.nomor, + newMessage: + "Halo sahabat HIConnect, \nMohon maaf akun anda telah dinonaktifkan ! Hubungi admin untuk informasi lebih lanjut. \n\n*Pesan ini di kirim secara otomatis, tidak perlu di balas.", + }); + } + console.log("[Update Active Berhasil]", updateData); } else if (category === "role") { const fixName = _.startCase(data.role.replace(/_/g, " ")); diff --git a/src/app/api/mobile/user/route.ts b/src/app/api/mobile/user/route.ts index 3a414098..e84faf27 100644 --- a/src/app/api/mobile/user/route.ts +++ b/src/app/api/mobile/user/route.ts @@ -5,8 +5,16 @@ export async function GET(request: Request) { try { const { searchParams } = new URL(request.url); const search = searchParams.get("search"); + const page = Number(searchParams.get("page")); + const takeData = 10; + const skipData = page * takeData - takeData; + + console.log("SEARCH", search); + console.log("PAGE", page); const data = await prisma.user.findMany({ + take: page ? takeData : undefined, + skip: page ? skipData : undefined, orderBy: { username: "asc", }, @@ -43,16 +51,12 @@ export async function GET(request: Request) { }, }); - return NextResponse.json( - { - success: true, - message: "Berhasil mendapatkan data", - data: data, - }, - { - status: 200, - } - ); + return NextResponse.json({ + status: 200, + success: true, + message: "Berhasil mendapatkan data", + data: data, + }); } catch (error) { return NextResponse.json( { @@ -62,7 +66,7 @@ export async function GET(request: Request) { }, { status: 500, - } + }, ); } diff --git a/src/lib/code-otp-sender.ts b/src/lib/code-otp-sender.ts index 134e20b5..30376c21 100644 --- a/src/lib/code-otp-sender.ts +++ b/src/lib/code-otp-sender.ts @@ -1,13 +1,16 @@ const sendCodeOtp = async ({ nomor, codeOtp, + newMessage, }: { nomor: string; - codeOtp: string; + codeOtp?: string; + newMessage?: string; }) => { - const msg = `HIPMI%20-%20Kode%20ini%20bersifat%20RAHASIA%20dan%20JANGAN%20DI%20BAGIKAN%20KEPADA%20SIAPAPUN%2C%20termasuk%20anggota%20ataupun%20pengurus%20HIPMI%20lainnya.%20Kode%20OTP%20anda%3A%20${codeOtp}.`; + const msg = newMessage || `HIPMI - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun pengurus HIPMI lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`; + const enCode = encodeURIComponent(msg); const res = await fetch( - `https://cld-dkr-prod-wajs-server.wibudev.com/api/wa/code?nom=${nomor}&text=${msg}`, + `https://cld-dkr-prod-wajs-server.wibudev.com/api/wa/code?nom=${nomor}&text=${enCode}`, { cache: "no-cache", headers: { @@ -25,4 +28,4 @@ const sendCodeOtp = async ({ return res; }; -export { sendCodeOtp }; +export { sendCodeOtp as funSendToWhatsApp };