import { FilterQuery } from "@mikro-orm/core"; import { EntityRepository } from "@mikro-orm/postgresql"; import { PaginationUtils } from "../../utils/providers/pagination.utils"; import { BillListQueryDto } from "../dto/bill-list-query.dto"; import { Bill } from "../entities/bill.entity"; export class BillsRepository extends EntityRepository { async getBillsList(queryDto: BillListQueryDto, businessId: string) { const { limit, skip } = PaginationUtils(queryDto); const where: FilterQuery = { business: { id: businessId }, }; if (queryDto.type) { where.type = queryDto.type; } if (queryDto.since || queryDto.to) { where.createdAt = {}; if (queryDto.since) { (where.createdAt as Record).$gte = new Date(queryDto.since); } if (queryDto.to) { (where.createdAt as Record).$lte = new Date(queryDto.to); } } return this.findAndCount(where, { populate: ["invoices"], orderBy: { createdAt: "DESC" }, limit, offset: skip, }); } }