From 5aad82a8c833bfbe602fe74a385611876b307af3 Mon Sep 17 00:00:00 2001 From: HugoRCD Date: Sun, 31 Mar 2024 19:01:53 +0200 Subject: [PATCH] feat: trim all name use for entity creation --- apps/app/server/api/auth/send-code.post.ts | 4 ++-- apps/app/server/api/project/[id]/index.put.ts | 1 + apps/app/server/api/teams/index.post.ts | 4 +++- apps/app/server/api/user/index.put.ts | 5 ++++- apps/app/server/app/userService.ts | 14 +++++++------- packages/shelve-types/src/User.ts | 4 ++-- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/apps/app/server/api/auth/send-code.post.ts b/apps/app/server/api/auth/send-code.post.ts index 711a6fa0..e4e95379 100644 --- a/apps/app/server/api/auth/send-code.post.ts +++ b/apps/app/server/api/auth/send-code.post.ts @@ -1,8 +1,8 @@ -import type { UserCreateInput } from '@shelve/types' +import type { CreateUserInput } from '@shelve/types' import { H3Event } from 'h3' import { upsertUser } from '~/server/app/userService' export default eventHandler(async (event: H3Event) => { - const body = await readBody(event) as UserCreateInput + const body = await readBody(event) as CreateUserInput return await upsertUser(body) }) diff --git a/apps/app/server/api/project/[id]/index.put.ts b/apps/app/server/api/project/[id]/index.put.ts index 6ffcdaa2..0149eb03 100644 --- a/apps/app/server/api/project/[id]/index.put.ts +++ b/apps/app/server/api/project/[id]/index.put.ts @@ -8,5 +8,6 @@ export default eventHandler(async (event: H3Event) => { const projectUpdateInput = await readBody(event) delete projectUpdateInput.variables delete projectUpdateInput.team + projectUpdateInput.name = projectUpdateInput.name.trim() return await updateProject(projectUpdateInput, parseInt(id), user.id) }) diff --git a/apps/app/server/api/teams/index.post.ts b/apps/app/server/api/teams/index.post.ts index c390b405..4f1fe906 100644 --- a/apps/app/server/api/teams/index.post.ts +++ b/apps/app/server/api/teams/index.post.ts @@ -1,8 +1,10 @@ import { H3Event } from 'h3' +import { CreateTeamInput } from '@shelve/types' import { createTeam } from '~/server/app/teamsService' export default eventHandler(async (event: H3Event) => { const user = event.context.user - const createTeamInput = await readBody(event) + const createTeamInput = await readBody(event) as CreateTeamInput + createTeamInput.name = createTeamInput.name.trim() return await createTeam(createTeamInput, user.id) }) diff --git a/apps/app/server/api/user/index.put.ts b/apps/app/server/api/user/index.put.ts index fa4a8644..de5bd0f9 100644 --- a/apps/app/server/api/user/index.put.ts +++ b/apps/app/server/api/user/index.put.ts @@ -1,9 +1,12 @@ import { H3Event } from 'h3' +import { UpdateUserInput } from '@shelve/types' import { updateUser } from '~/server/app/userService' export default eventHandler(async (event: H3Event) => { const user = event.context.user const authToken = event.context.authToken - const updateUserInput = await readBody(event) + const updateUserInput = await readBody(event) as UpdateUserInput + updateUserInput.username = updateUserInput.username?.trim() + updateUserInput.email = updateUserInput.email?.trim() return await updateUser(user, updateUserInput, authToken) }) diff --git a/apps/app/server/app/userService.ts b/apps/app/server/app/userService.ts index 4275eb60..c6fb38a4 100644 --- a/apps/app/server/app/userService.ts +++ b/apps/app/server/app/userService.ts @@ -1,4 +1,4 @@ -import type { publicUser, User, UserCreateInput, UserUpdateInput } from '@shelve/types' +import type { publicUser, User, CreateUserInput, UpdateUserInput } from '@shelve/types' import { Role } from '@shelve/types' import jwt from 'jsonwebtoken' import bcrypt from 'bcryptjs' @@ -16,25 +16,25 @@ type jwtPayload = { const runtimeConfig = useRuntimeConfig().private -export async function upsertUser(userCreateInput: UserCreateInput) { +export async function upsertUser(createUserInput: CreateUserInput) { const { otp, encryptedOtp } = await generateOtp() - let password = userCreateInput.password + let password = createUserInput.password if (password) password = await bcrypt.hash(password, 10) const user = await prisma.user.upsert({ where: { - email: userCreateInput.email, + email: createUserInput.email, }, update: { otp: encryptedOtp, password, }, create: { - ...userCreateInput, + ...createUserInput, password, otp: encryptedOtp, }, }) - if (!userCreateInput.password) await sendOtp(user.email, otp) + if (!createUserInput.password) await sendOtp(user.email, otp) return formatUser(user) } @@ -84,7 +84,7 @@ export async function verifyUserToken(token: string, user: User): Promise { +export async function updateUser(user: User, updateUserInput: UpdateUserInput, authToken: string): Promise { const newUsername = updateUserInput.username if (newUsername && newUsername !== user.username) { const usernameTaken = await prisma.user.findFirst({ diff --git a/packages/shelve-types/src/User.ts b/packages/shelve-types/src/User.ts index 4066c89d..150c653b 100644 --- a/packages/shelve-types/src/User.ts +++ b/packages/shelve-types/src/User.ts @@ -25,12 +25,12 @@ export type publicUser = { updatedAt: string | Date; }; -export type UserCreateInput = { +export type CreateUserInput = { email: string; password?: string; }; -export type UserUpdateInput = { +export type UpdateUserInput = { username?: string; password?: string; email?: string;