chore: company and industy related logix
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { EntityRepository } from "@mikro-orm/postgresql";
|
||||
import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql";
|
||||
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { CompanyListQueryDto } from "../DTO/company-list-query.dto";
|
||||
@@ -9,57 +9,52 @@ export class CompanyRepository extends EntityRepository<Company> {
|
||||
async getCompaniesListForAdmin(queryDto: CompanyListQueryDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const qb = this.createQueryBuilder("company")
|
||||
.select([
|
||||
"company.id",
|
||||
"company.name",
|
||||
"company.status",
|
||||
"company.isActive",
|
||||
"company.createdAt",
|
||||
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",
|
||||
"(SELECT COUNT(*) FROM invoice WHERE invoice.company_id = company.id) as invoiceCount",
|
||||
])
|
||||
.leftJoin("company.industry", "industry")
|
||||
.leftJoin("company.user", "user")
|
||||
.where({
|
||||
deletedAt: null,
|
||||
...(queryDto.status && { status: queryDto.status }),
|
||||
...(queryDto.isActive !== undefined && { isActive: queryDto.isActive === 1 }),
|
||||
});
|
||||
|
||||
if (queryDto.q) {
|
||||
qb.andWhere({ name: { $ilike: `%${queryDto.q}%` } });
|
||||
}
|
||||
|
||||
if (queryDto.industryId) {
|
||||
qb.andWhere({ industry: { id: queryDto.industryId } });
|
||||
}
|
||||
|
||||
if (queryDto.registrationDate) {
|
||||
qb.andWhere({
|
||||
createdAt: {
|
||||
$gte: queryDto.registrationDate,
|
||||
$lte: queryDto.registrationDate,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (queryDto.name) {
|
||||
qb.andWhere({ name: { $ilike: `%${queryDto.name}%` } });
|
||||
}
|
||||
|
||||
qb.orderBy({ createdAt: "DESC" }).limit(limit).offset(skip);
|
||||
|
||||
return qb.getResultAndCount();
|
||||
|
||||
// // Transform the result to include invoiceCount as a number
|
||||
// const transformedCompanies = companies.map((company) => ({
|
||||
// ...company,
|
||||
// invoiceCount: parseInt(company.invoiceCount, 10),
|
||||
// }));
|
||||
"user.fullName",
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user