Initial commit: page-manager Next.js app with Coolify deploy support
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,379 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
enum GlobalRole {
|
||||
SUPERADMIN // every page + connect/disconnect pages + manage users
|
||||
MEMBER // only pages listed in PageMember
|
||||
}
|
||||
|
||||
enum PageRole {
|
||||
VIEWER
|
||||
EDITOR // edit posts, reply inbox
|
||||
APPROVER // + approve, publish/schedule, edit auto-reply rules
|
||||
MANAGER // + manage page members
|
||||
}
|
||||
|
||||
enum PageStatus {
|
||||
ACTIVE
|
||||
TOKEN_INVALID
|
||||
DISCONNECTED
|
||||
}
|
||||
|
||||
enum PostStatus {
|
||||
DRAFT
|
||||
PENDING_APPROVAL
|
||||
APPROVED
|
||||
SCHEDULED
|
||||
PUBLISHED
|
||||
FAILED
|
||||
CANCELLED
|
||||
}
|
||||
|
||||
enum Platform {
|
||||
FACEBOOK
|
||||
INSTAGRAM
|
||||
}
|
||||
|
||||
enum ReplyRuleKind {
|
||||
DM // keyword in a Messenger / IG DM -> reply in the thread
|
||||
COMMENT // keyword in a comment -> private reply (+ optional public ack)
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
email String @unique
|
||||
name String
|
||||
bio String @default("")
|
||||
avatarUrl String?
|
||||
passwordHash String
|
||||
globalRole GlobalRole @default(MEMBER)
|
||||
isActive Boolean @default(true)
|
||||
// Set when an admin sets/resets the password for someone; cleared only when
|
||||
// the user sets their own.
|
||||
mustChangePassword Boolean @default(false)
|
||||
lastLoginAt DateTime?
|
||||
|
||||
memberships PageMember[]
|
||||
postsCreated Post[] @relation("PostCreatedBy")
|
||||
postsEdited Post[] @relation("PostLastEditedBy")
|
||||
approvals PostApproval[]
|
||||
auditLogs AuditLog[]
|
||||
metaConnections MetaConnection[]
|
||||
analyses PageAnalysis[]
|
||||
identities UserIdentity[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
enum AuthProvider {
|
||||
GOOGLE
|
||||
FACEBOOK
|
||||
}
|
||||
|
||||
// "Sign in with Google / Facebook" for an existing account — no self-signup.
|
||||
// Linked on the first social login whose verified email matches User.email;
|
||||
// afterwards the provider's stable user id is what logs the person in.
|
||||
model UserIdentity {
|
||||
id String @id @default(cuid())
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
provider AuthProvider
|
||||
providerUserId String
|
||||
email String? // provider email at link time, for display
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([provider, providerUserId])
|
||||
// One identity per provider per user: a second Google/Facebook account
|
||||
// carrying the same email cannot take over an already-linked user.
|
||||
@@unique([userId, provider])
|
||||
}
|
||||
|
||||
// A Facebook user who granted page access through "Login with Facebook".
|
||||
// The long-lived user token (~60 days) is only used to list that person's
|
||||
// pages; page tokens minted from it do not expire and are stored on Page.
|
||||
model MetaConnection {
|
||||
id String @id @default(cuid())
|
||||
fbUserId String @unique
|
||||
name String
|
||||
tokenCipher String // AES-256-GCM, same box as Page.tokenCipher
|
||||
scopes String[]
|
||||
expiresAt DateTime?
|
||||
connectedById String?
|
||||
connectedBy User? @relation(fields: [connectedById], references: [id], onDelete: SetNull)
|
||||
|
||||
pages Page[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model Page {
|
||||
id String @id @default(cuid())
|
||||
fbPageId String @unique
|
||||
// Which Facebook login supplied the token; null = system user token.
|
||||
connectionId String?
|
||||
connection MetaConnection? @relation(fields: [connectionId], references: [id], onDelete: SetNull)
|
||||
name String
|
||||
pictureUrl String?
|
||||
// AES-256-GCM ciphertext of the page access token (lib/crypto/tokenBox.ts).
|
||||
// Never select this outside lib/facebook/tokens.ts.
|
||||
tokenCipher String
|
||||
igUserId String?
|
||||
igUsername String?
|
||||
status PageStatus @default(ACTIVE)
|
||||
statusDetail String?
|
||||
webhookSubscribedAt DateTime?
|
||||
tokenCheckedAt DateTime?
|
||||
|
||||
members PageMember[]
|
||||
posts Post[]
|
||||
rules ReplyRule[]
|
||||
conversations Conversation[]
|
||||
insights PageInsightSnapshot[]
|
||||
auditLogs AuditLog[]
|
||||
analyses PageAnalysis[]
|
||||
profile PageProfile?
|
||||
aiUsage AiUsage[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model PageMember {
|
||||
id String @id @default(cuid())
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
role PageRole @default(EDITOR)
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([userId, pageId])
|
||||
@@index([pageId])
|
||||
}
|
||||
|
||||
model Post {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
title String
|
||||
caption String
|
||||
imageBlobUrl String?
|
||||
targets Platform[] @default([FACEBOOK])
|
||||
|
||||
status PostStatus @default(DRAFT)
|
||||
scheduledPublishAt DateTime?
|
||||
// The day/time the owner pinned this post to on the board. A plan only —
|
||||
// it does not publish. Becomes the default time when the post is scheduled.
|
||||
plannedAt DateTime?
|
||||
|
||||
fbPostId String?
|
||||
fbPermalink String?
|
||||
igMediaId String?
|
||||
igPermalink String?
|
||||
lastError String?
|
||||
// Sticky: set once a caption from the AI assistant is used, so approvers know.
|
||||
aiAssisted Boolean @default(false)
|
||||
// Claim lock for publish (manual or cron): set right before calling the Graph
|
||||
// API so a concurrent publish/cron run can't double-post. Cleared once the
|
||||
// attempt settles (success or failure). A stale value past the lock's own
|
||||
// timeout is reclaimable — see PUBLISH_LOCK_STALE_MS in workflow.ts.
|
||||
publishingAt DateTime?
|
||||
|
||||
createdById String?
|
||||
createdBy User? @relation("PostCreatedBy", fields: [createdById], references: [id])
|
||||
// Separation of duty: the last editor cannot approve their own edit.
|
||||
lastEditedById String?
|
||||
lastEditedBy User? @relation("PostLastEditedBy", fields: [lastEditedById], references: [id])
|
||||
lastEditedAt DateTime?
|
||||
|
||||
approvals PostApproval[]
|
||||
insights PostInsightSnapshot[]
|
||||
auditLogs AuditLog[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([pageId, status])
|
||||
@@index([status, scheduledPublishAt])
|
||||
}
|
||||
|
||||
// One approver's vote. Rows are deleted whenever the content changes or the
|
||||
// post is rejected, which restarts the approval round.
|
||||
model PostApproval {
|
||||
id String @id @default(cuid())
|
||||
postId String
|
||||
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
|
||||
approverId String
|
||||
approver User @relation(fields: [approverId], references: [id], onDelete: Cascade)
|
||||
approverLabel String
|
||||
note String?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([postId, approverId])
|
||||
}
|
||||
|
||||
// Append-only. Never update or delete rows.
|
||||
model AuditLog {
|
||||
id String @id @default(cuid())
|
||||
pageId String?
|
||||
page Page? @relation(fields: [pageId], references: [id], onDelete: SetNull)
|
||||
postId String?
|
||||
post Post? @relation(fields: [postId], references: [id], onDelete: SetNull)
|
||||
actorId String?
|
||||
actor User? @relation(fields: [actorId], references: [id], onDelete: SetNull)
|
||||
// Name snapshot at the time of the action ("ระบบ" for cron/webhook).
|
||||
actorLabel String
|
||||
action String
|
||||
detail Json?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@index([pageId, createdAt])
|
||||
@@index([postId, createdAt])
|
||||
}
|
||||
|
||||
// Metadata only — message text is never stored; the inbox reads it live from Graph.
|
||||
model Conversation {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
platform Platform
|
||||
psid String
|
||||
botEnabled Boolean @default(true) // false = a human took over, auto-reply skips it
|
||||
lastInboundAt DateTime
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@unique([pageId, platform, psid])
|
||||
}
|
||||
|
||||
model ReplyRule {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
name String
|
||||
kind ReplyRuleKind
|
||||
keywords String[]
|
||||
reply String
|
||||
imageUrl String?
|
||||
publicAck String? // COMMENT only: visible reply under the comment
|
||||
isActive Boolean @default(true)
|
||||
priority Int @default(0)
|
||||
|
||||
logs ReplyLog[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([pageId, kind, isActive])
|
||||
}
|
||||
|
||||
// Dedupe ledger: the same person gets a given rule once. Claimed before
|
||||
// sending, released if the send fails.
|
||||
model ReplyLog {
|
||||
id String @id @default(cuid())
|
||||
ruleId String
|
||||
rule ReplyRule @relation(fields: [ruleId], references: [id], onDelete: Cascade)
|
||||
recipientId String
|
||||
sourceId String // comment id or message mid, for troubleshooting
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([ruleId, recipientId])
|
||||
}
|
||||
|
||||
model PageInsightSnapshot {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
date DateTime @db.Date
|
||||
views Int
|
||||
engagements Int
|
||||
follows Int
|
||||
followDelta Int
|
||||
raw Json
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([pageId, date])
|
||||
}
|
||||
|
||||
model PostInsightSnapshot {
|
||||
id String @id @default(cuid())
|
||||
postId String
|
||||
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
|
||||
date DateTime @db.Date
|
||||
impressions Int
|
||||
engaged Int
|
||||
reactions Int
|
||||
raw Json
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@unique([postId, date])
|
||||
}
|
||||
|
||||
enum AnalysisStatus {
|
||||
QUEUED
|
||||
RUNNING
|
||||
COMPLETED
|
||||
FAILED
|
||||
}
|
||||
|
||||
model PageAnalysis {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
requestedById String?
|
||||
requestedBy User? @relation(fields: [requestedById], references: [id], onDelete: SetNull)
|
||||
status AnalysisStatus @default(QUEUED)
|
||||
brief Json
|
||||
input Json
|
||||
result Json?
|
||||
error String?
|
||||
createdAt DateTime @default(now())
|
||||
startedAt DateTime?
|
||||
finishedAt DateTime?
|
||||
|
||||
@@index([pageId, createdAt])
|
||||
@@index([status, createdAt])
|
||||
}
|
||||
|
||||
// Shop facts and voice the AI assistant writes from. One per page.
|
||||
model PageProfile {
|
||||
id String @id @default(cuid())
|
||||
pageId String @unique
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
business String
|
||||
audience String @default("")
|
||||
tone String @default("")
|
||||
avoid String @default("")
|
||||
cta String @default("")
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
// One row per successful AI call — the per-page daily limit counts these.
|
||||
model AiUsage {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
userId String?
|
||||
kind String
|
||||
inputTokens Int
|
||||
outputTokens Int
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
@@index([pageId, createdAt])
|
||||
}
|
||||
Reference in New Issue
Block a user