This commit is contained in:
Mahyargdz
2025-05-13 09:45:15 +03:30
parent f18295b628
commit 1efc3a2795
23 changed files with 1277 additions and 14 deletions
+68
View File
@@ -0,0 +1,68 @@
import { FilterQuery } from "@mikro-orm/core";
import { EntityRepository } from "@mikro-orm/postgresql";
import { PaginationUtils } from "../../utils/providers/pagination.utils";
import { InvoicesSearchQueryDto } from "../DTO/invoices-search-query.dto";
import { Invoice } from "../entities/invoice.entity";
export class InvoicesRepository extends EntityRepository<Invoice> {
//
async getInvoicesForAdmin(queryDto: InvoicesSearchQueryDto) {
const { limit, skip } = PaginationUtils(queryDto);
const where: FilterQuery<Invoice> = {};
if (queryDto.q) {
where.$or = [
{ company: { user: { firstName: { $ilike: `%${queryDto.q}%` } } } },
{ company: { user: { lastName: { $ilike: `%${queryDto.q}%` } } } },
{ company: { user: { email: { $ilike: `%${queryDto.q}%` } } } },
];
}
if (queryDto.status) {
where.status = queryDto.status;
}
if (queryDto.companyId) {
where.company = queryDto.companyId;
}
if (queryDto.since || queryDto.to) {
const dateConditions = [];
if (queryDto.since) {
const sinceDate = new Date(queryDto.since);
dateConditions.push({ createdAt: { $gte: sinceDate } }, { dueDate: { $gte: sinceDate } }, { paidAt: { $gte: sinceDate } });
}
if (queryDto.to) {
const toDate = new Date(queryDto.to);
dateConditions.push({ createdAt: { $lte: toDate } }, { dueDate: { $lte: toDate } }, { paidAt: { $lte: toDate } });
}
if (queryDto.since && queryDto.to) {
const sinceDate = new Date(queryDto.since);
const toDate = new Date(queryDto.to);
where.$and = [
{
$or: [
{ createdAt: { $gte: sinceDate, $lte: toDate } },
{ dueDate: { $gte: sinceDate, $lte: toDate } },
{ paidAt: { $gte: sinceDate, $lte: toDate } },
],
},
];
} else {
where.$or = dateConditions;
}
}
return this.findAndCount(where, {
populate: ["company", "items"],
orderBy: { createdAt: "DESC" },
limit,
offset: skip,
});
}
}