refactor: remove username from User model, login by email only
This commit is contained in:
@@ -11,7 +11,6 @@ import {
|
||||
createUser,
|
||||
getUserByEmail,
|
||||
getUserById,
|
||||
getUserByUsername,
|
||||
resetUserPassword,
|
||||
setUserActive,
|
||||
updateUser,
|
||||
@@ -49,15 +48,11 @@ function uniqueErrorFor(error: unknown): FieldErrors | null {
|
||||
|
||||
const target = Array.isArray(error.meta?.target) ? error.meta.target : []
|
||||
|
||||
if (target.includes("username")) {
|
||||
return { username: ["Username already exists"] }
|
||||
}
|
||||
|
||||
if (target.includes("email")) {
|
||||
return { email: ["Email already exists"] }
|
||||
}
|
||||
|
||||
return { username: ["Username already exists"] }
|
||||
return { email: ["Email already exists"] }
|
||||
}
|
||||
|
||||
function isTransactionConflictError(error: unknown) {
|
||||
@@ -113,14 +108,10 @@ async function getAdminAccessLossError(
|
||||
export async function createUserUseCase(
|
||||
input: CreateUserFormType,
|
||||
): Promise<UserUseCaseResult> {
|
||||
const { username, email } = input
|
||||
const { email } = input
|
||||
|
||||
try {
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
if (await getUserByUsername(username, tx)) {
|
||||
return userError({ username: ["Username already exists"] })
|
||||
}
|
||||
|
||||
if (await getUserByEmail(email, tx)) {
|
||||
return userError({ email: ["Email already exists"] })
|
||||
}
|
||||
@@ -145,7 +136,7 @@ export async function createUserUseCase(
|
||||
export async function updateUserUseCase(
|
||||
input: UpdateUserFormType & ActorInput,
|
||||
): Promise<UserUseCaseResult> {
|
||||
const { actorId, id, username, email, role, isActive, ...data } = input
|
||||
const { actorId, id, email, role, isActive, ...data } = input
|
||||
|
||||
try {
|
||||
return await runSerializableUserTransaction(async (tx) => {
|
||||
@@ -166,17 +157,12 @@ export async function updateUserUseCase(
|
||||
if (error) return userError({ id: [error] })
|
||||
}
|
||||
|
||||
const existingUsername = await getUserByUsername(username, tx)
|
||||
if (existingUsername && existingUsername.id !== id) {
|
||||
return userError({ username: ["Username already exists"] })
|
||||
}
|
||||
|
||||
const existingEmail = await getUserByEmail(email, tx)
|
||||
if (existingEmail && existingEmail.id !== id) {
|
||||
return userError({ email: ["Email already exists"] })
|
||||
}
|
||||
|
||||
await updateUser(id, { ...data, username, email, role, isActive }, tx)
|
||||
await updateUser(id, { ...data, email, role, isActive }, tx)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user