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:
1 parent
ab37b477df
commit
f35fee944d
33 files changed
+811
-295
No files matched your search
@@ -114,80 +114,76 @@ export async function POST(request: NextRequest) {
|
||||
// Criar usuário e personagens em transação atômica usando Prisma
|
||||
const { prisma } = await import("@/lib/prisma/app");
|
||||
|
||||
let newUser: { userId: number; username: string; email: string };
|
||||
const createdCharacters: Array<{ characterName: string; serverName: string }> = [];
|
||||
|
||||
try {
|
||||
// Usar transação Prisma
|
||||
await prisma.$transaction(async (tx) => {
|
||||
// Criar usuário
|
||||
const user = await tx.user.create({
|
||||
data: {
|
||||
username: validatedData.username,
|
||||
email: validatedData.email,
|
||||
password_hash: passwordHash,
|
||||
first_name: validatedData.firstName,
|
||||
last_name: validatedData.lastName,
|
||||
phone: validatedData.phone || null,
|
||||
cpf: null, // CPF não é mais obrigatório
|
||||
},
|
||||
select: {
|
||||
user_id: true,
|
||||
username: true,
|
||||
email: true,
|
||||
},
|
||||
});
|
||||
// Usar transação Prisma
|
||||
const newUser = await prisma.$transaction(async (tx) => {
|
||||
// Criar usuário
|
||||
const user = await tx.user.create({
|
||||
data: {
|
||||
username: validatedData.username,
|
||||
email: validatedData.email,
|
||||
password_hash: passwordHash,
|
||||
first_name: validatedData.firstName,
|
||||
last_name: validatedData.lastName,
|
||||
phone: validatedData.phone || null,
|
||||
cpf: null, // CPF não é mais obrigatório
|
||||
},
|
||||
select: {
|
||||
user_id: true,
|
||||
username: true,
|
||||
email: true,
|
||||
},
|
||||
});
|
||||
|
||||
newUser = {
|
||||
userId: user.user_id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
};
|
||||
const userData = {
|
||||
userId: user.user_id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
};
|
||||
|
||||
// Criar personagens (primeiro é main por padrão)
|
||||
for (let i = 0; i < validatedData.servers.length; i++) {
|
||||
const serverData = validatedData.servers[i];
|
||||
const isMain = i === 0 || serverData.isMain === true;
|
||||
// Criar personagens (primeiro é main por padrão)
|
||||
for (let i = 0; i < validatedData.servers.length; i++) {
|
||||
const serverData = validatedData.servers[i];
|
||||
const isMain = i === 0 || serverData.isMain === true;
|
||||
|
||||
// Se este personagem será main e não é o primeiro, desmarcar outros
|
||||
if (isMain && i > 0) {
|
||||
await tx.userCharacter.updateMany({
|
||||
where: { user_id: newUser.userId },
|
||||
data: { is_main: false },
|
||||
});
|
||||
}
|
||||
|
||||
// Inserir personagem
|
||||
const character = await tx.userCharacter.create({
|
||||
data: {
|
||||
user_id: newUser.userId,
|
||||
character_name: serverData.characterName,
|
||||
server_name: serverData.serverName,
|
||||
is_main: isMain,
|
||||
},
|
||||
});
|
||||
|
||||
createdCharacters.push({
|
||||
characterName: character.character_name,
|
||||
serverName: character.server_name,
|
||||
// Se este personagem será main e não é o primeiro, desmarcar outros
|
||||
if (isMain && i > 0) {
|
||||
await tx.userCharacter.updateMany({
|
||||
where: { user_id: userData.userId },
|
||||
data: { is_main: false },
|
||||
});
|
||||
}
|
||||
|
||||
// Incrementar total_users dos servidores (dentro da transação)
|
||||
await tx.server.updateMany({
|
||||
where: {
|
||||
server_name: { in: uniqueServerNames },
|
||||
},
|
||||
// Inserir personagem
|
||||
const character = await tx.userCharacter.create({
|
||||
data: {
|
||||
total_users: { increment: 1 },
|
||||
last_activity_at: new Date(),
|
||||
user_id: userData.userId,
|
||||
character_name: serverData.characterName,
|
||||
server_name: serverData.serverName,
|
||||
is_main: isMain,
|
||||
},
|
||||
});
|
||||
|
||||
createdCharacters.push({
|
||||
characterName: character.character_name,
|
||||
serverName: character.server_name,
|
||||
});
|
||||
}
|
||||
|
||||
// Incrementar total_users dos servidores (dentro da transação)
|
||||
await tx.server.updateMany({
|
||||
where: {
|
||||
server_name: { in: uniqueServerNames },
|
||||
},
|
||||
data: {
|
||||
total_users: { increment: 1 },
|
||||
last_activity_at: new Date(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// Prisma já faz rollback automático em caso de erro
|
||||
throw error;
|
||||
}
|
||||
|
||||
return userData;
|
||||
});
|
||||
|
||||
// Gerar tokens
|
||||
const accessToken = generateAccessToken(newUser.userId, newUser.username);
|
||||
|
||||
Reference in new issue
Block a user