101 lines
2.1 KiB
TypeScript
101 lines
2.1 KiB
TypeScript
import lpgModel from "./lpg.model";
|
|
import { ILpg } from "./lpg.interface";
|
|
import { generateRequestLPGNumber } from "../../utils/requestNumber";
|
|
|
|
export class LPGService{
|
|
|
|
//Obtener todas las requisiciones
|
|
async getAll() {
|
|
|
|
return await lpgModel
|
|
.find()
|
|
.populate("requestedBy")
|
|
.populate("approvedBy")
|
|
}
|
|
|
|
//Obtener requisición por Id
|
|
async getById(
|
|
id:string
|
|
){
|
|
return await lpgModel
|
|
.findById(id)
|
|
.populate("requestedBy")
|
|
.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>
|
|
){
|
|
//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,
|
|
data,
|
|
{
|
|
new:true
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
async getByStatus(
|
|
status:string
|
|
){
|
|
return await lpgModel
|
|
.find({
|
|
status
|
|
} as any)
|
|
}
|
|
|
|
async getByPlant(
|
|
plant:string
|
|
){
|
|
return await lpgModel
|
|
.find({
|
|
plant
|
|
}as any)
|
|
}
|
|
|
|
async delete(
|
|
id:string
|
|
){
|
|
return await lpgModel
|
|
.findByIdAndDelete(id);
|
|
}
|
|
} |