- wrap root layout with QueryProvider for React Query - replace market UnderConstruction placeholder with MarketClient and real data fetching - implement search, pagination, and sort via search params using getActiveListings - add necessary client components and queries - update dependencies (e.g., prisma, pkg-types)
165 lines
3.5 KiB
TypeScript
165 lines
3.5 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)
|
|
*/
|
|
export async function getUserById(userId: number): Promise<User | 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,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Busca usuário por CPF (verifica duplicidade)
|
|
*/
|
|
export async function getUserByCpf(cpf: string): Promise<User | null> {
|
|
return await prisma.user.findUnique({
|
|
where: { cpf },
|
|
select: {
|
|
user_id: true,
|
|
username: true,
|
|
email: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Atualiza dados do usuário
|
|
*/
|
|
export async function updateUser(
|
|
userId: number,
|
|
data: UpdateUserData
|
|
): Promise<User> {
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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() },
|
|
});
|
|
}
|
|
|