import lpgModel from "./lpg.model";
import { ILpg } from "./lpg.interface";

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")
    }

    //Obtener requisición por Id
    async getById(
        id:string
    ){
        return await lpgModel
        .findById(id)
        .populate("requestedBy")
        .populate("approveBy")
    }

    async update(
        id:string,
        data:Partial<ILpg>
    ){
        if (data.gallons &&
            data.pricePerGallon
        ){
            data.totalAmount =
            data.gallons * 
            data.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);
    }
}