diff --git a/apps/backend/src/routes/register/route.ts b/apps/backend/src/routes/register/route.ts index 9ac5706..432637c 100644 --- a/apps/backend/src/routes/register/route.ts +++ b/apps/backend/src/routes/register/route.ts @@ -7,7 +7,7 @@ import { hashPassword } from "../../utils/passwordUtils"; import requestValidator from "../../utils/requestValidator"; import authInfo from "../../middlewares/authInfo"; import checkPermission from "../../middlewares/checkPermission"; -import { and, eq, isNull, ilike, or, sql } from "drizzle-orm"; +import { and, eq, or } from "drizzle-orm"; import { z } from "zod"; import HonoEnv from "../../types/HonoEnv"; @@ -25,7 +25,6 @@ const registerFormSchema = z.object({ const respondentsRoute = new Hono() .use(authInfo) - //create user and respondent .post( "/", checkPermission("register.create"), @@ -33,6 +32,35 @@ const respondentsRoute = new Hono() async (c) => { const formData = c.req.valid("json"); + // Build conditions based on available formData + const conditions = []; + if (formData.email) { + conditions.push(eq(users.email, formData.email)); + } + conditions.push(eq(users.username, formData.username)); + + const existingUser = await db + .select() + .from(users) + .where(or(...conditions)); + + const existingRespondent = await db + .select() + .from(respondents) + .where(eq(respondents.phoneNumber, formData.phoneNumber)); + + if (existingUser.length > 0) { + throw new HTTPException(400, { + message: "Email atau username sudah terdaftar", + }); + } + + if (existingRespondent.length > 0) { + throw new HTTPException(400, { + message: "Nomor HP sudah terdaftar", + }); + } + // Hash the password const hashedPassword = await hashPassword(formData.password);