import { ReferenceRange } from "entity";
import { SelectQueryBuilder } from "typeorm";
import { OrmHelper } from "../helpers/orm";

export class ReferenceRangeModel {
	static async list(filter: Record<string, any> = {}): Promise<SelectQueryBuilder<any>> {
		const repo = OrmHelper.DB.getRepository(ReferenceRange);

		const query = repo.createQueryBuilder("ReferenceRange");

		if (filter && Object.keys(filter).length > 0) {
			Object.entries(filter).forEach(([key, value]) => {
				if (value !== undefined && value !== null && value !== "") {

					// 🔥 Handle kalau ada yang kirim "ReferenceRange.id"
					const field = key.includes(".") ? key.split(".")[1] : key;

					query.andWhere(`ReferenceRange.${field} = :${field}`, {
						[field]: value,
					});
				}
			});
		}

		return query;
	}
}