|
@@ -1,4 +1,5 @@
|
|
|
import bcrypt from "bcryptjs";
|
|
import bcrypt from "bcryptjs";
|
|
|
|
|
+import { AppError, NotFoundError, BadRequestError } from "../shared/errors/index.js";
|
|
|
import { Usuario } from "../models/Usuario.model.js";
|
|
import { Usuario } from "../models/Usuario.model.js";
|
|
|
import { formatarUsuario } from "../utils/formatarUsuario.js";
|
|
import { formatarUsuario } from "../utils/formatarUsuario.js";
|
|
|
|
|
|
|
@@ -24,9 +25,7 @@ export const UsuarioController = {
|
|
|
|
|
|
|
|
const loginTrimado = Login.trim();
|
|
const loginTrimado = Login.trim();
|
|
|
const existente = await Usuario.query().findOne({ Login: loginTrimado });
|
|
const existente = await Usuario.query().findOne({ Login: loginTrimado });
|
|
|
- if (existente) {
|
|
|
|
|
- return res.status(409).send({ status: false, msg: "Login já cadastrado!" });
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (existente) throw new AppError("Login já cadastrado!", 409);
|
|
|
|
|
|
|
|
const senhaHash = await bcrypt.hash(Senha, 10);
|
|
const senhaHash = await bcrypt.hash(Senha, 10);
|
|
|
const usuario = await Usuario.query().insert({
|
|
const usuario = await Usuario.query().insert({
|
|
@@ -48,7 +47,7 @@ export const UsuarioController = {
|
|
|
Atualizar: async function (req, res, next) {
|
|
Atualizar: async function (req, res, next) {
|
|
|
try {
|
|
try {
|
|
|
const id = Number(req.params.id);
|
|
const id = Number(req.params.id);
|
|
|
- if (!id) return res.status(400).send({ status: false, msg: "ID inválido!" });
|
|
|
|
|
|
|
+ if (!id) throw new BadRequestError("ID inválido");
|
|
|
|
|
|
|
|
const { Nome, Email, Nivel, Setor } = req.body;
|
|
const { Nome, Email, Nivel, Setor } = req.body;
|
|
|
const updates = {};
|
|
const updates = {};
|
|
@@ -58,7 +57,7 @@ export const UsuarioController = {
|
|
|
if (Setor !== undefined) updates.Setor = Setor.trim();
|
|
if (Setor !== undefined) updates.Setor = Setor.trim();
|
|
|
|
|
|
|
|
const usuario = await Usuario.query().patchAndFetchById(id, updates);
|
|
const usuario = await Usuario.query().patchAndFetchById(id, updates);
|
|
|
- if (!usuario) return res.status(404).send({ status: false, msg: "Usuário não encontrado!" });
|
|
|
|
|
|
|
+ if (!usuario) throw new NotFoundError("Usuário não encontrado");
|
|
|
|
|
|
|
|
return res.status(200).send({ status: true, usuario: formatarUsuario(usuario) });
|
|
return res.status(200).send({ status: true, usuario: formatarUsuario(usuario) });
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -69,10 +68,10 @@ export const UsuarioController = {
|
|
|
ToggleStatus: async function (req, res, next) {
|
|
ToggleStatus: async function (req, res, next) {
|
|
|
try {
|
|
try {
|
|
|
const id = Number(req.params.id);
|
|
const id = Number(req.params.id);
|
|
|
- if (!id) return res.status(400).send({ status: false, msg: "ID inválido!" });
|
|
|
|
|
|
|
+ if (!id) throw new BadRequestError("ID inválido");
|
|
|
|
|
|
|
|
const usuario = await Usuario.query().findById(id);
|
|
const usuario = await Usuario.query().findById(id);
|
|
|
- if (!usuario) return res.status(404).send({ status: false, msg: "Usuário não encontrado!" });
|
|
|
|
|
|
|
+ if (!usuario) throw new NotFoundError("Usuário não encontrado");
|
|
|
|
|
|
|
|
const novoStatus = String(usuario.Status) === "1" ? "0" : "1";
|
|
const novoStatus = String(usuario.Status) === "1" ? "0" : "1";
|
|
|
await Usuario.query().findById(id).patch({ Status: novoStatus });
|
|
await Usuario.query().findById(id).patch({ Status: novoStatus });
|
|
@@ -86,23 +85,21 @@ export const UsuarioController = {
|
|
|
AlterarSenha: async function (req, res, next) {
|
|
AlterarSenha: async function (req, res, next) {
|
|
|
try {
|
|
try {
|
|
|
const id = Number(req.params.id);
|
|
const id = Number(req.params.id);
|
|
|
- if (!id) return res.status(400).send({ status: false, msg: "ID inválido!" });
|
|
|
|
|
|
|
+ if (!id) throw new BadRequestError("ID inválido");
|
|
|
|
|
|
|
|
const callerIsOwner = String(req.user?.sub) === String(id);
|
|
const callerIsOwner = String(req.user?.sub) === String(id);
|
|
|
const callerIsAdmin = String(req.user?.nivel) === "3";
|
|
const callerIsAdmin = String(req.user?.nivel) === "3";
|
|
|
if (!callerIsOwner && !callerIsAdmin) {
|
|
if (!callerIsOwner && !callerIsAdmin) {
|
|
|
- return res.status(403).send({ status: false, msg: "Sem permissão para alterar a senha deste usuário." });
|
|
|
|
|
|
|
+ throw new AppError("Sem permissão para alterar a senha deste usuário", 403);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const { senhaAtual, senhaNova } = req.body;
|
|
const { senhaAtual, senhaNova } = req.body;
|
|
|
|
|
|
|
|
const usuario = await Usuario.query().findById(id);
|
|
const usuario = await Usuario.query().findById(id);
|
|
|
- if (!usuario) return res.status(404).send({ status: false, msg: "Usuário não encontrado!" });
|
|
|
|
|
|
|
+ if (!usuario) throw new NotFoundError("Usuário não encontrado");
|
|
|
|
|
|
|
|
const senhaValida = bcrypt.compareSync(senhaAtual, String(usuario.Senha ?? ""));
|
|
const senhaValida = bcrypt.compareSync(senhaAtual, String(usuario.Senha ?? ""));
|
|
|
- if (!senhaValida) {
|
|
|
|
|
- return res.status(401).send({ status: false, msg: "Senha atual incorreta!" });
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!senhaValida) throw new AppError("Senha atual incorreta", 401);
|
|
|
|
|
|
|
|
const novaHash = bcrypt.hashSync(senhaNova, 10);
|
|
const novaHash = bcrypt.hashSync(senhaNova, 10);
|
|
|
await Usuario.query().findById(id).patch({ Senha: novaHash });
|
|
await Usuario.query().findById(id).patch({ Senha: novaHash });
|