38 lines
1.1 KiB
TypeScript
Executable File
38 lines
1.1 KiB
TypeScript
Executable File
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<Bill> {
|
|
async getBillsList(queryDto: BillListQueryDto, businessId: string) {
|
|
const { limit, skip } = PaginationUtils(queryDto);
|
|
|
|
const where: FilterQuery<Bill> = {
|
|
business: { id: businessId },
|
|
};
|
|
|
|
if (queryDto.type) {
|
|
where.type = queryDto.type;
|
|
}
|
|
|
|
if (queryDto.since || queryDto.to) {
|
|
where.createdAt = {};
|
|
if (queryDto.since) {
|
|
(where.createdAt as Record<string, unknown>).$gte = new Date(queryDto.since);
|
|
}
|
|
if (queryDto.to) {
|
|
(where.createdAt as Record<string, unknown>).$lte = new Date(queryDto.to);
|
|
}
|
|
}
|
|
|
|
return this.findAndCount(where, {
|
|
populate: ["invoices"],
|
|
orderBy: { createdAt: "DESC" },
|
|
limit,
|
|
offset: skip,
|
|
});
|
|
}
|
|
}
|