Implementacion modulo de Fuel-Types

This commit is contained in:
2026-06-03 22:57:53 +00:00
parent d9584c2861
commit 7741bf06c5
8 changed files with 231 additions and 3 deletions

View File

@ -1,8 +1,11 @@
import express from "express";
import cors from "cors";
import roleRouter from "./modules/roles/role.routes";
import userRouter from "./modules/user/user.routes";
import authRouter from "./modules/auth/auth.routes";
import fuelRoutes from "./modules/fuel-types/fuelType.routes";
const app = express();
@ -12,6 +15,7 @@ app.use(express.json());
app.use("/api/roles", roleRouter);
app.use("/api/user", userRouter);
app.use("/api/fuel", fuelRoutes);
app.use("/api/auth", authRouter);

View File

@ -1,6 +1,7 @@
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import userModel from "../user/user.model";
import { resolve } from "dns/promises";
export class AuthService {
@ -11,8 +12,9 @@ export class AuthService {
const user =
await userModel.findOne({
userName
});
}).populate("roleId");
//Validador de usuario existente
if (!user) {
throw new Error("Usuario no encontrado");
}
@ -30,11 +32,15 @@ export class AuthService {
user.lastLogin = new Date();
await user.save();
const role = user.roleId as any;
const token = jwt.sign(
{
id:user._id,
roleId: user.roleId
roleId: user.roleId,
roleName:role.roleName
},
process.env.JWT_SECRET as string,
@ -45,7 +51,19 @@ export class AuthService {
return {
token,
user
user:{
id: user._id,
employeeId:user.employeeId,
userName:user.userName,
roleId: role._id,
rolename: role.roleName,
active: user.active
}
};
}
}

View File

@ -0,0 +1,72 @@
import { Request, Response } from "express";
import { FuelTypeService } from "./fuelType.service";
const fuelTypeService = new FuelTypeService();
export class FuelTypeController {
//Obtener los datos de combustible
async getAll(
req:Request,
res:Response
){
const fuelTypes = await fuelTypeService.getAll();
res.json(fuelTypes);
}
//Obtener tipo de combustible por ID
async getById(
req:Request,
res:Response
) {
const fuelType = await fuelTypeService.getById(
req.params.id as string
);
res.json(fuelType);
}
//Crear tipo de combustible
async create(
req:Request,
res:Response
) {
const fuelType = await fuelTypeService.create(
req.body
);
res.status(201).json(fuelType);
}
async update(
req:Request,
res:Response
){
const fuelType = fuelTypeService.update(
req.params.id as string,
req.body
)
res.json(fuelType);
}
async delete (
req:Request,
res:Response
){
const fuelType = fuelTypeService.delete(
req.params.id as string
)
res.json({
message:"Tipo de combustible eliminado"
});
}
}

View File

@ -0,0 +1,6 @@
export interface IFuelType {
fuelName:string;
unit:string;
active:boolean;
}

View File

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

View File

@ -0,0 +1,19 @@
import { Router } from "express";
import { FuelTypeController } from "./fuelType.controller";
import { authMiddleware } from "../auth/auth.middleware";
const fuelRoutes = Router();
const controller = new FuelTypeController();
fuelRoutes.get("/", authMiddleware, controller.getAll);
fuelRoutes.get("/id", authMiddleware, controller.getById);
fuelRoutes.post("/", authMiddleware, controller.create);
fuelRoutes.put("/id", authMiddleware, controller.update);
fuelRoutes.delete("/id", authMiddleware, controller.delete);
export default fuelRoutes;

View File

@ -0,0 +1,54 @@
import fuelTypeModel from "./fuelType.model";
export class FuelTypeService {
//Obtener datos
async getAll() {
return await fuelTypeModel.find();
}
//Obtener por id
async getById(
id:string
){
return fuelTypeModel.findById(
id
);
}
//Crear
async create (
data:any
) {
return await fuelTypeModel.create(
data
);
}
//Actualizar
async update(
id:String,
data:any
) {
return await fuelTypeModel.findByIdAndUpdate (
id,
data,
{
new:true
}
);
}
//Eliminar
async delete (
id:string,
){
return await fuelTypeModel.findByIdAndDelete(
id
);
}
}

View File

@ -0,0 +1,29 @@
import { Request, Response, NextFunction } from "express";
export const roleMiddleware =
(allowedRoles: string[]) => {
return (
req: any,
res: Response,
next: NextFunction
) => {
const userRole =
req.user.roleName;
if (
!allowedRoles.includes(
userRole
)
) {
return res.status(403).json({
message:
"No tiene permisos para realizar esta acción"
});
}
next();
};
};