Skip to content

Commit

Permalink
move some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-hendrix committed Nov 29, 2023
1 parent a0ef431 commit a9f32e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/app/api/users/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { ApiError, routeWrapper, checkUserMatchesSession } from '@/utils/api'
import { ApiError, routeWrapper, checkUserMatchesSession, checkUserBody, sanitizeUserSelect } from '@/utils/api'
import { generateHash, validatePassword } from '@/utils/hash'
import { checkUserBody, sanitizeUserSelect } from '../route'

export const GET = routeWrapper(
async (req: NextRequest, { params }: { params: { id: string } }) => {
Expand Down
26 changes: 1 addition & 25 deletions src/app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
import { NextRequest, NextResponse } from 'next/server'
import { Prisma } from '@prisma/client'
import prisma from '@/lib/prisma'
import { generateHash } from '@/utils/hash'
import { ApiError, routeWrapper, getQueryParams } from '@/utils/api'

export const sanitizeUserSelect = () => {
const fields = Object.keys(Prisma.UserScalarFieldEnum)
return Object.fromEntries(fields.map((k) => [k, k !== 'password']))
}

export const checkUserBody = async (body: any, id: string | null = null) => {
const usernameExists = async (username: string) => {
const user = await prisma.user.findUnique({ where: { username } })
if (!id) return Boolean(user)
return Boolean(user && user.id !== id)
}
const emailExists = async (email: string) => {
const user = await prisma.user.findUnique({ where: { email } })
if (!id) return Boolean(user)
return Boolean(user && user.id !== id)
}

if (!body) throw new ApiError('Request must have body', 400)
const { username, email } = body
if (username && await usernameExists(username)) throw new ApiError('Username exists', 400)
if (email && await emailExists(email)) throw new ApiError('Email exists', 400)
}
import { routeWrapper, getQueryParams, sanitizeUserSelect, checkUserBody } from '@/utils/api'

export const GET = routeWrapper(
async (req: NextRequest) => {
Expand Down
25 changes: 25 additions & 0 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-console */
import prisma from '@/lib/prisma'
import { Prisma } from '@prisma/client'
import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import authOptions from '@/lib/auth'
Expand Down Expand Up @@ -121,3 +123,26 @@ export const getQueryParams = (nextUrl: NextURL) => {

return queryParams
}

export const sanitizeUserSelect = () => {
const fields = Object.keys(Prisma.UserScalarFieldEnum)
return Object.fromEntries(fields.map((k) => [k, k !== 'password']))
}

export const checkUserBody = async (body: any, id: string | null = null) => {
const usernameExists = async (username: string) => {
const user = await prisma.user.findUnique({ where: { username } })
if (!id) return Boolean(user)
return Boolean(user && user.id !== id)
}
const emailExists = async (email: string) => {
const user = await prisma.user.findUnique({ where: { email } })
if (!id) return Boolean(user)
return Boolean(user && user.id !== id)
}

if (!body) throw new ApiError('Request must have body', 400)
const { username, email } = body
if (username && await usernameExists(username)) throw new ApiError('Username exists', 400)
if (email && await emailExists(email)) throw new ApiError('Email exists', 400)
}

0 comments on commit a9f32e9

Please sign in to comment.