Fix voting

Deksripsi:
- Fix user server to API di tampilan utama voting
This commit is contained in:
2024-12-27 11:04:55 +08:00
parent 5f8a1c38d0
commit 924e994236
34 changed files with 1039 additions and 468 deletions

View File

@@ -1,5 +1,5 @@
import { prisma } from "@/app/lib";
import { newFunGetUserId } from "@/app/lib/new_fun_user_id";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
@@ -7,7 +7,7 @@ export const dynamic = "force-dynamic";
export async function GET(request: Request) {
try {
const userLoginId = await newFunGetUserId();
const userLoginId = await funGetUserIdByToken();
const count = await prisma.notifikasi.findMany({
where: {

View File

@@ -1,5 +1,5 @@
import { prisma } from "@/app/lib";
import { newFunGetUserId } from "@/app/lib/new_fun_user_id";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { ICategoryapp } from "@/app_modules/notifikasi/model/interface";
import backendLogger from "@/util/backendLogger";
import _ from "lodash";
@@ -13,7 +13,7 @@ export async function GET(request: Request) {
const category = searchParams.get("category") as ICategoryapp;
const page = searchParams.get("page");
const userLoginId = await newFunGetUserId();
const userLoginId = await funGetUserIdByToken();
const fixPage = _.toNumber(page);
const takeData = 10;
const skipData = fixPage * takeData - takeData;

View File

@@ -1,4 +1,4 @@
import { jwtVerify } from "jose";
import { decrypt } from "@/app/auth/_lib/decrypt";
import _ from "lodash";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
@@ -20,22 +20,3 @@ export async function GET() {
return NextResponse.json({ status: 200, message: "OK", data: dataUser });
}
async function decrypt({
token,
encodedKey,
}: {
token: string;
encodedKey: string;
}): Promise<Record<string, any> | null> {
try {
const enc = new TextEncoder().encode(encodedKey);
const { payload } = await jwtVerify(token, enc, {
algorithms: ["HS256"],
});
return (payload.user as Record<string, any>) || null;
} catch (error) {
console.error("Gagal verifikasi session", error);
return null;
}
}

View File

@@ -0,0 +1,312 @@
import { prisma } from "@/app/lib";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import backendLogger from "@/util/backendLogger";
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(request: Request) {
try {
let fixData;
const { searchParams } = new URL(request.url);
const search = searchParams.get("search");
const kategori = searchParams.get("kategori");
const status = searchParams.get("status");
const page = searchParams.get("page");
const takeData = 5;
const skipData = Number(page) * 5 - 5;
const userLoginId = await funGetUserIdByToken();
if (userLoginId == null) {
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan data, user id tidak ada",
},
{ status: 500 }
);
}
if (kategori == "beranda" && search != null && search != "") {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
updatedAt: "desc",
},
where: {
voting_StatusId: "1",
isArsip: false,
isActive: true,
akhirVote: {
gte: new Date(),
},
title: {
contains: search,
mode: "insensitive",
},
},
select: {
id: true,
title: true,
isActive: true,
createdAt: true,
updatedAt: true,
deskripsi: true,
awalVote: true,
akhirVote: true,
catatan: true,
authorId: true,
voting_StatusId: true,
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
include: {
Voting_Kontributor: {
include: {
Author: true,
},
},
},
},
Author: {
select: {
id: true,
username: true,
nomor: true,
Profile: true,
},
},
},
});
} else if (kategori == "beranda") {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
updatedAt: "desc",
},
where: {
voting_StatusId: "1",
isArsip: false,
isActive: true,
akhirVote: {
gte: new Date(),
},
title: {
// contains: search,
mode: "insensitive",
},
},
select: {
id: true,
title: true,
isActive: true,
createdAt: true,
updatedAt: true,
deskripsi: true,
awalVote: true,
akhirVote: true,
catatan: true,
authorId: true,
voting_StatusId: true,
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
include: {
Voting_Kontributor: {
include: {
Author: true,
},
},
},
},
Author: {
select: {
id: true,
username: true,
nomor: true,
Profile: true,
},
},
},
});
} else if (kategori == "status" && status == "1") {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
updatedAt: "desc",
},
where: {
voting_StatusId: status,
authorId: userLoginId as string,
isActive: true,
akhirVote: {
gte: new Date(),
},
},
include: {
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
},
},
});
} else if (kategori == "status" && status != "1") {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
updatedAt: "desc",
},
where: {
voting_StatusId: status,
authorId: userLoginId as string,
isActive: true,
},
});
} else if (kategori == "kontribusi") {
fixData = await prisma.voting_Kontributor.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "desc",
},
where: {
authorId: userLoginId,
},
select: {
id: true,
Voting: {
select: {
id: true,
title: true,
isActive: true,
awalVote: true,
akhirVote: true,
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
},
Author: {
select: {
Profile: true,
},
},
},
},
Voting_DaftarNamaVote: true,
Author: true,
},
});
} else if (kategori == "riwayat" && status == "1") {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "asc",
},
where: {
voting_StatusId: "1",
isActive: true,
akhirVote: {
lte: new Date(),
},
},
select: {
id: true,
title: true,
isActive: true,
createdAt: true,
updatedAt: true,
deskripsi: true,
awalVote: true,
akhirVote: true,
catatan: true,
authorId: true,
voting_StatusId: true,
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
},
Author: {
select: {
id: true,
username: true,
nomor: true,
Profile: true,
},
},
},
});
} else if (kategori == "riwayat" && status == "2") {
fixData = await prisma.voting.findMany({
take: takeData,
skip: skipData,
orderBy: {
createdAt: "asc",
},
where: {
voting_StatusId: "1",
authorId: userLoginId as string,
isActive: true,
akhirVote: {
lte: new Date(),
},
},
select: {
id: true,
title: true,
isActive: true,
createdAt: true,
updatedAt: true,
deskripsi: true,
awalVote: true,
akhirVote: true,
catatan: true,
authorId: true,
voting_StatusId: true,
Voting_DaftarNamaVote: {
orderBy: {
createdAt: "asc",
},
},
Author: {
select: {
id: true,
username: true,
nomor: true,
Profile: true,
},
},
},
});
}
return NextResponse.json(
{ success: true, message: "Berhasil mendapatkan data", data: fixData },
{ status: 200 }
);
} catch (error) {
backendLogger.error("Error get voting: ", error);
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan data",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,22 @@
import { prisma } from "@/app/lib";
import { NextResponse } from "next/server";
export async function GET(request: Request) {
try {
const data = await prisma.voting_Status.findMany();
return NextResponse.json({
success: true,
message: "Berhasil mendapatkan data",
data: data,
});
} catch (error) {
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan data",
reason: (error as Error).message,
},
{ status: 500 }
);
}
}

