Files

74 lines
3.0 KiB
SQL

-- CreateEnum
CREATE TYPE "NotificationDeliveryStatus" AS ENUM ('PENDING', 'SENT', 'FAILED', 'READ');
-- CreateEnum
CREATE TYPE "NotificationPreferenceChannel" AS ENUM ('IN_APP', 'EMAIL');
-- CreateTable
CREATE TABLE "NotificationDelivery" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"tenantId" TEXT,
"userId" TEXT,
"channel" "NotificationPreferenceChannel" NOT NULL,
"category" TEXT NOT NULL,
"messageKey" TEXT NOT NULL,
"status" "NotificationDeliveryStatus" NOT NULL DEFAULT 'PENDING',
"attemptsMade" INTEGER NOT NULL DEFAULT 0,
"providerMessageId" TEXT,
"lastError" TEXT,
"payloadMetadata" JSONB,
"sentAt" TIMESTAMP(3),
"readAt" TIMESTAMP(3),
CONSTRAINT "NotificationDelivery_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "UserNotificationPreference" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"tenantId" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"channel" "NotificationPreferenceChannel" NOT NULL,
"category" TEXT NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT true,
CONSTRAINT "UserNotificationPreference_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "notification_delivery_tenant_user_status_idx" ON "NotificationDelivery"("tenantId", "userId", "status");
-- CreateIndex
CREATE INDEX "notification_delivery_tenant_channel_category_idx" ON "NotificationDelivery"("tenantId", "channel", "category");
-- CreateIndex
CREATE INDEX "notification_delivery_status_updated_at_idx" ON "NotificationDelivery"("status", "updatedAt");
-- CreateIndex
CREATE INDEX "notification_delivery_provider_message_id_idx" ON "NotificationDelivery"("providerMessageId");
-- CreateIndex
CREATE UNIQUE INDEX "user_notification_preference_scope_key" ON "UserNotificationPreference"("tenantId", "userId", "channel", "category");
-- CreateIndex
CREATE INDEX "user_notification_preference_tenant_user_category_idx" ON "UserNotificationPreference"("tenantId", "userId", "category");
-- CreateIndex
CREATE INDEX "user_notification_preference_tenant_category_idx" ON "UserNotificationPreference"("tenantId", "category");
-- AddForeignKey
ALTER TABLE "NotificationDelivery" ADD CONSTRAINT "NotificationDelivery_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "NotificationDelivery" ADD CONSTRAINT "NotificationDelivery_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "UserNotificationPreference" ADD CONSTRAINT "UserNotificationPreference_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "UserNotificationPreference" ADD CONSTRAINT "UserNotificationPreference_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;