Implementar modulo de Roles y Usuarios

This commit is contained in:
2026-06-02 23:19:34 +00:00
parent 8b306b9afc
commit c6f3f3d135
17 changed files with 405 additions and 2 deletions

View File

@ -0,0 +1,30 @@
import { Request, Response } from "express";
import { RoleService } from "./role.service";
const roleService = new RoleService();
export class RoleController {
async getAll(
req:Request,
res:Response
){
const roles =
await roleService.getAll();
res.json(roles);
}
async create(
req:Request,
res:Response
){
const role =
await roleService.create(
req.body
);
res.status(201).json(role);
}
}

View File

@ -0,0 +1,6 @@
export interface IRole {
roleName:string;
description:string;
active:boolean;
}

View File

@ -0,0 +1,26 @@
import mongoose, { Schema } from "mongoose";
const roleSchema = new mongoose.Schema({
roleName:{
type:String,
required:true,
unique:true
},
description:{
type:String
},
active:{
type:Boolean,
default:true
}
},
{
timestamps:true
}
);
export default mongoose.model("Role", roleSchema);

View File

@ -0,0 +1,13 @@
import { Router } from "express";
import { RoleController } from "./role.controller";
const roleRoutes = Router();
const controller = new RoleController();
roleRoutes.get("/", controller.getAll);
roleRoutes.post("/", controller.create);
export default roleRoutes ;

View File

@ -0,0 +1,12 @@
import roleModel from "./role.model";
export class RoleService {
async getAll() {
return await roleModel.find();
}
async create(data:any) {
return await roleModel.create(data);
}
}

View File

@ -0,0 +1,66 @@
import { Request, Response } from "express";
import { UserService } from "./user.service";
const userService = new UserService();
export class UserController {
async getAll(
req:Request,
res:Response
) {
const users =
await userService.getAll();
res.json(users);
}
async getById(
req:Request,
res:Response
){
const user =
await userService.getById(
req.params.id as string
);
res.json(user);
}
async create (
req:Request,
res:Response
) {
const user =
await userService.create(
req.body
);
res.status(201).json(user);
}
async update(
req:Request,
res:Response
){
const user =
await userService.update(
req.params.id as string,
req.body
);
res.json(user);
}
async detele (
req:Request,
res:Response
) {
await userService.delete(
req.params.id as string
);
res.json({message: "Usuario eliminado"});
}
}

View File

@ -0,0 +1,11 @@
import { ObjectId } from "mongoose";
export interface IUser {
employeeId:number;
userName:string;
passwordHash:string;
roleId:ObjectId;
active:boolean;
lastLogin:Date;
}

View File

@ -0,0 +1,46 @@
import mongoose from "mongoose";
const userSchema = new mongoose.Schema({
employeeId:{
type:Number,
required:true,
unique:true,
},
userName:{
type:String,
required:true,
unique:true,
},
passwordHash:{
type:String,
required:true
},
roleId:{
type:mongoose.Types.ObjectId,
ref:"Role",
required:true
},
active:{
type:Boolean,
default:true
},
lastLogin:{
type:Date
}
},
{
timestamps:true
}
);
export default mongoose.model("User", userSchema);

View File

@ -0,0 +1,19 @@
import { Router } from "express";
import { UserController } from "./user.controller";
const userRoutes = Router();
const controller = new UserController();
userRoutes.get("/", controller.getAll);
userRoutes.get("/:id", controller.getById);
userRoutes.post("/", controller.create);
userRoutes.put("/:id", controller.update);
userRoutes.delete("/:id", controller.detele);
export default userRoutes;

View File

@ -0,0 +1,46 @@
import userModel from "./user.model";
export class UserService {
async getAll() {
return await userModel
.find()
.populate("roleId");
}
async getById(
id:string
) {
return await userModel
.find()
.populate("roleId");
}
async create(
data:any
) {
return await userModel.create(data);
}
async update(
id:string,
data:any
) {
return await userModel.findByIdAndUpdate(
id,
data,
{
new:true
}
);
}
async delete(
id:string
) {
return await userModel.findByIdAndDelete(
id
);
}
}