Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

12.9 KB
Raw Blame
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

datasource db {
  provider     = "mysql"
  url          = env("DATABASE_URL")
  relationMode = "prisma"
}

enum NotificationType {
  LIKE
  REPLY
  MENTION
  MISC
  SYSTEM
}

enum Tags {
  POPULAR
  NEWEST
}

enum Difficulty {
  BEGINNER
  EASY
  MEDIUM
  HARD
  EXTREME
  EVENT
}

enum RoleTypes {
  USER
  ADMIN
  MODERATOR
  CREATOR
  SUPPORTER
  CONTRIBUTOR
}

enum UserStatus {
  BANNED
  ACTIVE // can post, comment, vote, etc
}

enum ReportStatus {
  PENDING
  CLEARED
  DISMISSED
}

enum ChallengeStatus {
  DECLINED
  BANNED
  PENDING
  ACTIVE
}

// Necessary for Next auth
model Account {
  id                String  @id @default(cuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? // @db.Text
  access_token      String? // @db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? // @db.Text
  session_state     String?
  user              User    @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
  @@index([userId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId])
}

model Role {
  id   String    @id @default(uuid())
  user User[]
  role RoleTypes @unique
}

/// This is a table that contains all the user links they can add to profile
model UserLink {
  id   String @id @default(uuid())
  user User[]
  url  String @db.VarChar(256)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model User {
  id String @id @default(uuid())

  name          String
  email         String    @unique
  emailVerified DateTime?
  image         String?
  roles         Role[]

  /// this is the bio that shows up on the profile page
  bio       String     @default("") @db.VarChar(1000)
  /// this is the twitter URL that shows up on the profile page
  userLinks UserLink[]

  status    UserStatus @default(ACTIVE)
  banReason String?    @db.MediumText

  accounts   Account[]
  sessions   Session[]
  challenge  Challenge[]
  vote       Vote[]
  submission Submission[]

  bookmark       Bookmark[]
  sharedSolution SharedSolution[]
  comment        Comment[]

  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
  reports      Report[] @relation("reporter")
  reportsAbout Report[] @relation("reported_user")
  moderated    Report[] @relation("report_moderator")

  imageUpload          ImageUpload[]
  tracks               Track[]
  BetaTokens           BetaTokens[]
  donations            Donation[]
  shortUrls            ShortURL[]
  toUserNotification   Notification[] @relation("notificationToUser")
  fromUserNotification Notification[] @relation("notificationFromUser")
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

model Tag {
  id         String      @id @default(uuid())
  tag        Tags        @unique
  challenges Challenge[]
}

model Challenge {
  id               Int             @id @default(autoincrement())
  createdAt        DateTime        @default(now())
  updatedAt        DateTime        @updatedAt
  difficulty       Difficulty
  name             String          @unique
  slug             String          @unique
  /// this is the description to describe the challenge on description panel
  description      String          @db.MediumText
  /// this is a short blue thats shown on the explore card
  shortDescription String
  /// Starter code for the challenge
  code             String          @db.MediumText
  /// Test cases that need to be passed in order have the submission be accepted.
  tests            String          @db.MediumText
  status           ChallengeStatus @default(PENDING)
  tsconfig         Json?

  user   User   @relation(fields: [userId], references: [id])
  userId String

  tags           Tag[]
  vote           Vote[]
  submission     Submission[]
  comment        Comment[]
  bookmark       Bookmark[]
  sharedSolution SharedSolution[]
  Report         Report[]         @relation("report_challenge")
  TrackChallenge TrackChallenge[]
  Notification   Notification[]

  @@index([userId])
}

enum VoteType {
  CHALLENGE
  COMMENT
  SUBMISSION
  SHAREDSOLUTION
}

model Vote {
  id       Int      @id @default(autoincrement())
  user     User     @relation(fields: [userId], references: [id])
  userId   String
  rootType VoteType

  challenge   Challenge? @relation(fields: [challengeId], references: [id])
  challengeId Int?

  comment   Comment? @relation(fields: [commentId], references: [id])
  commentId Int?

  submission   Submission? @relation(fields: [submissionId], references: [id])
  submissionId Int?

  sharedSolution   SharedSolution? @relation(fields: [sharedSolutionId], references: [id])
  sharedSolutionId Int?

  @@index([userId])
  @@index([challengeId])
  @@index([commentId])
  @@index([submissionId])
  @@index([sharedSolutionId])
}

/// This is a public solution created by a user for a challenge
model SharedSolution {
  id              Int            @id @default(autoincrement())
  createdAt       DateTime       @default(now())
  isPinned        Boolean        @default(false)
  title           String
  description     String         @db.MediumText
  vote            Vote[]
  user            User?          @relation(fields: [userId], references: [id])
  userId          String?
  challenge       Challenge?     @relation(fields: [challengeId], references: [id])
  challengeId     Int?
  solutionComment Comment[]
  Report          Report[]
  Notification    Notification[]

  @@index([userId])
  @@index([challengeId])
}

/// Every attempt a user makes to a challenge is stored here
model Submission {
  id           Int       @id @default(autoincrement())
  code         String    @db.MediumText
  createdAt    DateTime  @default(now())
  isSuccessful Boolean
  user         User      @relation(fields: [userId], references: [id])
  userId       String
  challenge    Challenge @relation(fields: [challengeId], references: [id])
  challengeId  Int
  vote         Vote[]

  @@index([userId])
  @@index([challengeId])
}

model Bookmark {
  id          Int        @id @default(autoincrement())
  user        User       @relation(fields: [userId], references: [id])
  userId      String
  challenge   Challenge? @relation(fields: [challengeId], references: [id])
  challengeId Int?

  createdAt DateTime? @default(now())

  @@unique([userId, challengeId])
  @@index([userId])
  @@index([challengeId])
}

enum ReportType {
  CHALLENGE
  USER
  COMMENT
  SOLUTION
}

enum IssueType {
  DEROGATORY
  OTHER
  UNCLEAR
  BULLYING
  SPAM
  HATESPEECH
  THREAT
}

model ReportIssue {
  id       Int       @id @default(autoincrement())
  reportId Int
  report   Report    @relation(fields: [reportId], references: [id])
  type     IssueType

  @@unique([id])
  @@index([reportId])
}

model Report {
  // Base table info
  id Int @id @default(autoincrement())

  text String @db.MediumText

  type   ReportType
  status ReportStatus
  issues ReportIssue[]

  // Reporter information
  reporterId String
  reporter   User   @relation("reporter", fields: [reporterId], references: [id])

  // reported User Info
  userId String?
  user   User?   @relation("reported_user", fields: [userId], references: [id])

  // reported Challenge Info
  challengeId Int?
  challenge   Challenge? @relation("report_challenge", fields: [challengeId], references: [id])

  commentId Int?
  comment   Comment? @relation("report_comment", fields: [commentId], references: [id])

  solutionId Int?
  solution   SharedSolution? @relation(fields: [solutionId], references: [id])

  moderatorId String?
  moderator   User?   @relation("report_moderator", fields: [moderatorId], references: [id])

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([id])
  @@index([commentId])
  @@index([reporterId])
  @@index([userId])
  @@index([challengeId])
  @@index([solutionId])
  @@index([moderatorId])
}

enum CommentRoot {
  SOLUTION
  CHALLENGE
}

model Comment {
  id            Int       @id @default(autoincrement())
  text          String    @db.MediumText
  visible       Boolean   @default(true)
  parentId      Int?
  parentComment Comment?  @relation("reply_comments", fields: [parentId], references: [id], onUpdate: NoAction, onDelete: NoAction)
  replies       Comment[] @relation("reply_comments")

  userId String
  user   User   @relation(fields: [userId], references: [id])

  rootType CommentRoot @default(CHALLENGE)

  rootChallengeId Int?
  rootChallenge   Challenge? @relation(fields: [rootChallengeId], references: [id])

  rootSolutionId Int?
  rootSolution   SharedSolution? @relation(fields: [rootSolutionId], references: [id])
  Report         Report[]        @relation("report_comment")

  createdAt    DateTime       @default(now())
  updatedAt    DateTime       @updatedAt
  vote         Vote[]
  Notification Notification[]

  @@index([rootSolutionId])
  @@index([rootChallengeId])
  @@index([parentId])
  @@index([userId])
}

model ImageUpload {
  /// the id of the db row
  id String @id @default(uuid())

  /// the user id fk
  userId String

  /// The user that this is related to
  user User @relation(fields: [userId], references: [id])

  /// the url to the s3 uploaded assets
  url String @db.VarChar(256)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([userId])
}

model TrackChallenge {
  id          Int       @id @default(autoincrement())
  orderId     Int
  challengeId Int
  challenge   Challenge @relation(fields: [challengeId], references: [id])
  track       Track?    @relation(fields: [trackId], references: [id])
  trackId     Int?

  @@index([challengeId])
  @@index([trackId])
}

model Track {
  id              Int              @id @default(autoincrement())
  name            String           @unique
  slug            String           @unique
  description     String           @db.MediumText
  /// determines if a track can be seen on tracks page
  visible         Boolean          @default(true)
  /// use this to display coming soon UI
  isComingSoon    Boolean          @default(false)
  trackChallenges TrackChallenge[]
  enrolledUsers   User[]
}

model BetaTokens {
  id     Int     @id @default(autoincrement())
  token  String  @unique
  user   User?   @relation(fields: [userId], references: [id])
  userId String?

  @@index([userId])
}

model Donation {
  id                      Int     @id @default(autoincrement())
  user                    User    @relation(fields: [userId], references: [id])
  userId                  String
  amount                  Int
  stripeCheckoutSessionId String  @unique
  createdAt DateTime? @default(now())

  @@index([userId])
}

model Notification {
  id               Int              @id @default(autoincrement())
  createdAt        DateTime         @default(now())
  updatedAt        DateTime         @updatedAt
  isRead           Boolean          @default(false)
  type             NotificationType
  toUser           User             @relation("notificationToUser", fields: [toUserId], references: [id])
  toUserId         String
  fromUser         User             @relation("notificationFromUser", fields: [fromUserId], references: [id])
  fromUserId       String
  commentId        Int?
  comment          Comment?         @relation(fields: [commentId], references: [id])
  /// could be comment text or solution title to avoid having to join to grab this data
  content          String?
  /// can be used to replace the blurb (eg: mention you in a comment)
  blurb            String?
  /// use this instead of building the url on the fly (avoids a lot of joins and perf hits to build a string)
  url              String
  Challenge        Challenge?       @relation(fields: [challengeId], references: [id])
  challengeId      Int?
  SharedSolution   SharedSolution?  @relation(fields: [sharedSolutionId], references: [id])
  sharedSolutionId Int?

  @@index([toUserId])
  @@index([fromUserId])
  @@index([commentId])
  @@index([challengeId])
  @@index([sharedSolutionId])
}

model ShortURL {
  id           Int       @id @default(autoincrement())
  /// the original full url that we want to shorten e.g. https://www.url.com
  originalUrl  String    @db.MediumText
  /// slug for the short url e.g. url.com/[shortUrlSlug]
  shortUrlSlug String    @unique
  createdAt    DateTime  @default(now())
  /// the time the short url will expire
  expiresAt    DateTime?
  /// the number of times the short url has redirected to the original url
  clicks       Int       @default(0)
  userId       String?
  user         User?     @relation(fields: [userId], references: [id])

  @@index([userId])
  @@index([shortUrlSlug])
}