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