Merge pull request #278 from bipproduction/bagas/5-feb-25

Bagas/5 feb 25
This commit is contained in:
Bagasbanuna02
2025-02-05 17:53:56 +08:00
committed by GitHub
59 changed files with 917 additions and 1547 deletions

View File

@@ -1,8 +1,12 @@
import { prisma } from "@/app/lib";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export { DELETE };
async function DELETE(request: Request) {
async function DELETE(
request: Request,
{ params }: { params: { id: string } }
) {
if (request.method !== "DELETE") {
return NextResponse.json(
{ success: false, message: "Method not allowed" },
@@ -10,39 +14,24 @@ async function DELETE(request: Request) {
);
}
try {
// Ambil parameter nomor dari URL
const { searchParams } = new URL(request.url);
const nomor = searchParams.get("nomor");
// Ambil parameter id dari URL
const { id } = params;
// Validasi parameter nomor
if (!nomor) {
if (!id) {
return NextResponse.json(
{
success: false,
message: "Parameter 'nomor' diperlukan",
message: "Parameter 'id' diperlukan",
},
{ status: 400 }
);
}
// Cek apakah data OTP dengan nomor tersebut ada
const existingOtp = await prisma.kodeOtp.findFirst({
where: { nomor },
});
if (!existingOtp) {
return NextResponse.json(
{
success: false,
message: "Data OTP tidak ditemukan",
},
{ status: 404 }
);
}
// Hapus data OTP
await prisma.kodeOtp.deleteMany({
where: { nomor },
await prisma.kodeOtp.delete({
where: {
id: id,
},
});
return NextResponse.json(
@@ -53,7 +42,7 @@ async function DELETE(request: Request) {
{ status: 200 }
);
} catch (error) {
console.error("Error deleting OTP:", error);
backendLogger.error("Error deleting OTP:", error);
return NextResponse.json(
{
success: false,

View File

@@ -1,11 +1,19 @@
import { sessionCreate } from "@/app/auth/_lib/session_create";
import { sessionCreate } from "@/app/(auth)/_lib/session_create";
import prisma from "@/app/lib/prisma";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export async function POST(req: Request) {
if (req.method === "POST") {
if (req.method !== "POST") {
return NextResponse.json(
{ success: false, message: "Method Not Allowed" },
{ status: 405 }
);
}
try {
const { data } = await req.json();
console.log("data api register", data);
const cekUsername = await prisma.user.findUnique({
where: {
@@ -13,46 +21,51 @@ export async function POST(req: Request) {
},
});
try {
if (cekUsername)
return NextResponse.json(
{ success: false, message: "Username sudah digunakan" },
{ status: 400 }
);
const createUser = await prisma.user.create({
data: {
username: data.username,
nomor: data.nomor,
active: false,
},
if (cekUsername)
return NextResponse.json({
success: false,
message: "Username sudah digunakan",
});
const token = await sessionCreate({
sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
user: createUser as any,
});
const createUser = await prisma.user.create({
data: {
username: data.username,
nomor: data.nomor,
active: false,
},
});
if (!createUser)
return NextResponse.json(
{ success: true, message: "Berhasil Login", data: createUser },
{ status: 200 }
);
} catch (error) {
backendLogger.log("Error registrasi:", error);
return NextResponse.json(
{
success: false,
message: "Server Error",
reason: (error as Error).message,
},
{ success: false, message: "Gagal Registrasi" },
{ status: 500 }
);
}
}
return NextResponse.json(
{ success: false, message: "Method Not Allowed" },
{ status: 405 }
);
const token = await sessionCreate({
sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
user: createUser as any,
});
return NextResponse.json(
{
success: true,
message: "Registrasi Berhasil, Anda Sedang Login",
// data: createUser,
},
{ status: 201 }
);
} catch (error) {
backendLogger.error("Error registrasi:", error);
return NextResponse.json(
{
success: false,
message: "Maaf, Terjadi Keselahan",
reason: (error as Error).message,
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -1,69 +1,72 @@
import { prisma } from "@/app/lib";
import { randomOTP } from "@/app_modules/auth/fun/rondom_otp";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export async function POST(req: Request) {
if (req.method === "POST") {
if (req.method !== "POST") {
return NextResponse.json(
{ success: false, message: "Method Not Allowed" },
{ status: 405 }
);
}
try {
const codeOtp = randomOTP();
const body = await req.json();
const { nomor } = body;
try {
const res = await fetch(
`https://wa.wibudev.com/code?nom=${nomor}&text=HIPMI - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun pengurus HIPMI lainnya.
const res = await fetch(
`https://wa.wibudev.com/code?nom=${nomor}&text=HIPMI - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun pengurus HIPMI lainnya.
\n
>> Kode OTP anda: ${codeOtp}.
`
);
const sendWa = await res.json();
if (sendWa.status !== "success")
return NextResponse.json(
{
success: false,
message: "Nomor Whatsapp Tidak Aktif",
},
{ status: 400 }
);
const createOtpId = await prisma.kodeOtp.create({
data: {
nomor: nomor,
otp: codeOtp,
},
});
if (!createOtpId)
return NextResponse.json(
{
success: false,
message: "Gagal Membuat Kode OTP",
},
{ status: 400 }
);
return NextResponse.json(
{
success: true,
message: "Kode Verifikasi Dikirim",
kodeId: createOtpId.id,
},
{ status: 200 }
);
} catch (error) {
console.log(error);
);
const sendWa = await res.json();
if (sendWa.status !== "success")
return NextResponse.json(
{
success: false,
message: "Server Whatsapp Error !!",
message: "Nomor Whatsapp Tidak Aktif",
},
{ status: 500 }
{ status: 400 }
);
}
const createOtpId = await prisma.kodeOtp.create({
data: {
nomor: nomor,
otp: codeOtp,
},
});
if (!createOtpId)
return NextResponse.json(
{
success: false,
message: "Gagal Membuat Kode OTP",
},
{ status: 400 }
);
return NextResponse.json(
{
success: true,
message: "Kode Verifikasi Dikirim",
kodeId: createOtpId.id,
},
{ status: 200 }
);
} catch (error) {
backendLogger.error(" Error Resend OTP", error);
return NextResponse.json(
{
success: false,
message: "Server Whatsapp Error !!",
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
return NextResponse.json(
{ success: false, message: "Method Not Allowed" },
{ status: 405 }
);
}

View File

@@ -1,4 +1,4 @@
import { sessionCreate } from "@/app/auth/_lib/session_create";
import { sessionCreate } from "@/app/(auth)/_lib/session_create";
import prisma from "@/app/lib/prisma";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
@@ -52,7 +52,7 @@ export async function POST(req: Request) {
return NextResponse.json(
{
success: false,
message: "API Error or Server Error",
message: "Maaf, Terjadi Keselahan",
reason: (error as Error).message,
},
{ status: 500 }

View File

@@ -1,4 +1,4 @@
import { decrypt } from "@/app/auth/_lib/decrypt";
import { decrypt } from "@/app/(auth)/_lib/decrypt";
import { prisma } from "@/app/lib";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
@@ -32,7 +32,6 @@ export async function GET(req: NextRequest) {
});
// Disconnect after successful query
await prisma.$disconnect();
return NextResponse.json({
success: true,
@@ -41,7 +40,6 @@ export async function GET(req: NextRequest) {
});
} catch (error) {
// Ensure connection is closed even if error occurs
await prisma.$disconnect();
console.error("Error in user validation:", error);
return NextResponse.json(
@@ -51,5 +49,7 @@ export async function GET(req: NextRequest) {
},
{ status: 500 }
);
} finally {
await prisma.$disconnect();
}
}

View File

@@ -1,4 +1,4 @@
import { decrypt } from "@/app/auth/_lib/decrypt";
import { decrypt } from "@/app/(auth)/_lib/decrypt";
import _ from "lodash";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";

View File

@@ -1,5 +1,5 @@
import { decrypt } from "@/app/auth/_lib/decrypt";
import { decrypt } from "@/app/(auth)/_lib/decrypt";
import _ from "lodash";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";

View File

@@ -1,10 +1,9 @@
import { NextResponse } from "next/server";
export async function GET(req: Request) {
const auth = req.headers.get("Authorization");
const token = auth?.split(" ")[1];
if (!token) return NextResponse.json({ success: false }, { status: 401 });
return NextResponse.json({ success: true });