From 41895ea3847d4b7d225224abeafc3c3f1bb2bebb Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 8 Dec 2024 23:38:50 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=89=F0=9F=91=87=F0=9F=91=88=20username?= =?UTF-8?q?=20and=20email=20should=20be=20unique=20for=20User?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../migrations/20241208123401_init/migration.sql | 12 ++++++++++++ backend/prisma/schema.prisma | 4 ++-- backend/src/routes/OTP/OTPToken.ts | 14 +++++++++----- backend/tests/otp.test.ts | 3 ++- 5 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 backend/prisma/migrations/20241208123401_init/migration.sql diff --git a/backend/package.json b/backend/package.json index 49b47d9..9b8ec60 100644 --- a/backend/package.json +++ b/backend/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "start": "NODE_ENV=dev ts-node src/index.ts", - "test": "vitest", + "test": "vitest --inspect-brk --no-file-parallelism --disable-console-intercept", "test:int": "./scripts/run-integration.sh" }, "keywords": [], diff --git a/backend/prisma/migrations/20241208123401_init/migration.sql b/backend/prisma/migrations/20241208123401_init/migration.sql new file mode 100644 index 0000000..74bc6d3 --- /dev/null +++ b/backend/prisma/migrations/20241208123401_init/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - A unique constraint covering the columns `[username]` on the table `User` will be added. If there are existing duplicate values, this will fail. + - A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail. + +*/ +-- CreateIndex +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index c30a494..551ef43 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -20,8 +20,8 @@ enum UserType { model User { id Int @id @default(autoincrement()) - username String - email String + username String @unique + email String @unique password String userType UserType salt String diff --git a/backend/src/routes/OTP/OTPToken.ts b/backend/src/routes/OTP/OTPToken.ts index e10440c..bb9757b 100644 --- a/backend/src/routes/OTP/OTPToken.ts +++ b/backend/src/routes/OTP/OTPToken.ts @@ -10,11 +10,15 @@ export interface OTPToken { export const getUserFromEmail = async (emailAddress: string) => { + try { const result = await prisma.user.findUnique({ - where: { - email: emailAddress, - }, - }); + where: { + email: emailAddress, + }, + }); + return result; + } catch (error) { + throw new Error("Error finding user"); + } - return result; } \ No newline at end of file diff --git a/backend/tests/otp.test.ts b/backend/tests/otp.test.ts index ebcd0c7..4822979 100644 --- a/backend/tests/otp.test.ts +++ b/backend/tests/otp.test.ts @@ -38,6 +38,7 @@ describe("Tests", () => { const response = await request(app).post("/auth/otp/generate").send({ email: "pyramidstestdump@gmail.com" }); + console.log(response.body); expect(response.status).toBe(200); const hash = await redisClient.get(newUser.email); @@ -45,7 +46,7 @@ describe("Tests", () => { expect(hash).not.toBeNull(); if(hash) { - console.log(hash); + //console.log(hash); await new Promise(resolve => setTimeout(resolve, 70000));