update:feature to room done
This commit is contained in:
57
src/model/service_to_depart.ts
Normal file
57
src/model/service_to_depart.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user