Skip to content
Snippets Groups Projects
Commit 9c129caf authored by Ricardo's avatar Ricardo
Browse files

FIX update and delete user routes fixed

parent f55d87ae
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,21 @@ import bcrypt from "bcrypt";
import jwt from 'jsonwebtoken';
export default class User{
static userRequestValidation (req: Request){ //valida a requisição do usuário
const validation = userSchema.safeParse(req.body);
static userRequestValidation (req: Request, update: boolean){ //valida a requisição do usuário
let validation;
if (update)
validation = updateUserSchema.safeParse(req.body);
else
validation = userSchema.safeParse(req.body);
return validation.success;
}
static userUpdateValidation(req: Request) {
const validation = updateUserSchema.safeParse(req.body);
if (!validation.success) {
console.error("Erro de validação:", validation.error.format());
}
return validation.success;
}
......@@ -24,7 +37,7 @@ export default class User{
static async updateUser (req: Request, res:Response): Promise<any>{
if (!this.userRequestValidation(req))
if (!User.userRequestValidation(req, true))
{
return res.status(400).json({ erro: "Invalid Request" });
}
......@@ -35,7 +48,7 @@ export default class User{
return res.status(400).json({ error: "ID inválido" });
}
if (!(await this.userExistenceValidation(parsedId)))
if (!(await User.userExistenceValidation(parsedId)))
{
return res.status(404).json({ error: "Not Found "});
}
......@@ -46,7 +59,7 @@ export default class User{
hashedPassword = await bcrypt.hash(req.body.password, 10);
}
const user_updated = await this.getUser(id);
const user_updated = await User.getUser(parsedId);
const updates: Partial<typeof usersTable.$inferInsert> = {
name: req.body.name || user_updated.name,
......@@ -59,24 +72,25 @@ export default class User{
cyberLimit: req.body.cyberLimit || user_updated.cyberLimit,
};
await db.update(usersTable).set(updates).where(eq(usersTable.id, id));
await db.update(usersTable).set(updates).where(eq(usersTable.id, parsedId));
return res.status(200).json({ message: "User Updated Successfully "});
}
static async deleteUser (id: number, req: Request, res: Response): Promise<any>{
if (!this.userRequestValidation(req))
{
return res.status(400).json({ error: "Invalid Request" });;
static async deleteUser (req: Request, res: Response): Promise<any>{
const { id } = req.params;
const parsedId = parseInt(id, 10);
if (isNaN(parsedId)) {
return res.status(400).json({ error: "ID inválido" });
}
if (!(await this.userExistenceValidation(id)))
if (!(await User.userExistenceValidation(parsedId)))
{
return res.status(404).json({ error: "Not Found"});
}
try{
await db.delete(usersTable).where(eq(usersTable.id, id));
await db.delete(usersTable).where(eq(usersTable.id, parsedId));
return res.status(200).json({ message: "User removed successfully! "});
} catch (error){
return res.status(408).json({ error: "Error during user deletion" });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment