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:
2026-07-03 17:49:38 +00:00
parent 4c34f20705
commit 61cfeb7a6c
6 changed files with 133 additions and 59 deletions

View File

@ -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,