From 4816d098da4bad01d0fde87bbe2153e74bda8409 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 18 May 2026 11:17:02 +0500 Subject: [PATCH] feat: add notification system database models --- .../migration.sql | 54 +++++++++++++++++++ .../migration.sql | 2 + server/prisma/schema.prisma | 50 +++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 server/prisma/migrations/20260518061700_add_notification_system/migration.sql create mode 100644 server/prisma/migrations/20260518062042_fix_notification_schema/migration.sql diff --git a/server/prisma/migrations/20260518061700_add_notification_system/migration.sql b/server/prisma/migrations/20260518061700_add_notification_system/migration.sql new file mode 100644 index 0000000..c780066 --- /dev/null +++ b/server/prisma/migrations/20260518061700_add_notification_system/migration.sql @@ -0,0 +1,54 @@ +-- CreateTable +CREATE TABLE "NotificationPreference" ( + "id" TEXT NOT NULL PRIMARY KEY, + "userId" TEXT NOT NULL, + "globalEnabled" BOOLEAN NOT NULL DEFAULT true, + "orderCreated" BOOLEAN NOT NULL DEFAULT true, + "orderStatusChanged" BOOLEAN NOT NULL DEFAULT true, + "orderMessageReceived" BOOLEAN NOT NULL DEFAULT true, + "paymentStatusChanged" BOOLEAN NOT NULL DEFAULT true, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + CONSTRAINT "NotificationPreference_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "AdminNotificationSettings" ( + "id" TEXT NOT NULL PRIMARY KEY, + "emailEnabled" BOOLEAN NOT NULL DEFAULT true, + "telegramEnabled" BOOLEAN NOT NULL DEFAULT false, + "telegramChatId" TEXT, + "newOrder" BOOLEAN NOT NULL DEFAULT true, + "newOrderMessage" BOOLEAN NOT NULL DEFAULT true, + "newReview" BOOLEAN NOT NULL DEFAULT true, + "authCodeDuplicate" BOOLEAN NOT NULL DEFAULT false, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL +); + +-- CreateTable +CREATE TABLE "NotificationLog" ( + "id" TEXT NOT NULL PRIMARY KEY, + "userId" TEXT, + "eventType" TEXT NOT NULL, + "channel" TEXT NOT NULL, + "status" TEXT NOT NULL, + "error" TEXT, + "payload" TEXT NOT NULL, + "attempts" INTEGER NOT NULL DEFAULT 0, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + CONSTRAINT "NotificationLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +-- CreateIndex +CREATE UNIQUE INDEX "NotificationPreference_userId_key" ON "NotificationPreference"("userId"); + +-- CreateIndex +CREATE INDEX "NotificationPreference_userId_idx" ON "NotificationPreference"("userId"); + +-- CreateIndex +CREATE INDEX "NotificationLog_status_createdAt_idx" ON "NotificationLog"("status", "createdAt"); + +-- CreateIndex +CREATE INDEX "NotificationLog_userId_createdAt_idx" ON "NotificationLog"("userId", "createdAt"); diff --git a/server/prisma/migrations/20260518062042_fix_notification_schema/migration.sql b/server/prisma/migrations/20260518062042_fix_notification_schema/migration.sql new file mode 100644 index 0000000..23e6a7a --- /dev/null +++ b/server/prisma/migrations/20260518062042_fix_notification_schema/migration.sql @@ -0,0 +1,2 @@ +-- DropIndex +DROP INDEX "NotificationPreference_userId_idx"; diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index af2366e..8cf163b 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -90,6 +90,8 @@ model User { reviews Review[] orderMessageReadStates UserOrderMessageReadState[] oauthAccounts OAuthAccount[] + notificationPreference NotificationPreference? + notificationLogs NotificationLog[] } /// Прочитанность чата по заказу (для сообщений от админа после lastReadAt) @@ -269,3 +271,51 @@ model InfoPageBlock { @@index([published, sort]) } + +/// Настройки оповещений пользователя +model NotificationPreference { + id String @id @default(cuid()) + userId String @unique + globalEnabled Boolean @default(true) + orderCreated Boolean @default(true) + orderStatusChanged Boolean @default(true) + orderMessageReceived Boolean @default(true) + paymentStatusChanged Boolean @default(true) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) +} + +/// Настройки оповещений админа +model AdminNotificationSettings { + id String @id @default(cuid()) + emailEnabled Boolean @default(true) + telegramEnabled Boolean @default(false) + telegramChatId String? + newOrder Boolean @default(true) + newOrderMessage Boolean @default(true) + newReview Boolean @default(true) + authCodeDuplicate Boolean @default(false) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} + +/// Лог отправки оповещений +model NotificationLog { + id String @id @default(cuid()) + userId String? + eventType String + channel String + status String + error String? + payload String + attempts Int @default(0) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + user User? @relation(fields: [userId], references: [id], onDelete: SetNull) + + @@index([status, createdAt]) + @@index([userId, createdAt]) +}