fixing doctor item package

This commit is contained in:
avicenan
2026-03-17 16:48:52 +07:00
parent a749e62c05
commit 4cc8db4484

View File

@ -1,4 +1,4 @@
import { DoctorItemPackage, ItemMaster, User } from "entity"; import { DoctorItemPackage, ItemMaster, ItemPrice, SellingPricePercentage, User } from "entity";
import { NextFunction, Response } from "express"; import { NextFunction, Response } from "express";
import { Request } from "express-jwt"; import { Request } from "express-jwt";
import Joi from "joi"; import Joi from "joi";
@ -52,27 +52,72 @@ export class DoctorItemPackageController {
const offset = (param.page - 1) * param.limit; const offset = (param.page - 1) * param.limit;
const res_count = query; const current_page = param.page;
const res_list = query const total_count_data = await query.getCount();
.leftJoinAndSelect("DoctorItemPackage.items", "ItemMaster")
.leftJoinAndSelect("ItemMaster.generic_name", "GenericName") // Query 1: packages only (pagination is correct)
.leftJoinAndSelect("ItemMaster.type_detail", "item_type") const packages = await query
.select([
"DoctorItemPackage.id",
"DoctorItemPackage.name",
"DoctorItemPackage.description",
"DoctorItemPackage.active",
])
.orderBy("DoctorItemPackage." + orderField, orderDirection) .orderBy("DoctorItemPackage." + orderField, orderDirection)
.offset(offset) .offset(offset)
.limit(param.limit) .limit(param.limit)
.getMany();
// const packages = rows.map((row: any) => ({ if (!packages || packages.length === 0) {
// id: row.id, return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, 0, current_page, total_count_data, []);
// name: row.name, }
// description: row.description || null,
// active: row.active,
// items: row.items || [],
// }));
const current_page = param.page; const packageIds = packages.map((p: any) => p.id);
const total_count_data = await res_count.getCount();
const list_data = await res_list.getMany(); // Query 2: all items for these packages (build items[] per package)
const count_data = CommonHelper.countObject(list_data); const itemsRaw = await OrmHelper.DB.getRepository(DoctorItemPackage)
.createQueryBuilder("DoctorItemPackage")
.select([
"DoctorItemPackage.id as package_id",
"ItemMaster.id as item_id",
"ItemMaster.item_name as item_name",
"GenericName.generic_name as item_generic_name",
"item_type.type_detail_name as item_type",
"ROUND(CASE WHEN selling_price_percentage.percentage IS NULL " +
"THEN COALESCE(item_price.selling_price, 0) " +
"ELSE COALESCE(item_price.selling_price, 0) + (COALESCE(item_price.selling_price, 0) * COALESCE(selling_price_percentage.percentage, 0) / 100) END, 2) as price",
])
.leftJoin("DoctorItemPackage.items", "ItemMaster")
.leftJoin("ItemMaster.generic_name", "GenericName")
.leftJoin("ItemMaster.type_detail", "item_type")
.leftJoin(ItemPrice, "item_price", "item_price.item_master_id = ItemMaster.id")
.leftJoin(SellingPricePercentage, "selling_price_percentage", "selling_price_percentage.item_master_id = ItemMaster.id")
.where("DoctorItemPackage.id IN (:...ids)", { ids: packageIds })
.getRawMany();
const itemsByPackageId = new Map<string, any[]>();
for (const row of itemsRaw) {
const pkgId = row["package_id"];
if (!pkgId || !row["item_id"]) continue;
if (!itemsByPackageId.has(pkgId)) itemsByPackageId.set(pkgId, []);
itemsByPackageId.get(pkgId)!.push({
item_id: row["item_id"],
item_name: row["item_name"],
item_generic_name: row["item_generic_name"],
item_type: row["item_type"],
price: row["price"],
});
}
const list_data = packages.map((p: any) => ({
id: p.id,
name: p.name,
description: p.description ?? null,
active: p.active,
items: itemsByPackageId.get(p.id) ?? [],
}));
const count_data = list_data.length;
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data); return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
} catch (e: unknown) { } catch (e: unknown) {