fixing doctor item package
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { DoctorItemPackage, ItemMaster, User } from "entity";
|
||||
import { DoctorItemPackage, ItemMaster, ItemPrice, SellingPricePercentage, User } from "entity";
|
||||
import { NextFunction, Response } from "express";
|
||||
import { Request } from "express-jwt";
|
||||
import Joi from "joi";
|
||||
@ -52,27 +52,72 @@ export class DoctorItemPackageController {
|
||||
|
||||
const offset = (param.page - 1) * param.limit;
|
||||
|
||||
const res_count = query;
|
||||
const res_list = query
|
||||
.leftJoinAndSelect("DoctorItemPackage.items", "ItemMaster")
|
||||
.leftJoinAndSelect("ItemMaster.generic_name", "GenericName")
|
||||
.leftJoinAndSelect("ItemMaster.type_detail", "item_type")
|
||||
const current_page = param.page;
|
||||
const total_count_data = await query.getCount();
|
||||
|
||||
// Query 1: packages only (pagination is correct)
|
||||
const packages = await query
|
||||
.select([
|
||||
"DoctorItemPackage.id",
|
||||
"DoctorItemPackage.name",
|
||||
"DoctorItemPackage.description",
|
||||
"DoctorItemPackage.active",
|
||||
])
|
||||
.orderBy("DoctorItemPackage." + orderField, orderDirection)
|
||||
.offset(offset)
|
||||
.limit(param.limit)
|
||||
.getMany();
|
||||
|
||||
// const packages = rows.map((row: any) => ({
|
||||
// id: row.id,
|
||||
// name: row.name,
|
||||
// description: row.description || null,
|
||||
// active: row.active,
|
||||
// items: row.items || [],
|
||||
// }));
|
||||
if (!packages || packages.length === 0) {
|
||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, 0, current_page, total_count_data, []);
|
||||
}
|
||||
|
||||
const current_page = param.page;
|
||||
const total_count_data = await res_count.getCount();
|
||||
const list_data = await res_list.getMany();
|
||||
const count_data = CommonHelper.countObject(list_data);
|
||||
const packageIds = packages.map((p: any) => p.id);
|
||||
|
||||
// Query 2: all items for these packages (build items[] per package)
|
||||
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);
|
||||
} catch (e: unknown) {
|
||||
|
||||
Reference in New Issue
Block a user