update:feature to room done

This commit is contained in:
wayanrivan
2026-02-16 12:08:19 +07:00
parent e7f4deca7c
commit f42e533a75
4 changed files with 218 additions and 2 deletions

View File

@ -0,0 +1,57 @@
// ServiceToDepartModel.ts
import { Service } from "entity";
import { SelectQueryBuilder } from "typeorm";
import CommonHelper from "../helpers/common";
import { OrmHelper } from "../helpers/orm";
export class ServiceToDepartModel {
static async list(filter = {}, withDeleted = false): Promise<SelectQueryBuilder<Service>> {
const repo = OrmHelper.DB.getRepository(Service);
let whereAttr: string[] = [];
let whereVal: any = {};
if (filter && Object.keys(filter).length > 0) {
const filterResult = CommonHelper.handleQueryFilter(filter);
whereAttr = [...whereAttr, ...filterResult.whereAttr];
whereVal = { ...whereVal, ...filterResult.whereVal };
}
let query = repo.createQueryBuilder("Service")
.leftJoinAndSelect("Service.rooms", "Room")
.leftJoinAndSelect("Room.department", "Department");
if (whereAttr.length != 0) {
query = query.where(whereAttr.join(" and "), whereVal);
}
if (withDeleted) {
query = query.withDeleted();
}
return query;
}
// Method baru untuk count rooms (bukan relationships)
static async countRooms(filter = {}, withDeleted = false): Promise<number> {
const repo = OrmHelper.DB.getRepository(Service);
// Query untuk menghitung total unique rooms yang punya services
let queryBuilder = repo.createQueryBuilder("Service")
.innerJoin("Service.rooms", "Room")
.select("COUNT(DISTINCT Room.id)", "count");
if (filter && Object.keys(filter).length > 0) {
const filterResult = CommonHelper.handleQueryFilter(filter);
if (filterResult.whereAttr.length > 0) {
queryBuilder = queryBuilder.where(filterResult.whereAttr.join(" and "), filterResult.whereVal);
}
}
if (withDeleted) {
queryBuilder = queryBuilder.withDeleted();
}
const result = await queryBuilder.getRawOne();
return parseInt(result.count) || 0;
}
}