View File

@@ -1,9 +1,9 @@
import { newFunGetUserId } from "@/app/lib/new_fun_user_id";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { Event_Create } from "@/app_modules/event";
import { Event_getMasterTipeAcara } from "@/app_modules/event/fun/master/get_tipe_acara";
export default async function Page() {
const userLoginId = await newFunGetUserId();
const userLoginId = await funGetUserIdByToken();
const listTipeAcara = await Event_getMasterTipeAcara();
return (

View File

@@ -1,10 +1,10 @@
import { newFunGetUserId } from "@/app/lib/new_fun_user_id";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { Event_DetailMain } from "@/app_modules/event";
import { Event_countTotalPesertaById } from "@/app_modules/event/fun/count/count_total_peserta_by_id";
export default async function Page({ params }: { params: { id: string } }) {
let eventId = params.id;
const userLoginId = await newFunGetUserId();
const userLoginId = await funGetUserIdByToken();
const totalPeserta = await Event_countTotalPesertaById(eventId);
return (

View File

@@ -1,4 +1,4 @@
import { newFunGetUserId } from "@/app/lib/new_fun_user_id";
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import Ui_Konfirmasi from "@/app_modules/event/_ui/konfirmasi";
export default async function Page({
@@ -7,7 +7,7 @@ export default async function Page({
params: Promise<{ id: string }>;
}) {
const eventId = (await params).id;
const userLoginId = await newFunGetUserId();
const userLoginId = await funGetUserIdByToken();
return (
<>

View File

@@ -1,5 +1,5 @@
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { RealtimeProvider } from "../lib";
import { newFunGetUserId } from "../lib/new_fun_user_id";
import { ServerEnv } from "../lib/server_env";
export default async function Layout({
@@ -8,7 +8,7 @@ export default async function Layout({
children: React.ReactNode;
}) {
const userId = await newFunGetUserId();
const userId = await funGetUserIdByToken();
return (
<>

View File

@@ -1,12 +1,10 @@
import { Vote_Beranda } from "@/app_modules/vote";
import { vote_getAllListPublish } from "@/app_modules/vote/fun/get/get_all_list_publish";
export default async function Page() {
const dataVote = await vote_getAllListPublish({ page: 1 });
return (
<>
<Vote_Beranda dataVote={dataVote as any} />
<Vote_Beranda />
</>
);
}

View File

@@ -1,11 +1,9 @@
import { Vote_Kontribusi } from "@/app_modules/vote";
import { vote_getAllListKontribusiByAuthorId } from "@/app_modules/vote/fun/get/get_list_kontribusi_by_author_id";
export default async function Page() {
const dataKontribusi = await vote_getAllListKontribusiByAuthorId({ page: 1 });
return (
<>
<Vote_Kontribusi dataKontribusi={dataKontribusi as any} />
<Vote_Kontribusi />
</>
);
}

View File

@@ -1,41 +1,9 @@
import { Vote_Riwayat } from "@/app_modules/vote";
import { vote_getAllListRiwayat } from "@/app_modules/vote/fun/get/get_all_list_riwayat";
import { Vote_getAllListRiwayatSaya as vote_getAllListRiwayatSaya } from "@/app_modules/vote/fun/get/get_all_list_riwayat_saya";
export default async function Page({ params }: { params: { id: string } }) {
let statusRiwayatId = params.id;
const listRiwayat = await vote_getAllListRiwayat({ page: 1 });
const listRiwayatSaya = await vote_getAllListRiwayatSaya({ page: 1 });
if (statusRiwayatId == "1") {
return (
<>
<Vote_Riwayat
riwayatId={statusRiwayatId}
listRiwayat={listRiwayat as any}
/>
</>
);
}
if (statusRiwayatId == "2") {
return (
<>
<Vote_Riwayat
riwayatId={statusRiwayatId}
listRiwayatSaya={listRiwayatSaya as any}
/>
</>
);
}
// return (
// <>
// <Vote_Riwayat
// riwayatId={statusRiwayatId}
// listRiwayat={listRiwayat as any}
// listRiwayatSaya={listRiwayatSaya as any}
// />
// </>
// );
return (
<>
<Vote_Riwayat />
</>
);
}

View File

@@ -1,25 +1,9 @@
import { Vote_Status } from "@/app_modules/vote";
import {
vote_funGetAllByStatusId,
voting_getMasterStatus,
} from "@/app_modules/vote/fun";
export default async function Page({ params }: { params: { id: string } }) {
const statusId = params.id;
const listStatus = await voting_getMasterStatus();
const dataVoting = await vote_funGetAllByStatusId({
page: 1,
statusId: statusId,
});
export default async function Page() {
return (
<>
<Vote_Status
statusId={statusId}
dataVoting={dataVoting as any}
listStatus={listStatus as any}
/>
<Vote_Status />
</>
);
}

View File

@@ -1,21 +1,28 @@
"use server"
"use server";
import _ from "lodash";
import { cookies } from "next/headers";
import { decrypt } from "../auth/_lib/decrypt";
import backendLogger from "@/util/backendLogger";
export async function newFunGetUserId() {
const c = cookies().get(process.env.NEXT_PUBLIC_BASE_SESSION_KEY!);
try {
const key = process.env.NEXT_PUBLIC_BASE_SESSION_KEY;
const c = cookies().get("hipmi-key");
if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) {
if (!c || !c?.value || _.isEmpty(c?.value) || _.isUndefined(c?.value)) {
return null;
}
const token = c.value;
const dataUser = await decrypt({
token: token,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
});
return dataUser?.id;
} catch (error) {
backendLogger.log("Gagal mendapatkan user id", error);
return null;
}
const token = c.value;
const dataUser = await decrypt({
token: token,
encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
});
return dataUser?.id;
}
}

View File

@@ -0,0 +1,51 @@
"use client";
import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component";
import {
UIGlobal_LayoutHeaderTamplate,
UIGlobal_LayoutTamplate,
} from "@/app_modules/_global/ui";
import { Center, Grid, Group, Skeleton, Stack } from "@mantine/core";
export default function Voting_ComponentSkeletonViewPuh() {
return (
<>
<UIGlobal_LayoutTamplate
header={<UIGlobal_LayoutHeaderTamplate title="Detail Publish" />}
>
<ComponentGlobal_CardStyles marginBottom={"0"}>
<Stack spacing={"lg"}>
<Grid align="center">
<Grid.Col span={"content"}>
<Skeleton circle height={40} />
</Grid.Col>
<Grid.Col span={4}>
<Skeleton height={20} w={150} />
</Grid.Col>
</Grid>
<Stack align="center">
<Skeleton height={20} w={150} />
<Skeleton height={20} w={300} />
</Stack>
<Group position="center" spacing={100}>
<Stack align="center">
<Skeleton circle height={70} />
<Skeleton height={20} w={50} />
</Stack>
<Stack align="center">
<Skeleton circle height={70} />
<Skeleton height={20} w={50} />
</Stack>
</Group>
<Stack align="center">
<Skeleton height={15} w={50} /> <Skeleton height={20} w={50} />
</Stack>
</Stack>
</ComponentGlobal_CardStyles>
</UIGlobal_LayoutTamplate>
</>
);
}