From 3943b3fc416fa41a0c3a85502adb8ba77108177b Mon Sep 17 00:00:00 2001 From: Pablo Alayeto <55535804+Pabl0cks@users.noreply.github.com> Date: Thu, 19 Sep 2024 22:51:01 +0200 Subject: [PATCH] Add "voter" role to /admin (#87) Co-authored-by: Damian --- .../app/admin/_components/SubmissionCard.tsx | 16 +++-- .../app/admin/_components/SubmissionTabs.tsx | 64 +++++++++++-------- packages/nextjs/app/admin/page.tsx | 2 +- .../[submissionId]/comments/route.ts | 2 +- .../[submissionId]/eligible/route.ts | 2 +- .../submissions/[submissionId]/votes/route.ts | 4 +- packages/nextjs/services/database/seed.ts | 1 + .../nextjs/types/next-auth/next-auth.d.ts | 1 + packages/nextjs/utils/auth.ts | 1 + 9 files changed, 58 insertions(+), 35 deletions(-) diff --git a/packages/nextjs/app/admin/_components/SubmissionCard.tsx b/packages/nextjs/app/admin/_components/SubmissionCard.tsx index 9b87443..0bb0ec3 100644 --- a/packages/nextjs/app/admin/_components/SubmissionCard.tsx +++ b/packages/nextjs/app/admin/_components/SubmissionCard.tsx @@ -55,6 +55,8 @@ export const SubmissionCard = ({ submission, tabName }: { submission: Submission const telegramUser = submission.telegram?.replace("@", ""); + const showResults = tabName === "all"; + return (
@@ -124,10 +126,16 @@ export const SubmissionCard = ({ submission, tabName }: { submission: Submission
-
-
{scoreAvg}
-
{submission.votes.length} votes
-
+ {showResults ? ( +
+
{scoreAvg}
+
{submission.votes.length} votes
+
+ ) : ( +
+
{submission.votes.length} votes
+
+ )}
diff --git a/packages/nextjs/app/admin/_components/SubmissionTabs.tsx b/packages/nextjs/app/admin/_components/SubmissionTabs.tsx index 903ad26..8297d21 100644 --- a/packages/nextjs/app/admin/_components/SubmissionTabs.tsx +++ b/packages/nextjs/app/admin/_components/SubmissionTabs.tsx @@ -1,12 +1,16 @@ "use client"; import { SubmissionCard } from "./SubmissionCard"; +import { useSession } from "next-auth/react"; import { useAccount } from "wagmi"; import { Submission, SubmissionWithAvg } from "~~/services/database/repositories/submissions"; const skeletonClasses = "animate-pulse bg-gray-200 rounded-none w-full h-96"; export const SubmissionTabs = ({ submissions }: { submissions: Submission[] }) => { + const { data: session } = useSession(); + const isAdmin = session?.user?.role === "admin"; + const { address: connectedAddress } = useAccount(); const { voted, notVoted, all } = submissions.reduce( @@ -61,15 +65,14 @@ export const SubmissionTabs = ({ submissions }: { submissions: Submission[] }) =
- ) : ( - notVoted.map(submission => { - return ; - }) - )} - {notVoted.length === 0 && ( + ) : notVoted.length === 0 ? (
There are no submissions to vote on.
+ ) : ( + notVoted.map(submission => ( + + )) )} @@ -84,34 +87,43 @@ export const SubmissionTabs = ({ submissions }: { submissions: Submission[] }) = />
- {voted.length === 0 && ( + {voted.length === 0 ? (
You have not voted on any submissions yet.
- )} - {voted - .sort((a, b) => b.avgScore - a.avgScore) - .map(submission => { - return ; - })} -
-
- - {/* All Submissions Tab */} - -
-
- {all.length === 0 ? ( -
- There are no submissions yet. -
) : ( - all + voted .sort((a, b) => b.avgScore - a.avgScore) - .map(submission => ) + .map(submission => ) )}
+ + {/* All Submissions Tab (only for admins) */} + {isAdmin && ( + <> + +
+
+ {all.length === 0 ? ( +
+ There are no submissions yet. +
+ ) : ( + all + .sort((a, b) => b.avgScore - a.avgScore) + .map(submission => ) + )} +
+
+ + )} ); diff --git a/packages/nextjs/app/admin/page.tsx b/packages/nextjs/app/admin/page.tsx index a7789d3..ec7d1ca 100644 --- a/packages/nextjs/app/admin/page.tsx +++ b/packages/nextjs/app/admin/page.tsx @@ -15,7 +15,7 @@ const Admin: NextPage = async () => { ); } - if (session?.user?.role !== "admin") { + if (!session?.user?.voter) { return
Access denied
; } diff --git a/packages/nextjs/app/api/submissions/[submissionId]/comments/route.ts b/packages/nextjs/app/api/submissions/[submissionId]/comments/route.ts index 24f484d..40604a2 100644 --- a/packages/nextjs/app/api/submissions/[submissionId]/comments/route.ts +++ b/packages/nextjs/app/api/submissions/[submissionId]/comments/route.ts @@ -7,7 +7,7 @@ export async function POST(req: NextRequest, { params }: { params: { submissionI try { const session = await getServerSession(authOptions); - if (session?.user.role !== "admin") { + if (!session?.user.voter) { return NextResponse.json({ error: "Only admins can add comments" }, { status: 401 }); } const { submissionId } = params; diff --git a/packages/nextjs/app/api/submissions/[submissionId]/eligible/route.ts b/packages/nextjs/app/api/submissions/[submissionId]/eligible/route.ts index 3b808a6..dae4c8b 100644 --- a/packages/nextjs/app/api/submissions/[submissionId]/eligible/route.ts +++ b/packages/nextjs/app/api/submissions/[submissionId]/eligible/route.ts @@ -7,7 +7,7 @@ export async function POST(req: NextRequest, { params }: { params: { submissionI try { const session = await getServerSession(authOptions); - if (session?.user.role !== "admin") { + if (!session?.user.voter) { return NextResponse.json({ error: "Only admins can set eligible" }, { status: 401 }); } const { submissionId } = params; diff --git a/packages/nextjs/app/api/submissions/[submissionId]/votes/route.ts b/packages/nextjs/app/api/submissions/[submissionId]/votes/route.ts index e761dcd..303fbfd 100644 --- a/packages/nextjs/app/api/submissions/[submissionId]/votes/route.ts +++ b/packages/nextjs/app/api/submissions/[submissionId]/votes/route.ts @@ -9,8 +9,8 @@ export async function POST(req: NextRequest, { params }: { params: { submissionI try { const session = await getServerSession(authOptions); - if (session?.user.role !== "admin") { - return NextResponse.json({ error: "Only admins can vote" }, { status: 401 }); + if (!session?.user.voter) { + return NextResponse.json({ error: "Only admins and voters can vote" }, { status: 401 }); } if (!votingEnabled) { diff --git a/packages/nextjs/services/database/seed.ts b/packages/nextjs/services/database/seed.ts index ed8bfa9..d8ad9c1 100644 --- a/packages/nextjs/services/database/seed.ts +++ b/packages/nextjs/services/database/seed.ts @@ -29,6 +29,7 @@ async function seed() { .values([ { id: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", role: "admin" }, { id: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", role: "user" }, + { id: "0x08fc7400ba37fc4ee1bf73bed5ddcb5db6a1036a", role: "voter" }, ]) .execute(); diff --git a/packages/nextjs/types/next-auth/next-auth.d.ts b/packages/nextjs/types/next-auth/next-auth.d.ts index 4aac85b..ec8d04e 100644 --- a/packages/nextjs/types/next-auth/next-auth.d.ts +++ b/packages/nextjs/types/next-auth/next-auth.d.ts @@ -7,6 +7,7 @@ declare module "next-auth" { user: { address?: string | null; role?: string | null; + voter?: boolean; }; expires: ISODateString; } diff --git a/packages/nextjs/utils/auth.ts b/packages/nextjs/utils/auth.ts index 1be6c87..7c61d50 100644 --- a/packages/nextjs/utils/auth.ts +++ b/packages/nextjs/utils/auth.ts @@ -72,6 +72,7 @@ export const authOptions: AuthOptions = { async session({ session, token }: { session: Session; token: JWT }) { session.user.address = token.sub; session.user.role = token.role; + session.user.voter = token.role ? ["admin", "voter"].includes(token.role) : false; return session; }, },