Actualizar CRUD módulo de tipos de combustibles
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import bcrypt from "bcrypt";
|
import bcrypt from "bcrypt";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import userModel from "../user/user.model";
|
import userModel from "../user/user.model";
|
||||||
|
import auditLogModel from "../auditLogs/auditLog.model";
|
||||||
|
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
|
|
||||||
@ -18,8 +19,13 @@ export class AuthService {
|
|||||||
throw new Error("Usuario no encontrado");
|
throw new Error("Usuario no encontrado");
|
||||||
}
|
}
|
||||||
|
|
||||||
const validPassword =
|
//Validar si el usuario está activo
|
||||||
await bcrypt.compare(
|
if(!user.active){
|
||||||
|
throw new Error("Usuario inactivo")
|
||||||
|
}
|
||||||
|
|
||||||
|
//Validar contraseña
|
||||||
|
const validPassword =await bcrypt.compare(
|
||||||
password,
|
password,
|
||||||
user.passwordHash
|
user.passwordHash
|
||||||
);
|
);
|
||||||
@ -31,6 +37,17 @@ export class AuthService {
|
|||||||
user.lastLogin = new Date();
|
user.lastLogin = new Date();
|
||||||
await user.save();
|
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 role = user.roleId as any;
|
||||||
|
|
||||||
const token = jwt.sign(
|
const token = jwt.sign(
|
||||||
@ -38,7 +55,6 @@ export class AuthService {
|
|||||||
|
|
||||||
id:user._id,
|
id:user._id,
|
||||||
roleId: user.roleId,
|
roleId: user.roleId,
|
||||||
|
|
||||||
roleName: role.roleName
|
roleName: role.roleName
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -59,10 +75,26 @@ export class AuthService {
|
|||||||
|
|
||||||
roleId: role._id,
|
roleId: role._id,
|
||||||
|
|
||||||
rolename: role.roleName,
|
roleName: role.roleName,
|
||||||
|
|
||||||
active: user.active
|
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,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
){
|
){
|
||||||
|
try{
|
||||||
const fuelTypes = await fuelTypeService.getAll();
|
const fuelTypes = await fuelTypeService.getAll();
|
||||||
|
|
||||||
res.json(fuelTypes);
|
res.json(fuelTypes);
|
||||||
|
|
||||||
|
}catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al obtener tipo de combustible",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Obtener tipo de combustible por ID
|
//Obtener tipo de combustible por ID
|
||||||
@ -23,24 +30,44 @@ export class FuelTypeController {
|
|||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
) {
|
) {
|
||||||
|
try{
|
||||||
const fuelType = await fuelTypeService.getById(
|
const fuelType = await fuelTypeService.getById(
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!fuelType) {
|
||||||
|
return res.status(404).json({
|
||||||
|
message: "Tipo de combustible no encontrado"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
res.json(fuelType);
|
res.json(fuelType);
|
||||||
|
} catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al obtener tipo de combustible",
|
||||||
|
error
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Crear tipo de combustible
|
//Crear tipo de combustible
|
||||||
|
|
||||||
async create(
|
async create(
|
||||||
req:Request,
|
req:Request,
|
||||||
res:Response
|
res:Response
|
||||||
) {
|
) {
|
||||||
|
try{
|
||||||
const fuelType = await fuelTypeService.create(
|
const fuelType = await fuelTypeService.create(
|
||||||
req.body
|
req.body
|
||||||
);
|
);
|
||||||
|
|
||||||
res.status(201).json(fuelType);
|
res.status(201).json(fuelType);
|
||||||
|
|
||||||
|
} catch(error){
|
||||||
|
res.status(500).json({
|
||||||
|
message:"Error al crear tipo de combustible",
|
||||||
|
error
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Actualizar tipo de combustible
|
//Actualizar tipo de combustible
|
||||||
@ -49,12 +76,27 @@ export class FuelTypeController {
|
|||||||
res:Response
|
res:Response
|
||||||
){
|
){
|
||||||
|
|
||||||
const fuelType = fuelTypeService.update(
|
try{
|
||||||
|
const fuelType = await fuelTypeService.update(
|
||||||
req.params.id as string,
|
req.params.id as string,
|
||||||
req.body
|
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
|
//Eliminar tipo de combustible
|
||||||
@ -63,12 +105,25 @@ export class FuelTypeController {
|
|||||||
res:Response
|
res:Response
|
||||||
){
|
){
|
||||||
|
|
||||||
const fuelType = fuelTypeService.delete(
|
try{
|
||||||
|
const fuelType = await fuelTypeService.delete(
|
||||||
req.params.id as string
|
req.params.id as string
|
||||||
)
|
);
|
||||||
|
|
||||||
|
if(!fuelType){
|
||||||
|
return res.status(404).json({
|
||||||
|
message:"Tipo de combustible no encontrado"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
message:"Tipo de combustible eliminado"
|
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 {
|
export interface IFuelType {
|
||||||
|
|
||||||
fuelName:string;
|
fuelName:string;
|
||||||
|
|
||||||
unit:string;
|
unit:string;
|
||||||
|
|
||||||
active:boolean;
|
active:boolean;
|
||||||
}
|
}
|
||||||
@ -8,12 +8,12 @@ const controller = new FuelTypeController();
|
|||||||
|
|
||||||
fuelRoutes.get("/", authMiddleware, controller.getAll);
|
fuelRoutes.get("/", authMiddleware, controller.getAll);
|
||||||
|
|
||||||
fuelRoutes.get("/id", authMiddleware, controller.getById);
|
fuelRoutes.get("/:id", authMiddleware, controller.getById);
|
||||||
|
|
||||||
fuelRoutes.post("/", authMiddleware, controller.create);
|
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;
|
export default fuelRoutes;
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import fuelTypeModel from "./fuelType.model";
|
import fuelTypeModel from "./fuelType.model";
|
||||||
|
import { IFuelType } from "./fuelType.interface";
|
||||||
|
|
||||||
export class FuelTypeService {
|
export class FuelTypeService {
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ export class FuelTypeService {
|
|||||||
//Crear
|
//Crear
|
||||||
|
|
||||||
async create (
|
async create (
|
||||||
data:any
|
data:IFuelType
|
||||||
) {
|
) {
|
||||||
return await fuelTypeModel.create(
|
return await fuelTypeModel.create(
|
||||||
data
|
data
|
||||||
@ -30,8 +31,8 @@ export class FuelTypeService {
|
|||||||
|
|
||||||
//Actualizar
|
//Actualizar
|
||||||
async update(
|
async update(
|
||||||
id:String,
|
id:string,
|
||||||
data:any
|
data:Partial<IFuelType>
|
||||||
) {
|
) {
|
||||||
return await fuelTypeModel.findByIdAndUpdate (
|
return await fuelTypeModel.findByIdAndUpdate (
|
||||||
id,
|
id,
|
||||||
|
|||||||
Reference in New Issue
Block a user