Skip to content

Open source CapCut alternative.

TypeScript
2.6 KB
Raw Blame
// Per-user email posture: whether the one-time welcome email has gone out, and
// whether the account has opted out of non-essential email. Transactional and
// security email ignores the opt-out; any other send must check it first
// (the back office, which sends all email).

model UserEmailSettings {
  userId                  String    @id
  user                    User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  // Claimed before sending, so a retried signup can never double-send.
  welcomeEmailSentAt      DateTime?
  // Null means subscribed. Set by the one-click endpoint, the /unsubscribe
  // page, or the settings toggle.
  marketingUnsubscribedAt DateTime?
  createdAt               DateTime  @default(now())
  updatedAt               DateTime  @updatedAt
}

// The outbox. Every email the back office sends is a row here first; one drainer
// in the back office sends rows in priority order under the plan's monthly
// budget. The row is the send's record: a retried queue
// call adds nothing because the idempotency key is unique.
model EmailSend {
  id             String     @id @default(cuid())
  // One of the back office's email kinds.
  kind           String
  // Higher goes first: the kind's priority when the row was queued.
  priority       Int
  // Order within a priority. A promotion row holds the seconds since its
  // recipient last used the product when it was queued, so the most recently
  // active go first.
  rank           Int        @default(0)
  userId         String?
  user           User?      @relation(fields: [userId], references: [id], onDelete: Cascade)
  promotionId    String?
  promotion      Promotion? @relation(fields: [promotionId], references: [id], onDelete: Cascade)
  // What the kind's builder needs to make the message.
  payload        Json
  idempotencyKey String     @unique
  // "queued" | "sending" | "sent" | "failed" | "skipped"
  state          String     @default("queued")
  attempts       Int        @default(0)
  error          String?
  // A failed attempt pushes the next one out.
  notBefore      DateTime   @default(now())
  sentAt         DateTime?
  // The first time the recipient followed the message's button.
  clickedAt      DateTime?
  createdAt      DateTime   @default(now())
  updatedAt      DateTime   @updatedAt

  @@index([state, priority, rank, createdAt])
  @@index([promotionId, state])
  @@index([userId, sentAt])
}

// Global outbound email cap for free-tier limits: how many emails were sent
// on each UTC calendar day.
model EmailDailyQuota {
  day      String   @id
  sent     Int      @default(0)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}