import { SelectQueryBuilder } from "typeorm";
import CommonHelper from "../helpers/common";
import { OrmHelper } from "../helpers/orm";
import { ItemPrice,ItemMaster } from "entity";

export class ItemPriceModel {
    static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
        const repo = OrmHelper.DB.getRepository(ItemMaster);
        let whereAttr: string[] = [];
        let whereVal: any = {};
        if (filter && Object.keys(filter).length > 0) {
            whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter, "item_master").whereAttr];
            whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter, "item_master").whereVal };
        }

        var query = repo.createQueryBuilder("item_master").leftJoinAndSelect("item_master.item_price", "item_price");
        if (whereAttr.length != 0) {
            query = query.where(whereAttr.join(" and "), whereVal);
        }

        return query;
    }
}