Fix UI Admin Menu Kesehatan, Login Admin, OTP
This commit is contained in:
@@ -1,30 +1,64 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function artikelKesehatanFindMany(context: Context) {
|
||||
// Ambil parameter dari query
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const search = (context.query.search as string) || "";
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Buat where clause
|
||||
const where: any = { isActive: true };
|
||||
|
||||
// Tambahkan pencarian (jika ada)
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ title: { contains: search, mode: "insensitive" } },
|
||||
{ introduction: { content: { contains: search, mode: "insensitive" } } },
|
||||
{ symptom: { title: { contains: search, mode: "insensitive" } } },
|
||||
{ prevention: { title: { contains: search, mode: "insensitive" } } },
|
||||
{ firstaid: { title: { contains: search, mode: "insensitive" } } },
|
||||
{ mythvsfact: { title: { contains: search, mode: "insensitive" } } },
|
||||
{ doctorsign: { content: { contains: search, mode: "insensitive" } } },
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil data dan total count secara paralel
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.artikelKesehatan.findMany({
|
||||
where,
|
||||
include: {
|
||||
introduction: true,
|
||||
symptom: true,
|
||||
prevention: true,
|
||||
firstaid: true,
|
||||
mythvsfact: true,
|
||||
doctorsign: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
prisma.artikelKesehatan.count({ where }),
|
||||
]);
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil ambil keamanan lingkungan dengan pagination",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error di findMany paginated:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data keamanan lingkungan",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default async function artikelKesehatanFindMany() {
|
||||
try {
|
||||
const data = await prisma.artikelKesehatan.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
},
|
||||
include: {
|
||||
introduction: true,
|
||||
symptom: true,
|
||||
prevention: true,
|
||||
firstaid: true,
|
||||
mythvsfact: true,
|
||||
doctorsign: true,
|
||||
}
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch artikel kesehatan",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find many error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch artikel kesehatan",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,35 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// /api/berita/findManyPaginated.ts
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function findManyFasilitasKesehatan() {
|
||||
try {
|
||||
const data = await prisma.fasilitasKesehatan.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
},
|
||||
async function fasilitasKesehatanFindMany(context: Context) {
|
||||
// Ambil parameter dari query
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const search = (context.query.search as string) || '';
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Buat where clause
|
||||
const where: any = { isActive: true };
|
||||
|
||||
// Tambahkan pencarian (jika ada)
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ informasiUmum: { fasilitas: { contains: search, mode: 'insensitive' } } },
|
||||
{ layananUnggulan: { content: { contains: search, mode: 'insensitive' } } },
|
||||
{ dokterdanTenagaMedis: { name: { contains: search, mode: 'insensitive' } } },
|
||||
{ fasilitasPendukung: { content: { contains: search, mode: 'insensitive' } } },
|
||||
{ prosedurPendaftaran: { content: { contains: search, mode: 'insensitive' } } },
|
||||
{ tarifdanlayanan: { layanan: { contains: search, mode: 'insensitive' } } },
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil data dan total count secara paralel
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.fasilitasKesehatan.findMany({
|
||||
where,
|
||||
include: {
|
||||
informasiumum: true,
|
||||
layananunggulan: true,
|
||||
@@ -13,18 +37,29 @@ export default async function findManyFasilitasKesehatan() {
|
||||
fasilitaspendukung: true,
|
||||
prosedurpendaftaran: true,
|
||||
tarifdanlayanan: true,
|
||||
}
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch fasilitas kesehatan",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find many error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch fasilitas kesehatan",
|
||||
}
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
prisma.fasilitasKesehatan.count({ where }),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil ambil keamanan lingkungan dengan pagination",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error di findMany paginated:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengambil data keamanan lingkungan",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
export default fasilitasKesehatanFindMany
|
||||
@@ -1,30 +1,60 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function jadwalKegiatanFindMany() {
|
||||
try {
|
||||
const data = await prisma.jadwalKegiatan.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
},
|
||||
include: {
|
||||
informasijadwalkegiatan: true,
|
||||
deskripsijadwalkegiatan: true,
|
||||
layananjadwalkegiatan: true,
|
||||
syaratketentuanjadwalkegiatan: true,
|
||||
dokumenjadwalkegiatan: true,
|
||||
pendaftaranjadwalkegiatan: true,
|
||||
}
|
||||
})
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch jadwal kegiatan",
|
||||
data,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Find many error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch jadwal kegiatan",
|
||||
}
|
||||
}
|
||||
}
|
||||
export default async function jadwalKegiatanFindMany(context: Context) {
|
||||
// Ambil parameter dari query
|
||||
const page = Number(context.query.page) || 1;
|
||||
const limit = Number(context.query.limit) || 10;
|
||||
const search = (context.query.search as string) || "";
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
// Buat where clause
|
||||
const where: any = { isActive: true };
|
||||
|
||||
// Tambahkan pencarian (jika ada)
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ informasijadwalkegiatan: { name: { contains: search, mode: "insensitive" } } },
|
||||
{ deskripsijadwalkegiatan: { deskripsi: { contains: search, mode: "insensitive" } } },
|
||||
{layananjadwalkegiatan: { content: { contains: search, mode: "insensitive" } } },
|
||||
{syaratketentuanjadwalkegiatan: { content: { contains: search, mode: "insensitive" } } },
|
||||
{dokumenjadwalkegiatan: { content: { contains: search, mode: "insensitive" } } },
|
||||
{pendaftaranjadwalkegiatan: { content: { contains: search, mode: "insensitive" } } },
|
||||
];
|
||||
}
|
||||
try {
|
||||
const [data, total] = await Promise.all([
|
||||
prisma.jadwalKegiatan.findMany({
|
||||
where,
|
||||
include: {
|
||||
informasijadwalkegiatan: true,
|
||||
deskripsijadwalkegiatan: true,
|
||||
layananjadwalkegiatan: true,
|
||||
syaratketentuanjadwalkegiatan: true,
|
||||
dokumenjadwalkegiatan: true,
|
||||
pendaftaranjadwalkegiatan: true,
|
||||
},
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
prisma.jadwalKegiatan.count({ where }),
|
||||
]);
|
||||
return {
|
||||
success: true,
|
||||
message: "Success fetch jadwal kegiatan",
|
||||
data,
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Find many error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed fetch jadwal kegiatan",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Elysia, t } from "elysia";
|
||||
import userFindMany from "./findMany";
|
||||
import userFindUnique from "./findUnique";
|
||||
import userDelete from "./del"; // `delete` nggak boleh jadi nama file JS langsung, jadi biasanya `del.ts`
|
||||
import userUpdate from "./updt";
|
||||
|
||||
const User = new Elysia({ prefix: "/api/user" })
|
||||
.get("/findMany", userFindMany)
|
||||
@@ -12,6 +13,7 @@ const User = new Elysia({ prefix: "/api/user" })
|
||||
params: t.Object({
|
||||
id: t.String(),
|
||||
}),
|
||||
}); // pakai PUT untuk soft delete
|
||||
}) // pakai PUT untuk soft delete
|
||||
.put("/updt", userUpdate);
|
||||
|
||||
export default User;
|
||||
|
||||
40
src/app/api/[[...slugs]]/_lib/user/updt.ts
Normal file
40
src/app/api/[[...slugs]]/_lib/user/updt.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export default async function userUpdate(context: Context) {
|
||||
try {
|
||||
const { id, isActive } = await context.body as { id: string, isActive: boolean };
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: "ID user wajib ada",
|
||||
};
|
||||
}
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: { id },
|
||||
data: { isActive },
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
nomor: true,
|
||||
isActive: true,
|
||||
updatedAt: true,
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `User berhasil ${isActive ? "diaktifkan" : "dinonaktifkan"}`,
|
||||
data: updatedUser,
|
||||
};
|
||||
} catch (e: any) {
|
||||
console.error("Error update user:", e);
|
||||
return {
|
||||
success: false,
|
||||
message: "Gagal mengupdate status user",
|
||||
};
|
||||
}
|
||||
}
|
||||
50
src/app/api/auth/_lib/decrypt.ts
Normal file
50
src/app/api/auth/_lib/decrypt.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { jwtVerify } from "jose";
|
||||
|
||||
export async function decrypt({
|
||||
token,
|
||||
encodedKey,
|
||||
}: {
|
||||
token: string;
|
||||
encodedKey: string;
|
||||
}): Promise<Record<string, any> | null> {
|
||||
if (!token || !encodedKey) {
|
||||
console.error("Missing required parameters:", {
|
||||
hasToken: !!token,
|
||||
hasEncodedKey: !!encodedKey,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const enc = new TextEncoder().encode(encodedKey);
|
||||
const { payload } = await jwtVerify(token, enc, {
|
||||
algorithms: ["HS256"],
|
||||
});
|
||||
|
||||
if (!payload || !payload.user) {
|
||||
console.error("Invalid payload structure:", {
|
||||
hasPayload: !!payload,
|
||||
hasUser: payload ? !!payload.user : false,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
// Logging untuk debug
|
||||
// console.log("Decrypt successful:", {
|
||||
// payloadExists: !!payload,
|
||||
// userExists: !!payload.user,
|
||||
// tokenPreview: token.substring(0, 10) + "...",
|
||||
// });
|
||||
|
||||
return payload.user as Record<string, any>;
|
||||
} catch (error) {
|
||||
console.error("Token verification failed:", {
|
||||
error,
|
||||
tokenLength: token?.length,
|
||||
errorName: error instanceof Error ? error.name : "Unknown error",
|
||||
errorMessage: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
26
src/app/api/auth/_lib/encrypt.ts
Normal file
26
src/app/api/auth/_lib/encrypt.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { SignJWT } from "jose";
|
||||
|
||||
export async function encrypt({
|
||||
user,
|
||||
exp = "7 year",
|
||||
encodedKey,
|
||||
}: {
|
||||
user: Record<string, any>;
|
||||
exp?: string;
|
||||
encodedKey: string;
|
||||
}): Promise<string | null> {
|
||||
try {
|
||||
const enc = new TextEncoder().encode(encodedKey);
|
||||
return new SignJWT({ user })
|
||||
.setProtectedHeader({ alg: "HS256" })
|
||||
.setIssuedAt()
|
||||
.setExpirationTime(exp)
|
||||
.sign(enc);
|
||||
} catch (error) {
|
||||
console.error("Gagal mengenkripsi", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// wibu:0.2.82
|
||||
13
src/app/api/auth/_lib/get_KodeOtp_By_Id.ts
Normal file
13
src/app/api/auth/_lib/get_KodeOtp_By_Id.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
"use server";
|
||||
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function auth_getCodeOtpByNumber({kodeId}: {kodeId: string}) {
|
||||
const data = await prisma.kodeOtp.findFirst({
|
||||
where: {
|
||||
id: kodeId,
|
||||
},
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
4
src/app/api/auth/_lib/randomOTP.ts
Normal file
4
src/app/api/auth/_lib/randomOTP.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function randomOTP() {
|
||||
const random = Math.floor(Math.random() * (9000 - 1000 )) + 1000
|
||||
return random;
|
||||
}
|
||||
36
src/app/api/auth/_lib/session_create.ts
Normal file
36
src/app/api/auth/_lib/session_create.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { cookies } from "next/headers";
|
||||
import { encrypt } from "./encrypt";
|
||||
|
||||
export async function sessionCreate({
|
||||
sessionKey,
|
||||
exp = "7 year",
|
||||
encodedKey,
|
||||
user,
|
||||
}: {
|
||||
sessionKey: string;
|
||||
exp?: string;
|
||||
encodedKey: string;
|
||||
user: Record<string, unknown>;
|
||||
}) {
|
||||
const token = await encrypt({
|
||||
exp,
|
||||
encodedKey,
|
||||
user,
|
||||
});
|
||||
|
||||
const cookie: any = {
|
||||
key: sessionKey,
|
||||
value: token,
|
||||
options: {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
},
|
||||
};
|
||||
|
||||
(await cookies()).set(cookie.key, cookie.value, { ...cookie.options });
|
||||
return token;
|
||||
}
|
||||
|
||||
// wibu:0.2.82
|
||||
63
src/app/api/auth/login/route.ts
Normal file
63
src/app/api/auth/login/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomOTP } from "../_lib/randomOTP";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
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;
|
||||
const res = await fetch(
|
||||
`https://wa.wibudev.com/code?nom=${nomor}&text=Website Desa Darmasaba - Kode ini bersifat RAHASIA dan JANGAN DI BAGIKAN KEPADA SIAPAPUN, termasuk anggota ataupun Admin 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 mengirim kode OTP" },
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Kode verifikasi terkirim",
|
||||
kodeId: createOtpId.id,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("Error Login", error);
|
||||
return NextResponse.json(
|
||||
{ success: false, message: "Terjadi masalah saat login" , reason: error as Error },
|
||||
{ status: 500 }
|
||||
);
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user