refactor(register, listings, items): simplify transaction, remove unused variable, add explicit types

- Remove newUser variable and directly return created user from Prisma transaction in register route
- Add explicit casts for Prisma QueryMode to avoid TypeScript widening in listings search
- Fix items search and getItemBySource with any cast and ts-expect-error to satisfy type checks

Improves readability and type safety without changing behavior.
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-05 04:38:07 -03:00
1 parent ab37b477df
commit f35fee944d
33 files changed
+811 -295

No files matched your search

+24 -6
View File
@@ -76,7 +76,25 @@ export async function getUserByUsername(
/**
* Busca usuário por ID (sem password_hash)
*/
export async function getUserById(userId: number): Promise<User | null> {
type PublicUser = {
user_id: number;
username: string;
email: string;
first_name: string;
last_name: string;
phone: string | null;
cpf: string | null;
account_level: string;
has_donated: boolean;
whatsapp_notifications: boolean;
created_at: Date;
updated_at: Date;
last_login: Date | null;
is_active: boolean;
is_verified: boolean;
};
export async function getUserById(userId: number): Promise<PublicUser | null> {
return await prisma.user.findUnique({
where: { user_id: userId },
select: {
@@ -96,13 +114,13 @@ export async function getUserById(userId: number): Promise<User | null> {
is_active: true,
is_verified: true,
},
});
}) as PublicUser | null;
}
/**
* Busca usuário por CPF (verifica duplicidade)
*/
export async function getUserByCpf(cpf: string): Promise<User | null> {
export async function getUserByCpf(cpf: string): Promise<Pick<User, 'user_id' | 'username' | 'email'> | null> {
return await prisma.user.findUnique({
where: { cpf },
select: {
@@ -110,7 +128,7 @@ export async function getUserByCpf(cpf: string): Promise<User | null> {
username: true,
email: true,
},
});
}) as Pick<User, 'user_id' | 'username' | 'email'> | null;
}
/**
@@ -119,7 +137,7 @@ export async function getUserByCpf(cpf: string): Promise<User | null> {
export async function updateUser(
userId: number,
data: UpdateUserData
): Promise<User> {
): Promise<PublicUser> {
const updateData: any = {};
if (data.first_name !== undefined) updateData.first_name = data.first_name;
if (data.last_name !== undefined) updateData.last_name = data.last_name;
@@ -149,7 +167,7 @@ export async function updateUser(
},
});
return user;
return user as PublicUser;
}
/**