diff --git a/backend/src/modules/lpg/lpg.controller.ts b/backend/src/modules/lpg/lpg.controller.ts index ee3f00e..7407fd6 100644 --- a/backend/src/modules/lpg/lpg.controller.ts +++ b/backend/src/modules/lpg/lpg.controller.ts @@ -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 + }); } + } } \ No newline at end of file diff --git a/backend/src/modules/lpg/lpg.interface.ts b/backend/src/modules/lpg/lpg.interface.ts index 94d5849..574d80a 100644 --- a/backend/src/modules/lpg/lpg.interface.ts +++ b/backend/src/modules/lpg/lpg.interface.ts @@ -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"; } diff --git a/backend/src/modules/lpg/lpg.model.ts b/backend/src/modules/lpg/lpg.model.ts index 1d9b9b1..576a712 100644 --- a/backend/src/modules/lpg/lpg.model.ts +++ b/backend/src/modules/lpg/lpg.model.ts @@ -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, }, diff --git a/backend/src/modules/lpg/lpg.routes.ts b/backend/src/modules/lpg/lpg.routes.ts index ab9726b..c442083 100644 --- a/backend/src/modules/lpg/lpg.routes.ts +++ b/backend/src/modules/lpg/lpg.routes.ts @@ -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); diff --git a/backend/src/modules/lpg/lpg.service.ts b/backend/src/modules/lpg/lpg.service.ts index 6e1af77..1a918a0 100644 --- a/backend/src/modules/lpg/lpg.service.ts +++ b/backend/src/modules/lpg/lpg.service.ts @@ -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 ){ - 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, diff --git a/backend/src/utils/requestNumber.ts b/backend/src/utils/requestNumber.ts index 2cf8baa..33ba4c3 100644 --- a/backend/src/utils/requestNumber.ts +++ b/backend/src/utils/requestNumber.ts @@ -1,3 +1,7 @@ export const generateRequestNumber = ():string =>{ return `REQ-${Date.now()}`; +} + +export const generateRequestLPGNumber =():string =>{ + return `LPG-${Date.now()}`; } \ No newline at end of file