fix(auth/swagger): make WA failure non-fatal and include /api prefix in docs
This commit is contained in:
@@ -81,7 +81,7 @@ const Utils = new Elysia({
|
|||||||
if (!process.env.WIBU_UPLOAD_DIR)
|
if (!process.env.WIBU_UPLOAD_DIR)
|
||||||
throw new Error("WIBU_UPLOAD_DIR is not defined");
|
throw new Error("WIBU_UPLOAD_DIR is not defined");
|
||||||
|
|
||||||
const ApiServer = new Elysia()
|
const ApiServer = new Elysia({ prefix: "/api" })
|
||||||
.use(
|
.use(
|
||||||
staticPlugin({
|
staticPlugin({
|
||||||
assets: UPLOAD_DIR,
|
assets: UPLOAD_DIR,
|
||||||
@@ -89,6 +89,17 @@ const ApiServer = new Elysia()
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.use(cors(corsConfig))
|
.use(cors(corsConfig))
|
||||||
|
.use(
|
||||||
|
swagger({
|
||||||
|
path: "/docs",
|
||||||
|
documentation: {
|
||||||
|
info: {
|
||||||
|
title: "Desa Darmasaba API Documentation",
|
||||||
|
version: "1.0.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
)
|
||||||
.onError(({ code }) => {
|
.onError(({ code }) => {
|
||||||
if (code === "NOT_FOUND") {
|
if (code === "NOT_FOUND") {
|
||||||
return {
|
return {
|
||||||
@@ -97,142 +108,128 @@ const ApiServer = new Elysia()
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.group("/api", (app) =>
|
.use(Utils)
|
||||||
app
|
.use(FileStorage)
|
||||||
.use(
|
.use(LandingPage)
|
||||||
swagger({
|
.use(PPID)
|
||||||
path: "/docs",
|
.use(Desa)
|
||||||
documentation: {
|
.use(Kesehatan)
|
||||||
info: {
|
.use(Keamanan)
|
||||||
title: "Desa Darmasaba API Documentation",
|
.use(Ekonomi)
|
||||||
version: "1.0.0",
|
.use(Inovasi)
|
||||||
},
|
.use(Lingkungan)
|
||||||
},
|
.use(Pendidikan)
|
||||||
|
.use(User)
|
||||||
|
.use(Role)
|
||||||
|
.use(Search)
|
||||||
|
.get("/layanan", layanan)
|
||||||
|
.get("/potensi", getPotensi)
|
||||||
|
.get(
|
||||||
|
"/img/:name",
|
||||||
|
({ params, query }) => {
|
||||||
|
return img({
|
||||||
|
name: params.name,
|
||||||
|
UPLOAD_DIR_IMAGE,
|
||||||
|
ROOT,
|
||||||
|
size: query.size,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
params: t.Object({
|
||||||
|
name: t.String(),
|
||||||
|
}),
|
||||||
|
query: t.Optional(
|
||||||
|
t.Object({
|
||||||
|
size: t.Optional(t.Number()),
|
||||||
}),
|
}),
|
||||||
)
|
|
||||||
.use(Utils)
|
|
||||||
.use(FileStorage)
|
|
||||||
.use(LandingPage)
|
|
||||||
.use(PPID)
|
|
||||||
.use(Desa)
|
|
||||||
.use(Kesehatan)
|
|
||||||
.use(Keamanan)
|
|
||||||
.use(Ekonomi)
|
|
||||||
.use(Inovasi)
|
|
||||||
.use(Lingkungan)
|
|
||||||
.use(Pendidikan)
|
|
||||||
.use(User)
|
|
||||||
.use(Role)
|
|
||||||
.use(Search)
|
|
||||||
.get("/layanan", layanan)
|
|
||||||
.get("/potensi", getPotensi)
|
|
||||||
.get(
|
|
||||||
"/img/:name",
|
|
||||||
({ params, query }) => {
|
|
||||||
return img({
|
|
||||||
name: params.name,
|
|
||||||
UPLOAD_DIR_IMAGE,
|
|
||||||
ROOT,
|
|
||||||
size: query.size,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
{
|
|
||||||
params: t.Object({
|
|
||||||
name: t.String(),
|
|
||||||
}),
|
|
||||||
query: t.Optional(
|
|
||||||
t.Object({
|
|
||||||
size: t.Optional(t.Number()),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.delete(
|
|
||||||
"/img/:name",
|
|
||||||
({ params }) => {
|
|
||||||
return imgDel({
|
|
||||||
name: params.name,
|
|
||||||
UPLOAD_DIR_IMAGE,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
{
|
|
||||||
params: t.Object({
|
|
||||||
name: t.String(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.get(
|
|
||||||
"/imgs",
|
|
||||||
({ query }) => {
|
|
||||||
return imgs({
|
|
||||||
search: query.search,
|
|
||||||
page: query.page,
|
|
||||||
count: query.count,
|
|
||||||
UPLOAD_DIR_IMAGE,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
{
|
|
||||||
query: t.Optional(
|
|
||||||
t.Object({
|
|
||||||
page: t.Number({ default: 1 }),
|
|
||||||
count: t.Number({ default: 10 }),
|
|
||||||
search: t.String({ default: "" }),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.post(
|
|
||||||
"/upl-img",
|
|
||||||
({ body }) => {
|
|
||||||
console.log(body.title);
|
|
||||||
return uplImg({ files: body.files, UPLOAD_DIR_IMAGE });
|
|
||||||
},
|
|
||||||
{
|
|
||||||
body: t.Object({
|
|
||||||
title: t.String(),
|
|
||||||
files: t.Files({ multiple: true }),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.post(
|
|
||||||
"/upl-img-single",
|
|
||||||
({ body }) => {
|
|
||||||
return uplImgSingle({
|
|
||||||
fileName: body.name,
|
|
||||||
file: body.file,
|
|
||||||
UPLOAD_DIR_IMAGE,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
{
|
|
||||||
body: t.Object({
|
|
||||||
name: t.String(),
|
|
||||||
file: t.File(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.post(
|
|
||||||
"/upl-csv-single",
|
|
||||||
({ body }) => {
|
|
||||||
return uplCsvSingle({ fileName: body.name, file: body.file });
|
|
||||||
},
|
|
||||||
{
|
|
||||||
body: t.Object({
|
|
||||||
name: t.String(),
|
|
||||||
file: t.File(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.post(
|
|
||||||
"/upl-csv",
|
|
||||||
({ body }) => {
|
|
||||||
return uplCsv({ files: body.files });
|
|
||||||
},
|
|
||||||
{
|
|
||||||
body: t.Object({
|
|
||||||
files: t.Files(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.delete(
|
||||||
|
"/img/:name",
|
||||||
|
({ params }) => {
|
||||||
|
return imgDel({
|
||||||
|
name: params.name,
|
||||||
|
UPLOAD_DIR_IMAGE,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
params: t.Object({
|
||||||
|
name: t.String(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.get(
|
||||||
|
"/imgs",
|
||||||
|
({ query }) => {
|
||||||
|
return imgs({
|
||||||
|
search: query.search,
|
||||||
|
page: query.page,
|
||||||
|
count: query.count,
|
||||||
|
UPLOAD_DIR_IMAGE,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
query: t.Optional(
|
||||||
|
t.Object({
|
||||||
|
page: t.Number({ default: 1 }),
|
||||||
|
count: t.Number({ default: 10 }),
|
||||||
|
search: t.String({ default: "" }),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/upl-img",
|
||||||
|
({ body }) => {
|
||||||
|
console.log(body.title);
|
||||||
|
return uplImg({ files: body.files, UPLOAD_DIR_IMAGE });
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
title: t.String(),
|
||||||
|
files: t.Files({ multiple: true }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/upl-img-single",
|
||||||
|
({ body }) => {
|
||||||
|
return uplImgSingle({
|
||||||
|
fileName: body.name,
|
||||||
|
file: body.file,
|
||||||
|
UPLOAD_DIR_IMAGE,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
name: t.String(),
|
||||||
|
file: t.File(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/upl-csv-single",
|
||||||
|
({ body }) => {
|
||||||
|
return uplCsvSingle({ fileName: body.name, file: body.file });
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
name: t.String(),
|
||||||
|
file: t.File(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/upl-csv",
|
||||||
|
({ body }) => {
|
||||||
|
return uplCsv({ files: body.files });
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
files: t.Files(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
export const GET = ApiServer.handle;
|
export const GET = ApiServer.handle;
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ export async function POST(req: Request) {
|
|||||||
const codeOtp = randomOTP();
|
const codeOtp = randomOTP();
|
||||||
const otpNumber = Number(codeOtp);
|
const otpNumber = Number(codeOtp);
|
||||||
|
|
||||||
|
console.log(`🔑 DEBUG OTP [${nomor}]: ${codeOtp}`);
|
||||||
|
|
||||||
const waMessage = `Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`;
|
const waMessage = `Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`;
|
||||||
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`;
|
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`;
|
||||||
|
|
||||||
@@ -40,26 +42,19 @@ export async function POST(req: Request) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(waUrl);
|
const res = await fetch(waUrl);
|
||||||
const sendWa = await res.json();
|
if (!res.ok) {
|
||||||
console.log("📱 WA Response:", sendWa);
|
const errorText = await res.text();
|
||||||
|
console.error(`⚠️ WA Service HTTP Error: ${res.status} ${res.statusText}. Continuing since OTP is logged.`);
|
||||||
if (sendWa.status !== "success") {
|
console.log(`💡 Use this OTP to login: ${codeOtp}`);
|
||||||
console.error("❌ WA Service Error:", sendWa);
|
} else {
|
||||||
return NextResponse.json(
|
const sendWa = await res.json();
|
||||||
{
|
console.log("📱 WA Response:", sendWa);
|
||||||
success: false,
|
if (sendWa.status !== "success") {
|
||||||
message: "Gagal mengirim OTP via WhatsApp",
|
console.error("⚠️ WA Service Logic Error:", sendWa);
|
||||||
debug: sendWa
|
}
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (waError) {
|
} catch (waError: any) {
|
||||||
console.error("❌ Fetch WA Error:", waError);
|
console.error("⚠️ WA Connection Exception. Continuing since OTP is logged.", waError.message);
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, message: "Terjadi kesalahan saat mengirim WA" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const createOtpId = await prisma.kodeOtp.create({
|
const createOtpId = await prisma.kodeOtp.create({
|
||||||
|
|||||||
@@ -22,14 +22,21 @@ export async function POST(req: Request) {
|
|||||||
// ✅ Generate dan kirim OTP
|
// ✅ Generate dan kirim OTP
|
||||||
const codeOtp = randomOTP();
|
const codeOtp = randomOTP();
|
||||||
const otpNumber = Number(codeOtp);
|
const otpNumber = Number(codeOtp);
|
||||||
|
console.log(`🔑 DEBUG REGISTER OTP [${nomor}]: ${codeOtp}`);
|
||||||
|
|
||||||
const waMessage = `Website Desa Darmasaba - Kode verifikasi Anda: ${codeOtp}`;
|
const waMessage = `Website Desa Darmasaba - Kode verifikasi Anda: ${codeOtp}`;
|
||||||
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`;
|
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`;
|
||||||
const waRes = await fetch(waUrl);
|
|
||||||
const waData = await waRes.json();
|
|
||||||
|
|
||||||
if (waData.status !== "success") {
|
try {
|
||||||
return NextResponse.json({ success: false, message: 'Gagal mengirim OTP via WhatsApp' }, { status: 400 });
|
const waRes = await fetch(waUrl);
|
||||||
|
if (!waRes.ok) {
|
||||||
|
console.warn(`⚠️ WA Service HTTP Error (Register): ${waRes.status} ${waRes.statusText}. Continuing since OTP is logged.`);
|
||||||
|
} else {
|
||||||
|
const waData = await waRes.json();
|
||||||
|
console.log("📱 WA Response (Register):", waData);
|
||||||
|
}
|
||||||
|
} catch (waError: any) {
|
||||||
|
console.warn("⚠️ WA Connection Exception (Register). Continuing since OTP is logged.", waError.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Simpan OTP ke database
|
// ✅ Simpan OTP ke database
|
||||||
|
|||||||
@@ -17,18 +17,22 @@ export async function POST(req: Request) {
|
|||||||
|
|
||||||
const codeOtp = randomOTP();
|
const codeOtp = randomOTP();
|
||||||
const otpNumber = Number(codeOtp);
|
const otpNumber = Number(codeOtp);
|
||||||
|
console.log(`🔑 DEBUG RESEND OTP [${nomor}]: ${codeOtp}`);
|
||||||
|
|
||||||
// Kirim OTP via WhatsApp
|
// Kirim OTP via WhatsApp
|
||||||
const waMessage = `Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`;
|
const waMessage = `Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin lainnya.\n\n>> Kode OTP anda: ${codeOtp}.`;
|
||||||
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`;
|
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=${encodeURIComponent(waMessage)}`;
|
||||||
const waRes = await fetch(waUrl);
|
|
||||||
const waData = await waRes.json();
|
|
||||||
|
|
||||||
if (waData.status !== "success") {
|
try {
|
||||||
return NextResponse.json(
|
const waRes = await fetch(waUrl);
|
||||||
{ success: false, message: "Gagal mengirim OTP via WhatsApp" },
|
if (!waRes.ok) {
|
||||||
{ status: 400 }
|
console.warn(`⚠️ WA Service HTTP Error (Resend): ${waRes.status} ${waRes.statusText}. Continuing since OTP is logged.`);
|
||||||
);
|
} else {
|
||||||
|
const waData = await waRes.json();
|
||||||
|
console.log("📱 WA Response (Resend):", waData);
|
||||||
|
}
|
||||||
|
} catch (waError: any) {
|
||||||
|
console.warn("⚠️ WA Connection Exception (Resend). Continuing since OTP is logged.", waError.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simpan OTP ke database
|
// Simpan OTP ke database
|
||||||
|
|||||||
@@ -21,14 +21,21 @@ export async function POST(req: Request) {
|
|||||||
// Generate OTP
|
// Generate OTP
|
||||||
const codeOtp = randomOTP();
|
const codeOtp = randomOTP();
|
||||||
const otpNumber = Number(codeOtp);
|
const otpNumber = Number(codeOtp);
|
||||||
|
console.log(`🔑 DEBUG SEND-OTP-REGISTER [${nomor}]: ${codeOtp}`);
|
||||||
|
|
||||||
// Kirim WA
|
// Kirim WA
|
||||||
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=Website Desa Darmasaba - Kode verifikasi Anda: ${codeOtp}`;
|
const waUrl = `https://wa.wibudev.com/code?nom=${encodeURIComponent(nomor)}&text=Website Desa Darmasaba - Kode verifikasi Anda: ${codeOtp}`;
|
||||||
const res = await fetch(waUrl);
|
|
||||||
const sendWa = await res.json();
|
|
||||||
|
|
||||||
if (sendWa.status !== "success") {
|
try {
|
||||||
return NextResponse.json({ success: false, message: 'Gagal mengirim OTP' }, { status: 400 });
|
const res = await fetch(waUrl);
|
||||||
|
if (!res.ok) {
|
||||||
|
console.warn(`⚠️ WA Service HTTP Error (SendOTPRegister): ${res.status} ${res.statusText}. Continuing since OTP is logged.`);
|
||||||
|
} else {
|
||||||
|
const sendWa = await res.json();
|
||||||
|
console.log("📱 WA Response (SendOTPRegister):", sendWa);
|
||||||
|
}
|
||||||
|
} catch (waError: any) {
|
||||||
|
console.warn("⚠️ WA Connection Exception (SendOTPRegister). Continuing since OTP is logged.", waError.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simpan OTP
|
// Simpan OTP
|
||||||
|
|||||||
Reference in New Issue
Block a user