Files
pagodera/lib/prisma/queries/users.ts
T
Evandro Fonseca Junior f35fee944d 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.
2025-11-05 04:38:07 -03:00

183 lines
4.0 KiB
TypeScript

/**
* Queries de usuários usando Prisma
* Migrado de lib/db/queries/users.ts
*/
import { prisma } from "../app";
import type { User } from "@prisma/client";
export interface CreateUserData {
username: string;
email: string;
password_hash: string;
first_name: string;
last_name: string;
phone?: string;
cpf?: string;
}
export interface UpdateUserData {
first_name?: string;
last_name?: string;
phone?: string;
whatsapp_notifications?: boolean;
}
/**
* Cria um novo usuário
*/
export async function createUser(
data: CreateUserData
): Promise<{ userId: number; username: string; email: string }> {
const user = await prisma.user.create({
data: {
username: data.username,
email: data.email,
password_hash: data.password_hash,
first_name: data.first_name,
last_name: data.last_name,
phone: data.phone || null,
cpf: data.cpf || null,
},
select: {
user_id: true,
username: true,
email: true,
},
});
return {
userId: user.user_id,
username: user.username,
email: user.email,
};
}
/**
* Busca usuário por email (inclui password_hash para login)
*/
export async function getUserByEmail(email: string): Promise<User | null> {
return await prisma.user.findUnique({
where: { email },
});
}
/**
* Busca usuário por username
*/
export async function getUserByUsername(
username: string
): Promise<User | null> {
return await prisma.user.findUnique({
where: { username },
});
}
/**
* Busca usuário por ID (sem password_hash)
*/
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: {
user_id: true,
username: true,
email: true,
first_name: true,
last_name: true,
phone: true,
cpf: true,
account_level: true,
has_donated: true,
whatsapp_notifications: true,
created_at: true,
updated_at: true,
last_login: true,
is_active: true,
is_verified: true,
},
}) as PublicUser | null;
}
/**
* Busca usuário por CPF (verifica duplicidade)
*/
export async function getUserByCpf(cpf: string): Promise<Pick<User, 'user_id' | 'username' | 'email'> | null> {
return await prisma.user.findUnique({
where: { cpf },
select: {
user_id: true,
username: true,
email: true,
},
}) as Pick<User, 'user_id' | 'username' | 'email'> | null;
}
/**
* Atualiza dados do usuário
*/
export async function updateUser(
userId: number,
data: UpdateUserData
): 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;
if (data.phone !== undefined) updateData.phone = data.phone || null;
if (data.whatsapp_notifications !== undefined)
updateData.whatsapp_notifications = data.whatsapp_notifications;
const user = await prisma.user.update({
where: { user_id: userId },
data: updateData,
select: {
user_id: true,
username: true,
email: true,
first_name: true,
last_name: true,
phone: true,
cpf: true,
account_level: true,
has_donated: true,
whatsapp_notifications: true,
created_at: true,
updated_at: true,
last_login: true,
is_active: true,
is_verified: true,
},
});
return user as PublicUser;
}
/**
* Atualiza timestamp de último login
*/
export async function updateLastLogin(userId: number): Promise<void> {
await prisma.user.update({
where: { user_id: userId },
data: { last_login: new Date() },
});
}