Skip to content

Commit

Permalink
fix(EditSurveyName): prevent infinite API calls and add seed configur…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
georgi-s committed Dec 12, 2024
1 parent 1e4b536 commit b2082f9
Show file tree
Hide file tree
Showing 30 changed files with 830 additions and 575 deletions.
15 changes: 15 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ const nextConfig = {
});
return config;
},
// Deaktiviere API-Routen-Logs im Development-Modus
logging: {
fetches: {
fullUrl: false,
},
},
// Deaktiviere Server-Logs
onDemandEntries: {
maxInactiveAge: 25 * 1000,
pagesBufferLength: 2,
},
// Minimiere Logs
devIndicators: {
buildActivity: false,
},
};

module.exports = nextConfig;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"format": "prettier --write \"**/*.{ts,tsx,js,jsx,json,css,scss,yml,yaml}\"",
"seed": "ts-node prisma/seed.ts"
},
"prisma": {
"seed": " ts-node prisma/seed.ts"
},
"dependencies": {
"@hello-pangea/dnd": "^17.0.0",
"@heroicons/react": "^2.0.18",
Expand All @@ -21,6 +24,7 @@
"@paypal/react-paypal-js": "^8.7.0",
"@prisma/client": "^5.13.0",
"@radix-ui/react-accordion": "^1.2.1",
"@radix-ui/react-alert-dialog": "^1.1.2",
"@radix-ui/react-avatar": "^1.1.1",
"@radix-ui/react-checkbox": "^1.1.2",
"@radix-ui/react-dialog": "1.0.4",
Expand Down
149 changes: 149 additions & 0 deletions prisma/migrations/0_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
-- CreateTable
CREATE TABLE "Survey" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"userId" TEXT NOT NULL,
"updatedAt" TIMESTAMP(3),
"status" TEXT NOT NULL DEFAULT 'draft',
"responseCount" INTEGER NOT NULL DEFAULT 0,

CONSTRAINT "Survey_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Question" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT NOT NULL,
"min" TEXT NOT NULL,
"steps" INTEGER NOT NULL,
"max" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),
"responseCount" INTEGER NOT NULL DEFAULT 0,
"averageValue" DOUBLE PRECISION NOT NULL DEFAULT 0,
"surveyId" TEXT NOT NULL,
"scaleType" TEXT NOT NULL DEFAULT 'numeric',
"scaleOptions" TEXT[] DEFAULT ARRAY[]::TEXT[],

CONSTRAINT "Question_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Answer" (
"id" TEXT NOT NULL,
"questionId" TEXT NOT NULL,
"surveyId" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"value" INTEGER NOT NULL,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "Answer_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"name" TEXT,
"email" TEXT,
"emailVerified" TIMESTAMP(3),
"image" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"lastLogin" TIMESTAMP(3),
"role" TEXT NOT NULL DEFAULT 'RESEARCHER',
"updatedAt" TIMESTAMP(3),
"planId" TEXT,
"planActiveUntil" TIMESTAMP(3),

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Session" (
"id" TEXT NOT NULL,
"sessionToken" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "Session_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Account" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"type" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"providerAccountId" TEXT NOT NULL,
"refresh_token" TEXT,
"access_token" TEXT,
"expires_at" INTEGER,
"token_type" TEXT,
"scope" TEXT,
"id_token" TEXT,
"session_state" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "VerificationToken" (
"identifier" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "Plan" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"price" DOUBLE PRECISION NOT NULL,
"description" TEXT NOT NULL,
"features" TEXT[],
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "Plan_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken");

-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");

-- AddForeignKey
ALTER TABLE "Survey" ADD CONSTRAINT "Survey_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Question" ADD CONSTRAINT "Question_surveyId_fkey" FOREIGN KEY ("surveyId") REFERENCES "Survey"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Answer" ADD CONSTRAINT "Answer_questionId_fkey" FOREIGN KEY ("questionId") REFERENCES "Question"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Answer" ADD CONSTRAINT "Answer_surveyId_fkey" FOREIGN KEY ("surveyId") REFERENCES "Survey"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_planId_fkey" FOREIGN KEY ("planId") REFERENCES "Plan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- AlterTable
ALTER TABLE "Question" ADD COLUMN IF NOT EXISTS "position" INTEGER NOT NULL DEFAULT 0;

-- Add index on surveyId
CREATE INDEX IF NOT EXISTS "Question_surveyId_idx" ON "Question"("surveyId");

-- UpdateData: Set position based on creation date within each survey
WITH numbered_questions AS (
SELECT id, ROW_NUMBER() OVER (PARTITION BY "surveyId" ORDER BY "createdAt") - 1 as new_position
FROM "Question"
)
UPDATE "Question" q
SET position = nq.new_position
FROM numbered_questions nq
WHERE q.id = nq.id;
11 changes: 7 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ model Question {
steps Int
max String
createdAt DateTime @default(now())
surveyId String
updatedAt DateTime? @updatedAt
responseCount Int @default(0)
averageValue Float @default(0)
scaleType String @default("default")
scaleOptions String[] @default([])
answers Answer[]
surveyId String
survey Survey @relation(fields: [surveyId], references: [id], onDelete: Cascade)
answers Answer[]
scaleType String @default("numeric")
scaleOptions String[] @default([])
position Int @default(0)
@@index([surveyId])
}

model Answer {
Expand Down
31 changes: 29 additions & 2 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function main() {
// Lösche existierende Pläne
// Lösche existierende Daten in der richtigen Reihenfolge
await prisma.survey.deleteMany();
await prisma.user.deleteMany();
await prisma.plan.deleteMany();

// Erstelle Standard-Pläne
Expand Down Expand Up @@ -48,10 +50,35 @@ async function main() {
},
];

// Erstelle Pläne und speichere die IDs
const createdPlans: Record<string, string> = {};
for (const plan of plans) {
await prisma.plan.create({
const createdPlan = await prisma.plan.create({
data: plan,
});
createdPlans[createdPlan.name] = createdPlan.id;
}

// Erstelle Pro-User
const proUsers = [
{
email: '[email protected]',
name: 'Georgi Semov',
},
{
email: '[email protected]',
name: 'Thomas Bartoschek',
},
];

for (const user of proUsers) {
await prisma.user.create({
data: {
...user,
planId: createdPlans['Professional'],
planActiveUntil: new Date('2025-12-31'), // Plan gültig bis Ende 2025
},
});
}

console.log('Seed erfolgreich ausgeführt');
Expand Down
25 changes: 0 additions & 25 deletions src/app/api/survey/[surveyId]/stats/route.ts

This file was deleted.

60 changes: 0 additions & 60 deletions src/app/api/survey/[surveyId]/stats/update/route.ts

This file was deleted.

Loading

0 comments on commit b2082f9

Please sign in to comment.