fix pagination

This commit is contained in:
hannathkadher
2025-01-08 12:16:20 +04:00
parent 0ba681d93b
commit cdba0f6554
2 changed files with 54 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import { FindManyOptions, Repository } from 'typeorm'; import { FindManyOptions, Repository, SelectQueryBuilder } from 'typeorm';
import { InternalServerErrorException } from '@nestjs/common'; import { InternalServerErrorException } from '@nestjs/common';
import { BaseResponseDto } from '../dto/base.response.dto'; import { BaseResponseDto } from '../dto/base.response.dto';
import { PageResponseDto } from '../dto/pagination.response.dto'; import { PageResponseDto } from '../dto/pagination.response.dto';
@ -70,8 +70,8 @@ export function TypeORMCustomModel(repository: Repository<any>) {
return Object.assign(repository, { return Object.assign(repository, {
findAll: async function ( findAll: async function (
query: Partial<TypeORMCustomModelFindAllQuery>, query: Partial<TypeORMCustomModelFindAllQuery>,
customQueryBuilder?: SelectQueryBuilder<any>,
): Promise<TypeORMCustomModelFindAllResponse> { ): Promise<TypeORMCustomModelFindAllResponse> {
// Extract values from the query
const { const {
page = 1, page = 1,
size = 10, size = 10,
@ -82,12 +82,7 @@ export function TypeORMCustomModel(repository: Repository<any>) {
select, select,
} = getDefaultQueryOptions(query); } = getDefaultQueryOptions(query);
// Ensure modelName is set before proceeding
if (!modelName) { if (!modelName) {
console.error(
'modelName is missing after getDefaultQueryOptions:',
query,
);
throw new InternalServerErrorException( throw new InternalServerErrorException(
`[TypeORMCustomModel] Cannot findAll with unknown modelName`, `[TypeORMCustomModel] Cannot findAll with unknown modelName`,
); );
@ -96,12 +91,32 @@ export function TypeORMCustomModel(repository: Repository<any>) {
const skip = (page - 1) * size; const skip = (page - 1) * size;
const order = buildTypeORMSortQuery(sort); const order = buildTypeORMSortQuery(sort);
const relations = buildTypeORMIncludeQuery(modelName, include); const relations = buildTypeORMIncludeQuery(modelName, include);
// Use the where clause directly, without wrapping it under 'where'
const whereClause = buildTypeORMWhereClause({ where }); const whereClause = buildTypeORMWhereClause({ where });
// Ensure the whereClause is passed directly to findAndCount let data: any[] = [];
const [data, count] = await repository.findAndCount({ let count = 0;
if (customQueryBuilder) {
const qb = customQueryBuilder.skip(skip).take(size);
if (order) {
Object.keys(order).forEach((key) => {
qb.addOrderBy(key, order[key]);
});
}
if (whereClause) {
qb.where(whereClause);
}
if (select) {
const selectColumns = Array.isArray(select)
? select
: Object.keys(select).map(
(key) => `${customQueryBuilder.alias}.${key}`,
);
qb.select(selectColumns as string[]);
}
[data, count] = await qb.getManyAndCount();
} else {
[data, count] = await repository.findAndCount({
where: whereClause, where: whereClause,
take: size, take: size,
skip: skip, skip: skip,
@ -109,6 +124,7 @@ export function TypeORMCustomModel(repository: Repository<any>) {
select: select, select: select,
relations: relations, relations: relations,
}); });
}
const paginationResponseDto = getPaginationResponseDto(count, page, size); const paginationResponseDto = getPaginationResponseDto(count, page, size);
const baseResponseDto: BaseResponseDto = { const baseResponseDto: BaseResponseDto = {

View File

@ -13,7 +13,6 @@ import {
TypeORMCustomModelFindAllQuery, TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model'; } from '@app/common/models/typeOrmCustom.model';
import { PageResponse } from '@app/common/dto/pagination.response.dto'; import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
import { SpaceModelParam } from '../dtos/space-model-param'; import { SpaceModelParam } from '../dtos/space-model-param';
import { ProjectService } from 'src/project/services'; import { ProjectService } from 'src/project/services';
import { ProjectEntity } from '@app/common/modules/project/entities'; import { ProjectEntity } from '@app/common/modules/project/entities';
@ -25,6 +24,7 @@ import {
ModifiedTagsModelPayload, ModifiedTagsModelPayload,
ModifySubspaceModelPayload, ModifySubspaceModelPayload,
} from '../interfaces'; } from '../interfaces';
import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
@Injectable() @Injectable()
export class SpaceModelService { export class SpaceModelService {
@ -117,20 +117,30 @@ export class SpaceModelService {
pageable.where = { pageable.where = {
project: { uuid: param.projectUuid }, project: { uuid: param.projectUuid },
disabled: false, disabled: false,
subspaceModels: {
disabled: false,
},
tags: {
disabled: false,
},
}; };
pageable.include = pageable.include =
'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product'; 'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product';
const customModel = TypeORMCustomModel(this.spaceModelRepository); const queryBuilder = this.spaceModelRepository
.createQueryBuilder('spaceModel')
.leftJoinAndSelect('spaceModel.subspaceModels', 'subspaceModels')
.leftJoinAndSelect('spaceModel.tags', 'tags')
.leftJoinAndSelect('subspaceModels.tags', 'subspaceModelTags')
.where('spaceModel.disabled = :disabled', { disabled: false })
.andWhere('spaceModel.project = :projectUuid', {
projectUuid: param.projectUuid,
});
const customModel = TypeORMCustomModel(this.spaceModelRepository);
const { baseResponseDto, paginationResponseDto } = const { baseResponseDto, paginationResponseDto } =
await customModel.findAll(pageable); await customModel.findAll(
{
page: pageable.page || 1,
size: pageable.size || 10,
modelName: 'spaceModel',
},
queryBuilder,
);
return new PageResponse<SpaceModelDto>( return new PageResponse<SpaceModelDto>(
baseResponseDto, baseResponseDto,
@ -138,8 +148,7 @@ export class SpaceModelService {
); );
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || `Error fetching paginated list: ${error.message}`,
'An error occurred while fetching space models in project.',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }