61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql";
|
|
|
|
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
|
import { CompanyListQueryDto } from "../DTO/company-list-query.dto";
|
|
import { Company } from "../entities/company.entity";
|
|
|
|
export class CompanyRepository extends EntityRepository<Company> {
|
|
//
|
|
async getCompaniesListForAdmin(queryDto: CompanyListQueryDto) {
|
|
const { limit, skip } = PaginationUtils(queryDto);
|
|
|
|
const where: FilterQuery<Company> = {
|
|
deletedAt: null,
|
|
};
|
|
|
|
if (queryDto.status) {
|
|
where.status = queryDto.status;
|
|
}
|
|
if (queryDto.isActive !== undefined) {
|
|
where.isActive = queryDto.isActive === 1;
|
|
}
|
|
if (queryDto.q) {
|
|
where.name = { $ilike: `%${queryDto.q}%` };
|
|
}
|
|
if (queryDto.industryId) {
|
|
where.industry = queryDto.industryId;
|
|
}
|
|
if (queryDto.registrationDate) {
|
|
where.createdAt = {
|
|
$gte: queryDto.registrationDate,
|
|
$lte: queryDto.registrationDate,
|
|
};
|
|
}
|
|
if (queryDto.name) {
|
|
where.name = { $ilike: `%${queryDto.name}%` };
|
|
}
|
|
|
|
return this.findAndCount(where, {
|
|
populate: ["industry", "user"],
|
|
limit,
|
|
offset: skip,
|
|
orderBy: { createdAt: "DESC" },
|
|
fields: [
|
|
"id",
|
|
"name",
|
|
"isActive",
|
|
"dateOfEstablishment",
|
|
"status",
|
|
"invoiceCount",
|
|
"createdAt",
|
|
"industry.id",
|
|
"industry.title",
|
|
"user.id",
|
|
"user.firstName",
|
|
"user.lastName",
|
|
"user.fullName",
|
|
],
|
|
});
|
|
}
|
|
}
|