Actualización CRUD módulo roles y creación módulo auditLog
This commit is contained in:
48
backend/src/modules/auditLogs/auditLog.interface.ts
Normal file
48
backend/src/modules/auditLogs/auditLog.interface.ts
Normal file
@ -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;
|
||||
|
||||
}
|
||||
135
backend/src/modules/auditLogs/auditLog.model.ts
Normal file
135
backend/src/modules/auditLogs/auditLog.model.ts
Normal file
@ -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);
|
||||
0
backend/src/modules/auditLogs/auditLog.service.ts
Normal file
0
backend/src/modules/auditLogs/auditLog.service.ts
Normal file
@ -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(
|
||||
try
|
||||
{
|
||||
const role = await roleService.create(
|
||||
req.body
|
||||
);
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
export interface IRole {
|
||||
|
||||
roleName:string;
|
||||
description:string;
|
||||
active:boolean;
|
||||
roleName: string;
|
||||
|
||||
description: string;
|
||||
|
||||
active: boolean;
|
||||
}
|
||||
@ -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 ;
|
||||
roleRouter.post("/", authMiddleware, controller.create);
|
||||
|
||||
roleRouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
roleRouter.delete("/:id", authMiddleware, controller.delete);
|
||||
|
||||
export default roleRouter;
|
||||
@ -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<IRole>
|
||||
){
|
||||
return await roleModel.findByIdAndUpdate(
|
||||
id,
|
||||
data,
|
||||
{
|
||||
new:true
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async delete(
|
||||
id:string
|
||||
){
|
||||
return await roleModel.findByIdAndDelete(id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user