import { SelectQueryBuilder } from "typeorm";
import CommonHelper from "../helpers/common";
import { OrmHelper } from "../helpers/orm";
import { UserToRoom } from "entity";

export class UserToRoomModel {
    static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
        const repo = OrmHelper.DB.getRepository(UserToRoom);
        let whereAttr: string[] = [];
        let whereVal: any = {};
        if (filter && Object.keys(filter).length > 0) {
            whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter, "user_to_room").whereAttr];
            whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter, "user_to_room").whereVal };
        }

        var query = repo.createQueryBuilder("user_to_room")
            .leftJoinAndSelect("user_to_room.user", "user")
            .leftJoinAndSelect("user_to_room.room", "room");
        if (whereAttr.length != 0) {
            query = query.where(whereAttr.join(" and "), whereVal);
        }

        return query;
    }
}