diff --git a/prisma/migrations/20260706120000_add_pdf_api_key/migration.sql b/prisma/migrations/20260706120000_add_pdf_api_key/migration.sql new file mode 100644 index 0000000..f13dc1b --- /dev/null +++ b/prisma/migrations/20260706120000_add_pdf_api_key/migration.sql @@ -0,0 +1,14 @@ +-- Чистый PDF — персональный API-ключ студента (Zotero/curl) +CREATE TABLE "PdfApiKey" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "key" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + CONSTRAINT "PdfApiKey_pkey" PRIMARY KEY ("id") +); + +CREATE UNIQUE INDEX "PdfApiKey_userId_key" ON "PdfApiKey"("userId"); +CREATE UNIQUE INDEX "PdfApiKey_key_key" ON "PdfApiKey"("key"); + +ALTER TABLE "PdfApiKey" ADD CONSTRAINT "PdfApiKey_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cf509ea..d0b3d72 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -47,6 +47,7 @@ model User { questionMessages StudentQuestionMessage[] wrappedRuns WrappedRun[] toolUsages ToolUsage[] + pdfApiKey PdfApiKey? } // Obsidian Wrapped — агрегаты прогона (БЕЗ имён/текстов заметок; обработка волта 100% client-side) @@ -82,6 +83,16 @@ model ToolUsage { @@index([createdAt]) } +// Чистый PDF — персональный API-ключ студента (Zotero/curl) +model PdfApiKey { + id String @id @default(cuid()) + userId String @unique + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + key String @unique // sbpdf_, показывается студенту на /tools/clean-pdf + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} + model Session { id String @id @default(cuid()) userId String diff --git a/src/lib/clean-pdf/keys.ts b/src/lib/clean-pdf/keys.ts new file mode 100644 index 0000000..cf014d7 --- /dev/null +++ b/src/lib/clean-pdf/keys.ts @@ -0,0 +1,23 @@ +import { prisma } from "@/lib/prisma"; +import { generatePdfKey } from "@/lib/clean-pdf/api-key"; + +export async function getOrCreatePdfKey(userId: string): Promise { + const existing = await prisma.pdfApiKey.findUnique({ where: { userId } }); + if (existing) return existing.key; + const created = await prisma.pdfApiKey.upsert({ + where: { userId }, + create: { userId, key: generatePdfKey() }, + update: {}, + }); + return created.key; +} + +export async function regenerateKey(userId: string): Promise { + const key = generatePdfKey(); + await prisma.pdfApiKey.upsert({ + where: { userId }, + create: { userId, key }, + update: { key }, + }); + return key; +}