Actualizar CRUD módulo de tipos de combustibles
This commit is contained in:
@ -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,7 +55,6 @@ export class AuthService {
|
||||
|
||||
id:user._id,
|
||||
roleId: user.roleId,
|
||||
|
||||
roleName: role.roleName
|
||||
},
|
||||
|
||||
@ -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"
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -12,9 +12,16 @@ export class FuelTypeController {
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const fuelTypes = await fuelTypeService.getAll();
|
||||
|
||||
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
|
||||
) {
|
||||
try{
|
||||
const fuelType = await fuelTypeService.getById(
|
||||
req.params.id as string
|
||||
);
|
||||
|
||||
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
|
||||
) {
|
||||
try{
|
||||
const fuelType = await fuelTypeService.create(
|
||||
req.body
|
||||
);
|
||||
|
||||
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(
|
||||
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(
|
||||
try{
|
||||
const fuelType = await fuelTypeService.delete(
|
||||
req.params.id as string
|
||||
)
|
||||
);
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
export interface IFuelType {
|
||||
|
||||
fuelName:string;
|
||||
|
||||
unit:string;
|
||||
|
||||
active:boolean;
|
||||
}
|
||||
@ -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;
|
||||
@ -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<IFuelType>
|
||||
) {
|
||||
return await fuelTypeModel.findByIdAndUpdate (
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user