diff --git a/backend/src/modules/auditLogs/auditLog.interface.ts b/backend/src/modules/auditLogs/auditLog.interface.ts new file mode 100644 index 0000000..d9bfef0 --- /dev/null +++ b/backend/src/modules/auditLogs/auditLog.interface.ts @@ -0,0 +1,48 @@ +import { ObjectId } from "mongoose"; + +export interface IAuditLog { + + _id?: ObjectId; + + userId: ObjectId; + + module: + | "User" + | "Role" + | "FuelRequest" + | "Approval" + | "ExternalInvoice" + | "LPG" + | "PumpReading" + | "FuelPump" + | "FuelPrice" + | "FuelType" + | "VehicleConfig" + | "VehicleAssignment" + | "Dashboard" + | "Report" + | "Authentication"; + + action: + | "CREATE" + | "UPDATE" + | "DELETE" + | "APPROVE" + | "REJECT" + | "LOGIN" + | "LOGOUT" + | "CLOSE"; + + recordId?: ObjectId; + + description: string; + + ipAddress?: string; + + status: + | "EXITOSO" + | "FALLIDO"; + + createdAt?: Date; + +} \ No newline at end of file diff --git a/backend/src/modules/auditLogs/auditLog.model.ts b/backend/src/modules/auditLogs/auditLog.model.ts new file mode 100644 index 0000000..a15842e --- /dev/null +++ b/backend/src/modules/auditLogs/auditLog.model.ts @@ -0,0 +1,135 @@ +import mongoose from "mongoose"; + +const auditLogSchema = new mongoose.Schema( + + { + + userId: { + + type: mongoose.Schema.Types.ObjectId, + + ref: "User" + + }, + + module: { + + type: String, + + required: true, + + enum: [ + + "User", + + "Role", + + "FuelRequest", + + "Approval", + + "ExternalInvoice", + + "LPG", + + "PumpReading", + + "FuelPump", + + "FuelPrice", + + "FuelType", + + "VehicleConfig", + + "VehicleAssignment", + + "Dashboard", + + "Report", + + "Authentication" + + ] + + }, + + action: { + + type: String, + + required: true, + + enum: [ + + "CREATE", + + "UPDATE", + + "DELETE", + + "APPROVE", + + "REJECT", + + "LOGIN", + + "LOGOUT", + + "CLOSE" + + ] + + }, + + recordId: { + + type: mongoose.Schema.Types.ObjectId + + }, + + description: { + + type: String, + + required: true + + }, + + ipAddress: { + + type: String + + }, + + status: { + + type: String, + + required: true, + + enum: [ + + "SUCCESS", + + "FAILED" + + ], + + default: "SUCCESS" + + } + + }, + + { + + timestamps: true, + + versionKey: false + + } + +); + +export default mongoose.model("AuditLog", auditLogSchema); \ No newline at end of file diff --git a/backend/src/modules/auditLogs/auditLog.service.ts b/backend/src/modules/auditLogs/auditLog.service.ts new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/modules/roles/role.controller.ts b/backend/src/modules/roles/role.controller.ts index 7646a6c..2dab603 100644 --- a/backend/src/modules/roles/role.controller.ts +++ b/backend/src/modules/roles/role.controller.ts @@ -10,9 +10,38 @@ export class RoleController { req:Request, res:Response ){ - const roles = - await roleService.getAll(); + try{ + const roles = await roleService.getAll(); + res.json(roles); + + } catch(error){ + res.status(500).json({ + message:"Error al obtener roles", + error + }) + + } + } + + async getById( + req:Request, + res:Response + ){ + try{ + const role = await roleService.getById( + req.params.id as string + ); + res.json(role); + + }catch(error){ + res.status(500).json({ + message:"Error al obtener roles", + error + }) + + } + } async create( @@ -20,11 +49,73 @@ export class RoleController { req:Request, res:Response ){ - const role = - await roleService.create( - req.body - ); + try + { + const role = await roleService.create( + req.body + ); - res.status(201).json(role); + res.status(201).json(role); + + } catch(error){ + res.status(500).json({ + message:"Error al crear role" + }); + + } + } + + async update( + + req:Request, + res:Response + ){ + try + { + const role = await roleService.update( + req.params.id as string, + req.body + ) + + if(!role){ + return res.status(404).json({ + message:"Rol no encontrado" + }); + } + + res.status(200).json(role); + } catch(error) { + res.status(500).json({ + message:"Error al actualizar rol" + }); + + } + } + + async delete ( + req:Request, + res:Response + ){ + try{ + const role = await roleService.delete( + req.params.id as string + ) + + if(!role){ + return res.status(404).json({ + message:"Role no encontrado" + }); + } + res.json({ + message:"Rol eliminado correctamente" + }); + + }catch(error){ + res.status(500).json({ + message:"Error al eliminar rol", + error + }); + + } } } \ No newline at end of file diff --git a/backend/src/modules/roles/role.interface.ts b/backend/src/modules/roles/role.interface.ts index 02000ab..9ecd8b2 100644 --- a/backend/src/modules/roles/role.interface.ts +++ b/backend/src/modules/roles/role.interface.ts @@ -1,6 +1,8 @@ export interface IRole { - roleName:string; - description:string; - active:boolean; + roleName: string; + + description: string; + + active: boolean; } \ No newline at end of file diff --git a/backend/src/modules/roles/role.routes.ts b/backend/src/modules/roles/role.routes.ts index 7253994..56ad24d 100644 --- a/backend/src/modules/roles/role.routes.ts +++ b/backend/src/modules/roles/role.routes.ts @@ -1,13 +1,19 @@ import { Router } from "express"; - import { RoleController } from "./role.controller"; +import { authMiddleware } from "../auth/auth.middleware"; const roleRouter = Router(); const controller = new RoleController(); -roleRouter.get("/", controller.getAll); +roleRouter.get("/", authMiddleware, controller.getAll); -roleRouter.post("/", controller.create); +roleRouter.get("/:id", authMiddleware, controller.getById); -export default roleRouter ; \ No newline at end of file +roleRouter.post("/", authMiddleware, controller.create); + +roleRouter.put("/:id", authMiddleware, controller.update); + +roleRouter.delete("/:id", authMiddleware, controller.delete); + +export default roleRouter; \ No newline at end of file diff --git a/backend/src/modules/roles/role.service.ts b/backend/src/modules/roles/role.service.ts index f36586f..b8e0df2 100644 --- a/backend/src/modules/roles/role.service.ts +++ b/backend/src/modules/roles/role.service.ts @@ -1,4 +1,5 @@ import roleModel from "./role.model"; +import { IRole } from "./role.interface"; export class RoleService { async getAll() { @@ -6,7 +7,34 @@ export class RoleService { return await roleModel.find(); } - async create(data:any) { + async getById( + id:string + ){ + return await roleModel.findById(id); + } + + async create( + data:IRole + ) { return await roleModel.create(data); } + + async update( + id:string, + data:Partial + ){ + return await roleModel.findByIdAndUpdate( + id, + data, + { + new:true + } + ) + } + + async delete( + id:string + ){ + return await roleModel.findByIdAndDelete(id) + } } \ No newline at end of file