diff --git a/backend/src/modules/auth/auth.service.ts b/backend/src/modules/auth/auth.service.ts index 264ad95..75dc2e9 100644 --- a/backend/src/modules/auth/auth.service.ts +++ b/backend/src/modules/auth/auth.service.ts @@ -1,6 +1,7 @@ import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; import userModel from "../user/user.model"; +import auditLogModel from "../auditLogs/auditLog.model"; export class AuthService { @@ -18,8 +19,13 @@ export class AuthService { throw new Error("Usuario no encontrado"); } - const validPassword = - await bcrypt.compare( + //Validar si el usuario está activo + if(!user.active){ + throw new Error("Usuario inactivo") + } + + //Validar contraseña + const validPassword =await bcrypt.compare( password, user.passwordHash ); @@ -31,6 +37,17 @@ export class AuthService { user.lastLogin = new Date(); await user.save(); + //Registrar login en auditoría + + await auditLogModel.create({ + userId: user._id, + module:"Authentication", + action:"LOGIN", + description:"El usuario inició sesión.", + status:"EXITOSO" + + }); + const role = user.roleId as any; const token = jwt.sign( @@ -38,8 +55,7 @@ export class AuthService { id:user._id, roleId: user.roleId, - - roleName:role.roleName + roleName: role.roleName }, process.env.JWT_SECRET as string, @@ -59,10 +75,26 @@ export class AuthService { roleId: role._id, - rolename: role.roleName, + roleName: role.roleName, active: user.active } }; } + + async logout ( + userId:string + ){ + await auditLogModel.create({ + userId, + module:"Authentication", + action:"LOGOUT", + description:"El usuario cerró sesión.", + status:"EXITOSO" + }); + + return{ + message:"Sesión cerrada exitosamente" + }; + } } \ No newline at end of file diff --git a/backend/src/modules/fuel-types/fuelType.controller.ts b/backend/src/modules/fuel-types/fuelType.controller.ts index 3e3243a..bbef3bc 100644 --- a/backend/src/modules/fuel-types/fuelType.controller.ts +++ b/backend/src/modules/fuel-types/fuelType.controller.ts @@ -12,9 +12,16 @@ export class FuelTypeController { req:Request, res:Response ){ - const fuelTypes = await fuelTypeService.getAll(); + try{ + const fuelTypes = await fuelTypeService.getAll(); + res.json(fuelTypes); - res.json(fuelTypes); + }catch(error){ + res.status(500).json({ + message:"Error al obtener tipo de combustible", + error + }); + } } //Obtener tipo de combustible por ID @@ -23,24 +30,44 @@ export class FuelTypeController { req:Request, res:Response ) { - const fuelType = await fuelTypeService.getById( - req.params.id as string - ); + try{ + const fuelType = await fuelTypeService.getById( + req.params.id as string + ); - res.json(fuelType); + if (!fuelType) { + return res.status(404).json({ + message: "Tipo de combustible no encontrado" + }); + } + + res.json(fuelType); + } catch(error){ + res.status(500).json({ + message:"Error al obtener tipo de combustible", + error + }) + } } //Crear tipo de combustible - async create( req:Request, res:Response ) { - const fuelType = await fuelTypeService.create( - req.body - ); + try{ + const fuelType = await fuelTypeService.create( + req.body + ); - res.status(201).json(fuelType); + res.status(201).json(fuelType); + + } catch(error){ + res.status(500).json({ + message:"Error al crear tipo de combustible", + error + }); + } } //Actualizar tipo de combustible @@ -49,12 +76,27 @@ export class FuelTypeController { res:Response ){ - const fuelType = fuelTypeService.update( - req.params.id as string, - req.body - ) + try{ + const fuelType = await fuelTypeService.update( + req.params.id as string, + req.body + ); - res.json(fuelType); + if(!fuelType){ + return res.status(404).json({ + message:"Tipo de combustible no encontrado" + }); + } + + res.status(200).json(fuelType); + + }catch(error){ + res.status(500).json({ + message:"Error al actualizar tipo de combustible", + error + }); + + } } //Eliminar tipo de combustible @@ -63,12 +105,25 @@ export class FuelTypeController { res:Response ){ - const fuelType = fuelTypeService.delete( - req.params.id as string - ) + try{ + const fuelType = await fuelTypeService.delete( + req.params.id as string + ); - res.json({ - message:"Tipo de combustible eliminado" - }); + if(!fuelType){ + return res.status(404).json({ + message:"Tipo de combustible no encontrado" + }); + } + + res.json({ + message:"Tipo de combustible eliminado" + }); + } catch(error){ + res.status(500).json({ + message:"Error al eliminar tipo de combustible", + error + }) + } } } \ No newline at end of file diff --git a/backend/src/modules/fuel-types/fuelType.interface.ts b/backend/src/modules/fuel-types/fuelType.interface.ts index fcb1189..4028abe 100644 --- a/backend/src/modules/fuel-types/fuelType.interface.ts +++ b/backend/src/modules/fuel-types/fuelType.interface.ts @@ -1,6 +1,8 @@ export interface IFuelType { fuelName:string; + unit:string; + active:boolean; } \ No newline at end of file diff --git a/backend/src/modules/fuel-types/fuelType.routes.ts b/backend/src/modules/fuel-types/fuelType.routes.ts index a10c6f8..c5345f9 100644 --- a/backend/src/modules/fuel-types/fuelType.routes.ts +++ b/backend/src/modules/fuel-types/fuelType.routes.ts @@ -8,12 +8,12 @@ const controller = new FuelTypeController(); fuelRoutes.get("/", authMiddleware, controller.getAll); -fuelRoutes.get("/id", authMiddleware, controller.getById); +fuelRoutes.get("/:id", authMiddleware, controller.getById); fuelRoutes.post("/", authMiddleware, controller.create); -fuelRoutes.put("/id", authMiddleware, controller.update); +fuelRoutes.put("/:id", authMiddleware, controller.update); -fuelRoutes.delete("/id", authMiddleware, controller.delete); +fuelRoutes.delete("/:id", authMiddleware, controller.delete); export default fuelRoutes; \ No newline at end of file diff --git a/backend/src/modules/fuel-types/fuelType.service.ts b/backend/src/modules/fuel-types/fuelType.service.ts index e797149..d2b3ca7 100644 --- a/backend/src/modules/fuel-types/fuelType.service.ts +++ b/backend/src/modules/fuel-types/fuelType.service.ts @@ -1,4 +1,5 @@ import fuelTypeModel from "./fuelType.model"; +import { IFuelType } from "./fuelType.interface"; export class FuelTypeService { @@ -21,7 +22,7 @@ export class FuelTypeService { //Crear async create ( - data:any + data:IFuelType ) { return await fuelTypeModel.create( data @@ -30,8 +31,8 @@ export class FuelTypeService { //Actualizar async update( - id:String, - data:any + id:string, + data:Partial ) { return await fuelTypeModel.findByIdAndUpdate ( id,