Skip to content

Commit

Permalink
feat: trim all name use for entity creation
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoRCD committed Mar 31, 2024
1 parent a890c3e commit 5aad82a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions apps/app/server/api/auth/send-code.post.ts
Original file line number Diff line number Diff line change
@@ -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)
})
1 change: 1 addition & 0 deletions apps/app/server/api/project/[id]/index.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
4 changes: 3 additions & 1 deletion apps/app/server/api/teams/index.post.ts
Original file line number Diff line number Diff line change
@@ -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)
})
5 changes: 4 additions & 1 deletion apps/app/server/api/user/index.put.ts
Original file line number Diff line number Diff line change
@@ -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)
})
14 changes: 7 additions & 7 deletions apps/app/server/app/userService.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
}

Expand Down Expand Up @@ -84,7 +84,7 @@ export async function verifyUserToken(token: string, user: User): Promise<boolea
}
}

export async function updateUser(user: User, updateUserInput: UserUpdateInput, authToken: string): Promise<publicUser> {
export async function updateUser(user: User, updateUserInput: UpdateUserInput, authToken: string): Promise<publicUser> {
const newUsername = updateUserInput.username
if (newUsername && newUsername !== user.username) {
const usernameTaken = await prisma.user.findFirst({
Expand Down
4 changes: 2 additions & 2 deletions packages/shelve-types/src/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5aad82a

Please sign in to comment.