diff --git a/src/app/api/users/[userId]/route.ts b/src/app/api/users/[userId]/route.ts index 383f0ffac..e642fe3c1 100644 --- a/src/app/api/users/[userId]/route.ts +++ b/src/app/api/users/[userId]/route.ts @@ -24,10 +24,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ user return json(user); } -export async function POST( - request: Request, - { params }: { params: Promise<{ userId: string }> }, -) { +export async function POST(request: Request, { params }: { params: Promise<{ userId: string }> }) { const schema = z.object({ username: z.string().max(255).optional(), password: z.string().min(8).max(255).optional(), @@ -66,15 +63,15 @@ export async function POST( } if (username && auth.user.isAdmin) { - data.username = username.toLowerCase(); + data.username = username; } // Check when username changes if (data.username && user.username !== data.username) { - const existingUser = await getUserByUsername(username); + const user = await getUserByUsername(username); - if (existingUser && existingUser.id !== userId) { - return badRequest({ message: "User already exists" }); + if (user) { + return badRequest({ message: 'User already exists' }); } } @@ -100,7 +97,7 @@ export async function DELETE( } if (userId === auth.user.id) { - return badRequest({ message: "You cannot delete yourself." }); + return badRequest({ message: 'You cannot delete yourself.' }); } await deleteUser(userId); diff --git a/src/app/api/users/route.ts b/src/app/api/users/route.ts index d70faba06..4335c33fb 100644 --- a/src/app/api/users/route.ts +++ b/src/app/api/users/route.ts @@ -28,17 +28,15 @@ export async function POST(request: Request) { const { id, username, password, role } = body; - const existingUser = await getUserByUsername(username, { - showDeleted: true, - }); + const existingUser = await getUserByUsername(username, { showDeleted: true }); if (existingUser) { - return badRequest({ message: "User already exists" }); + return badRequest({ message: 'User already exists' }); } const user = await createUser({ id: id || uuid(), - username: username.toLowerCase(), + username, password: hashPassword(password), role: role ?? ROLES.user, }); diff --git a/src/queries/prisma/user.ts b/src/queries/prisma/user.ts index 4893bb1b3..467ea1e02 100644 --- a/src/queries/prisma/user.ts +++ b/src/queries/prisma/user.ts @@ -1,8 +1,8 @@ -import { Prisma } from "@/generated/prisma/client"; -import { ROLES } from "@/lib/constants"; -import { getRandomChars } from "@/lib/generate"; -import prisma from "@/lib/prisma"; -import type { QueryFilters, Role } from "@/lib/types"; +import { Prisma } from '@/generated/prisma/client'; +import { ROLES } from '@/lib/constants'; +import { getRandomChars } from '@/lib/generate'; +import prisma from '@/lib/prisma'; +import type { QueryFilters, Role } from '@/lib/types'; import UserFindManyArgs = Prisma.UserFindManyArgs; @@ -11,10 +11,7 @@ export interface GetUserOptions { showDeleted?: boolean; } -async function findUser( - criteria: Prisma.UserFindUniqueArgs, - options: GetUserOptions = {}, -) { +async function findUser(criteria: Prisma.UserFindUniqueArgs, options: GetUserOptions = {}) { const { includePassword = false, showDeleted = false } = options; return prisma.client.user.findUnique({ @@ -44,33 +41,27 @@ export async function getUser(userId: string, options: GetUserOptions = {}) { ); } -export async function getUserByUsername( - username: string, - options: GetUserOptions = {}, -) { - return findUser({ where: { username: username.toLowerCase() } }, options); +export async function getUserByUsername(username: string, options: GetUserOptions = {}) { + return findUser({ where: { username } }, options); } -export async function getUsers( - criteria: UserFindManyArgs, - filters: QueryFilters = {}, -) { +export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters = {}) { const { search } = filters; const where: Prisma.UserWhereInput = { ...criteria.where, - ...prisma.getSearchParameters(search, [{ username: "contains" }]), + ...prisma.getSearchParameters(search, [{ username: 'contains' }]), deletedAt: null, }; return prisma.pagedQuery( - "user", + 'user', { ...criteria, where, }, { - orderBy: "createdAt", + orderBy: 'createdAt', sortDescending: true, ...filters, }, @@ -119,7 +110,7 @@ export async function deleteUser(userId: string) { let websiteIds = []; if (websites.length > 0) { - websiteIds = websites.map((a) => a.id); + websiteIds = websites.map(a => a.id); } const teams = await client.team.findMany({ @@ -133,7 +124,7 @@ export async function deleteUser(userId: string) { }, }); - const teamIds = teams.map((a) => a.id); + const teamIds = teams.map(a => a.id); if (cloudMode) { return transaction([