Connect, collaborate, and grow with a community of TypeScript developers
chore: clean up challenges
bautistaaad60331bef9ff
diff --git a/prisma/challenges.mock.ts b/prisma/challenges.mock.tsindex 9ac3b7c7..af0b0fab 100644--- a/prisma/challenges.mock.ts+++ b/prisma/challenges.mock.ts@@ -1,4 +1,4 @@-import { type Difficulty, type Prisma } from '@prisma/client';+import { ChallengeStatus, type Difficulty, type Prisma } from '@prisma/client';import { gId, trashId } from './seed';import url from 'url';import { parse } from 'yaml';@@ -65,6 +65,7 @@ export async function loadChallengesFromTypeChallenge() {id: idNum,name: title,description: README,+ status: ChallengeStatus.ACTIVE,prompt: `// TEST CASE START\n${testData}\n\n// CODE START\n${prompt}`,difficulty: difficulty === 'warm' ? 'BEGINNER' : (difficulty.toUpperCase() as Difficulty),shortDescription: README.slice(0, 100),diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 2f37fba1..da7b3f5e 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -23,6 +23,7 @@ enum RoleTypes {USERADMINMODERATOR+ CREATOR}enum UserStatus {@@ -37,9 +38,10 @@ enum ReportStatus {DISMISSED}-enum Visibility {- VISIBLE- HIDDEN+enum ChallengeStatus {+ BANNED+ PENDING+ ACTIVE}// Necessary for Next auth@@ -86,7 +88,6 @@ model UserLink {createdAt DateTime @default(now())updatedAt DateTime @updatedAt-}model User {@@ -112,19 +113,15 @@ model User {vote Vote[]submission Submission[]- challengeReports ChallengeReport[] @relation(name: "authored_challenge_reports")- moderatedChallengeReports ChallengeReport[] @relation(name: "moderated_challenge_reports")-- bookmark Bookmark[]- sharedSolution SharedSolution[]- comment Comment[]+ bookmark Bookmark[]+ sharedSolution SharedSolution[]+ comment Comment[]createdAt DateTime @default(now())updatedAt DateTime @updatedAtreports Report[] @relation("reporter")reportsAbout Report[] @relation("reported_user")- moderated Report[] @relation("report_moderator")-+ moderated Report[] @relation("report_moderator")}model VerificationToken {@@ -136,29 +133,28 @@ model VerificationToken {}model Challenge {- id Int @id @default(autoincrement())- createdAt DateTime @default(now())- updatedAt DateTime @updatedAt+ id Int @id @default(autoincrement())+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAtdifficulty Difficultyname String/// this is the description to describe the challenge on description panel- description String @db.MediumText+ description String @db.MediumText/// this is a short blue thats shown on the explore cardshortDescription String/// this is the test cases + default starter code- prompt String @db.MediumText- visibility Visibility @default(VISIBLE)+ prompt String @db.MediumText+ status ChallengeStatus @default(PENDING)user User @relation(fields: [userId], references: [id])userId Stringvote Vote[]submission Submission[]- comment Comment[]- report ChallengeReport[]+ comment Comment[]bookmark Bookmark[]sharedSolution SharedSolution[]- Report Report[] @relation("report_challenge")+ Report Report[] @relation("report_challenge")@@index([userId])}@@ -182,14 +178,14 @@ model Vote {/// This is a public solution created by a user for a challengemodel SharedSolution {- id Int @id @default(autoincrement())- createdAt DateTime @default(now())+ id Int @id @default(autoincrement())+ createdAt DateTime @default(now())title String- description String @db.MediumText+ description String @db.MediumTextvote Vote[]- user User? @relation(fields: [userId], references: [id])+ user User? @relation(fields: [userId], references: [id])userId String?- challenge Challenge? @relation(fields: [challengeId], references: [id])+ challenge Challenge? @relation(fields: [challengeId], references: [id])challengeId Int?solutionComment Comment[]Report Report[]@@ -214,36 +210,6 @@ model Submission {@@index([challengeId])}-model ChallengeReport {- id Int @id @default(autoincrement())-- // meta information- text String @db.MediumText- status ReportStatus @default(PENDING)-- // report types- derogatory Boolean @default(false)- unclear Boolean @default(false)-- // user infor- author User @relation(fields: [authorId], references: [id], name: "authored_challenge_reports")- authorId String- moderator User? @relation(fields: [moderatorId], references: [id], name: "moderated_challenge_reports")- moderatorId String?-- // attached challenge.- challenge Challenge @relation(fields: [challengeId], references: [id])- challengeId Int-- // time- createdAt DateTime @default(now())- updatedAt DateTime @updatedAt-- @@index([authorId])- @@index([challengeId])- @@index([moderatorId])-}-model Bookmark {id Int @id @default(autoincrement())user User @relation(fields: [userId], references: [id])@@ -311,7 +277,7 @@ model Report {solution SharedSolution? @relation(fields: [solutionId], references: [id])moderatorId String?- moderator User? @relation("report_moderator", fields: [moderatorId], references: [id])+ moderator User? @relation("report_moderator", fields: [moderatorId], references: [id])createdAt DateTime @default(now())updatedAt DateTime @updatedAtdiff --git a/src/app/admin/report/[id]/actions.tsx b/src/app/admin/report/[id]/actions.tsxindex 93b19232..7874bcd0 100644--- a/src/app/admin/report/[id]/actions.tsx+++ b/src/app/admin/report/[id]/actions.tsx@@ -15,7 +15,7 @@ import {banUser,deleteComment,deleteSolution,- disableChallenge,+ banChallenge,dismissReport,} from '~/components/admin/admin.actions';import { Button } from '~/components/ui/button';@@ -59,7 +59,7 @@ export default function ReportActions({ report }: ReportActionsProps) {title: 'Success',description: <p>Comment deleted successfully</p>,});- router.push('/admin/report');+ router.push('/admin');} catch (e) {toast({title: 'Error',@@ -71,8 +71,8 @@ export default function ReportActions({ report }: ReportActionsProps) {async function handleDisableChallenge(challengeId: number, reportId: number) {try {- await disableChallenge(challengeId, reportId);- router.push('/admin/report');+ await banChallenge(challengeId, reportId);+ router.push('/admin');} catch (e) {toast({title: 'Error',diff --git a/src/app/challenge/[id]/getChallengeRouteData.ts b/src/app/challenge/[id]/getChallengeRouteData.tsindex 67107b14..690d6f5a 100644--- a/src/app/challenge/[id]/getChallengeRouteData.ts+++ b/src/app/challenge/[id]/getChallengeRouteData.ts@@ -6,15 +6,16 @@ import { prisma } from '~/server/db';export type ChallengeRouteData = NonNullable<Awaited<ReturnType<typeof getChallengeRouteData>>>;// this is to data to populate the description tab (default tab on challenge page)-export const getChallengeRouteData = cache(async (id: string, session: Session | null) => {- const challenge = await prisma.challenge.findFirst({+export const getChallengeRouteData = cache((id: string, session: Session | null) => {+ return prisma.challenge.findFirstOrThrow({where: {id: +id,+ status: 'ACTIVE',user: {NOT: {- status: 'BANNED'- }- }+ status: 'BANNED',+ },+ },},include: {user: true,@@ -47,6 +48,4 @@ export const getChallengeRouteData = cache(async (id: string, session: Session |},},});-- return challenge;});diff --git a/src/app/challenge/[id]/layout.tsx b/src/app/challenge/[id]/layout.tsxindex 6da2facb..cb5a7d4d 100644--- a/src/app/challenge/[id]/layout.tsx+++ b/src/app/challenge/[id]/layout.tsx@@ -1,9 +1,8 @@import { ChallengeLayout } from '~/components/challenge/challenge-layout';-import { Wrapper } from './wrapper';-import { getChallengeRouteData } from './getChallengeRouteData';import { getServerAuthSession } from '~/server/auth';+import { getChallengeRouteData } from './getChallengeRouteData';import { LeftWrapper } from './left-wrapper';-import { redirect } from 'next/navigation';+import { Wrapper } from './wrapper';export default async function LayoutData({children,@@ -15,8 +14,6 @@ export default async function LayoutData({const session = await getServerAuthSession();const challenge = await getChallengeRouteData(id, session);- if (!challenge) return redirect('/explore');-return (<ChallengeLayoutleft={<LeftWrapper challengeId={challenge.id}>{children}</LeftWrapper>}diff --git a/src/app/challenge/error.tsx b/src/app/challenge/error.tsxnew file mode 100644index 00000000..2712ca8d--- /dev/null+++ b/src/app/challenge/error.tsx@@ -0,0 +1,18 @@+'use client'; // Error components must be Client Components++import Link from 'next/link';+import { Button } from '~/components/ui/button';+import Text from '~/components/ui/typography/typography';++export default function Error() {+ return (+ <div className="container flex h-full flex-col items-center justify-center">+ <Text intent="h2" className="mb-6">+ Uh oh! We couldn't find the challenge you were looking for.+ </Text>+ <Link href="/explore">+ <Button>Explore Challenges</Button>+ </Link>+ </div>+ );+}diff --git a/src/components/admin/admin.actions.tsx b/src/components/admin/admin.actions.tsxindex 3431ea61..c5dedfab 100644--- a/src/components/admin/admin.actions.tsx+++ b/src/components/admin/admin.actions.tsx@@ -4,8 +4,6 @@ import { type Report, type Prisma } from '@prisma/client';import { getServerAuthSession } from '~/server/auth';import { prisma } from '~/server/db';-export type AdminReportDetails = Awaited<ReturnType<typeof getChallengeReports>>;-// FML this was obnoxious to doexport type ChallengeInfo = { type: 'CHALLENGE' } & Omit<Report,@@ -36,24 +34,6 @@ export async function addReport(});}-/**- * The function fetches all the reports along- * with challenge and the user.- */-export async function getChallengeReports() {- return prisma.challengeReport.findMany({- include: {- challenge: {- include: {- user: true,- },- },- author: true,- moderator: true,- },- });-}-export type AdminBannedUsers = Awaited<ReturnType<typeof getBannedUsers>>;export async function deleteComment(commentId: number, reportId: number) {@@ -94,7 +74,7 @@ export async function getBannedUsers() {* @param reportId The id of the report.* @returns*/-export async function disableChallenge(challengeId: number, reportId: number) {+export async function banChallenge(challengeId: number, reportId: number) {const session = await getServerAuthSession();await prisma.$transaction([prisma.challenge.update({@@ -102,7 +82,7 @@ export async function disableChallenge(challengeId: number, reportId: number) {id: challengeId,},data: {- visibility: 'HIDDEN',+ status: 'BANNED',},}),prisma.report.update({@@ -187,7 +167,7 @@ export async function banUser(userId: string, reportId: number, banReason?: striuserId: userId,},data: {- visibility: 'HIDDEN',+ status: 'BANNED',},}),prisma.session.deleteMany({@@ -220,7 +200,7 @@ export async function unbanUser(userId: string) {userId: userId,},data: {- visibility: 'VISIBLE',+ status: 'ACTIVE',},}),prisma.user.update({@@ -245,7 +225,7 @@ export async function getChallenge(id: number) {vote: true,bookmark: true,comment: true,- }+ },},},});diff --git a/src/components/explore/index.tsx b/src/components/explore/index.tsxindex 5edd0b08..ef0dcabb 100644--- a/src/components/explore/index.tsx+++ b/src/components/explore/index.tsx@@ -35,13 +35,13 @@ export type ExploreChallengeData = Awaited<ReturnType<typeof getEasyChallenges>>async function getEasyChallenges() {return prisma.challenge.findMany({where: {- visibility: 'VISIBLE',+ status: 'ACTIVE',difficulty: { in: ['EASY'] },user: {NOT: {- status: 'BANNED'- }- }+ status: 'BANNED',+ },+ },},include: {_count: {@@ -56,13 +56,13 @@ async function getEasyChallenges() {async function getMediumChallenges() {return prisma.challenge.findMany({where: {- visibility: 'VISIBLE',+ status: 'ACTIVE',difficulty: { in: ['MEDIUM'] },user: {NOT: {- status: 'BANNED'- }- }+ status: 'BANNED',+ },+ },},include: {_count: {@@ -77,13 +77,13 @@ async function getMediumChallenges() {async function getHardChallenges() {return prisma.challenge.findMany({where: {- visibility: 'VISIBLE',+ status: 'ACTIVE',difficulty: { in: ['HARD'] },user: {NOT: {- status: 'BANNED'- }- }+ status: 'BANNED',+ },+ },},include: {_count: {diff --git a/src/components/report/report.action.ts b/src/components/report/report.action.tsindex 6cfe8552..8bf7ca48 100644--- a/src/components/report/report.action.ts+++ b/src/components/report/report.action.ts@@ -178,32 +178,6 @@ export async function getReports(lastCursor?: number, take = 25) {});}-export async function addChallengeReport(challengeId: number, userId: string, data: FormValues) {- if (userId === undefined) return 'not_logged_in';-- const report = await prisma.challengeReport.findMany({- where: {- challengeId,- authorId: userId,- },- });- if (report.length > 0) {- return 'report_already_made';- }-- await prisma.challengeReport.create({- data: {- challengeId,- authorId: userId,- text: data.comments,- unclear: data.examples,- derogatory: data.derogatory,- },- });-- return 'created';-}-export async function getReportedUserInformation(userId: string) {return await prisma.user.findFirstOrThrow({where: {