Actualización CRUD módulo LPG, adición de validación y adición de la función para generar número de requisición
This commit is contained in:
@ -45,7 +45,7 @@ export class LPGController{
|
||||
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
message:"Error al obtener requisicion",
|
||||
message:"Error al obtener requisición",
|
||||
error
|
||||
});
|
||||
}
|
||||
@ -105,33 +105,71 @@ export class LPGController{
|
||||
}
|
||||
}
|
||||
|
||||
//Eliminar requisición
|
||||
async getByStatus(
|
||||
req:Request,
|
||||
res:Response,
|
||||
){
|
||||
|
||||
try{
|
||||
const request = await service.getByStatus(
|
||||
req.params.status as string
|
||||
);
|
||||
|
||||
res.json(request);
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al obtener requisición "
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getByPlant(
|
||||
req:Request,
|
||||
res:Response,
|
||||
){
|
||||
|
||||
try{
|
||||
const request = await service.getByPlant(
|
||||
req.params.plant as string
|
||||
);
|
||||
|
||||
res.json(request);
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al obtener requisición "
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//Eliminar requisición
|
||||
|
||||
async delete(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const request =
|
||||
await service.delete(
|
||||
req.params.id as string
|
||||
);
|
||||
|
||||
if(!request){
|
||||
return res.status(404).json({
|
||||
message:
|
||||
"Requisición no encontrada"
|
||||
});
|
||||
}
|
||||
res.json({
|
||||
async delete(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const request =
|
||||
await service.delete(
|
||||
req.params.id as string
|
||||
);
|
||||
|
||||
if(!request){
|
||||
return res.status(404).json({
|
||||
message:"Requisición no encontrada"
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
res.json({
|
||||
message:"Requisición eliminada correctamente"
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
res.status(500).json({
|
||||
message: "Error al eliminar requisición",
|
||||
error
|
||||
});
|
||||
}
|
||||
|
||||
}catch(error) {
|
||||
res.status(500).json({
|
||||
message: "Error al eliminar requisición",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,16 +1,29 @@
|
||||
import { ObjectId } from "mongoose";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export interface ILpg{
|
||||
|
||||
requestNumber:string;
|
||||
|
||||
plant:string,
|
||||
|
||||
equipment:string;
|
||||
requestedBy:ObjectId;
|
||||
approvedBy?:ObjectId;
|
||||
|
||||
requestedBy:Types.ObjectId;
|
||||
|
||||
approvedBy?:Types.ObjectId;
|
||||
|
||||
requestDate:Date;
|
||||
gallons:number;
|
||||
|
||||
gallonQuantity:number;
|
||||
|
||||
pricePerGallon:number;
|
||||
|
||||
totalAmount:number,
|
||||
|
||||
comments?:string;
|
||||
status:string;
|
||||
|
||||
status:
|
||||
| "PENDIENTE"
|
||||
| "APROBADO"
|
||||
| "RECHAZADO";
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ const lpgSchema = new mongoose.Schema({
|
||||
|
||||
plant:{
|
||||
type:String,
|
||||
requiered:true,
|
||||
required:true,
|
||||
enum:[
|
||||
"Planta 1",
|
||||
"Planta 2"
|
||||
@ -25,7 +25,7 @@ const lpgSchema = new mongoose.Schema({
|
||||
requestedBy:{
|
||||
type:mongoose.Schema.Types.ObjectId,
|
||||
ref:"User",
|
||||
requeride:true
|
||||
required:true
|
||||
},
|
||||
|
||||
approvedBy:{
|
||||
@ -36,10 +36,10 @@ const lpgSchema = new mongoose.Schema({
|
||||
requestDate:{
|
||||
type:Date,
|
||||
default:Date.now,
|
||||
requiered:true
|
||||
required:true
|
||||
},
|
||||
|
||||
gallons:{
|
||||
gallonQuantity:{
|
||||
type:Number,
|
||||
required:true,
|
||||
},
|
||||
|
||||
@ -10,14 +10,18 @@ const controller = new LPGController();
|
||||
//Obtener todas las requisiciones
|
||||
lpgRouter.get("/", authMiddleware, controller.getAll);
|
||||
|
||||
//Obtener requisición por status
|
||||
lpgRouter.get("/status/:status", authMiddleware, controller.getByStatus);
|
||||
|
||||
//Obtener requisición por planta
|
||||
lpgRouter.get("/plant/:plant", authMiddleware, controller.getByPlant);
|
||||
|
||||
//Obtener requisición por ID
|
||||
lpgRouter.get("/:id", authMiddleware, controller.getById);
|
||||
|
||||
|
||||
//Crear requisición
|
||||
lpgRouter.post( "/", authMiddleware, controller.create);
|
||||
|
||||
|
||||
//Actualizar requisición
|
||||
lpgRouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
|
||||
@ -1,29 +1,16 @@
|
||||
import lpgModel from "./lpg.model";
|
||||
import { ILpg } from "./lpg.interface";
|
||||
import { generateRequestLPGNumber } from "../../utils/requestNumber";
|
||||
|
||||
export class LPGService{
|
||||
|
||||
|
||||
//Crear solicitud gas LPG
|
||||
async create (
|
||||
data:ILpg
|
||||
){
|
||||
|
||||
data.totalAmount = data.gallons *
|
||||
data.pricePerGallon;
|
||||
|
||||
const lpg = new lpgModel(data);
|
||||
|
||||
return await lpg.save();
|
||||
}
|
||||
|
||||
//Obtener todas las requisiciones
|
||||
async getAll() {
|
||||
|
||||
return await lpgModel
|
||||
.find()
|
||||
.populate("requestedBy")
|
||||
.populate("approveBy")
|
||||
.populate("approvedBy")
|
||||
}
|
||||
|
||||
//Obtener requisición por Id
|
||||
@ -33,21 +20,49 @@ export class LPGService{
|
||||
return await lpgModel
|
||||
.findById(id)
|
||||
.populate("requestedBy")
|
||||
.populate("approveBy")
|
||||
.populate("approvedBy")
|
||||
}
|
||||
|
||||
|
||||
//Crear solicitud gas LPG
|
||||
async create (
|
||||
data:ILpg
|
||||
){
|
||||
//Validar que los galones sean mayor a 0
|
||||
if(data.gallonQuantity <= 0){
|
||||
throw new Error ("La cantidad de galones debe ser mayor que 0");
|
||||
}
|
||||
|
||||
//Generar número automático
|
||||
data.requestNumber = generateRequestLPGNumber();
|
||||
|
||||
data.totalAmount = data.gallonQuantity * data.pricePerGallon;
|
||||
|
||||
//Estado inicial
|
||||
data.status = "PENDIENTE";
|
||||
|
||||
const lpg = new lpgModel(data);
|
||||
|
||||
return await lpg.save();
|
||||
}
|
||||
|
||||
async update(
|
||||
id:string,
|
||||
data:Partial<ILpg>
|
||||
){
|
||||
if (data.gallons &&
|
||||
data.pricePerGallon
|
||||
){
|
||||
data.totalAmount =
|
||||
data.gallons *
|
||||
data.pricePerGallon
|
||||
//Buscar la requisición actual
|
||||
const lpgRequest = await lpgModel.findById(id);
|
||||
|
||||
if(!lpgRequest){
|
||||
return null;
|
||||
}
|
||||
|
||||
const gallonQuantity = data.gallonQuantity ?? lpgRequest.gallonQuantity;
|
||||
|
||||
const pricePerGallon = data.pricePerGallon ?? lpgRequest.pricePerGallon;
|
||||
|
||||
data.totalAmount = gallonQuantity * pricePerGallon
|
||||
|
||||
return await lpgModel
|
||||
.findByIdAndUpdate(
|
||||
id,
|
||||
|
||||
Reference in New Issue
Block a user