From 8ec4a33d9492a7e7f8c9f9ce5817f23b7ec4ff8d Mon Sep 17 00:00:00 2001 From: Mohamed Marrouchi Date: Fri, 22 Nov 2024 16:05:30 +0100 Subject: [PATCH] fix: pagination --- .../chat/controllers/block.controller.spec.ts | 8 +++---- api/src/chat/controllers/block.controller.ts | 14 ++++++++--- .../chat/controllers/category.controller.ts | 6 ++++- .../controllers/context-var.controller.ts | 6 ++++- api/src/chat/controllers/label.controller.ts | 10 ++++++-- .../chat/controllers/message.controller.ts | 13 +++++++--- .../chat/controllers/subscriber.controller.ts | 10 ++++++-- .../controllers/content-type.controller.ts | 6 ++++- api/src/cms/controllers/content.controller.ts | 9 +++++-- api/src/cms/controllers/menu.controller.ts | 6 ++++- .../i18n/controllers/language.controller.ts | 6 ++++- .../controllers/translation.controller.ts | 5 +++- .../nlp/controllers/nlp-entity.controller.ts | 10 ++++++-- .../nlp/controllers/nlp-sample.controller.ts | 10 ++++++-- .../nlp/controllers/nlp-value.controller.ts | 10 ++++++-- api/src/user/controllers/role.controller.ts | 10 ++++++-- api/src/user/controllers/user.controller.ts | 10 ++++++-- .../utils/pagination/pagination-query.dto.ts | 4 ++-- .../utils/pagination/pagination-query.pipe.ts | 24 ++++++++++--------- 19 files changed, 132 insertions(+), 45 deletions(-) diff --git a/api/src/chat/controllers/block.controller.spec.ts b/api/src/chat/controllers/block.controller.spec.ts index e56d186..403744b 100644 --- a/api/src/chat/controllers/block.controller.spec.ts +++ b/api/src/chat/controllers/block.controller.spec.ts @@ -48,13 +48,13 @@ import { BlockCreateDto, BlockUpdateDto } from '../dto/block.dto'; import { BlockRepository } from '../repositories/block.repository'; import { CategoryRepository } from '../repositories/category.repository'; import { LabelRepository } from '../repositories/label.repository'; -import { BlockModel, Block } from '../schemas/block.schema'; +import { Block, BlockModel } from '../schemas/block.schema'; import { LabelModel } from '../schemas/label.schema'; import { BlockService } from '../services/block.service'; import { CategoryService } from '../services/category.service'; import { LabelService } from '../services/label.service'; -import { CategoryModel, Category } from './../schemas/category.schema'; +import { Category, CategoryModel } from './../schemas/category.schema'; import { BlockController } from './block.controller'; describe('BlockController', () => { @@ -167,7 +167,7 @@ describe('BlockController', () => { blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks.id] : [], })); - expect(blockService.find).toHaveBeenCalledWith({}); + expect(blockService.find).toHaveBeenCalledWith({}, undefined); expect(result).toEqualPayload(blocksWithCategory, [ ...IGNORED_TEST_FIELDS, 'attachedToBlock', @@ -187,7 +187,7 @@ describe('BlockController', () => { blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks] : [], })); - expect(blockService.findAndPopulate).toHaveBeenCalledWith({}); + expect(blockService.findAndPopulate).toHaveBeenCalledWith({}, undefined); expect(result).toEqualPayload(blocksWithCategory); }); }); diff --git a/api/src/chat/controllers/block.controller.ts b/api/src/chat/controllers/block.controller.ts index 47ef62f..fd59e8a 100644 --- a/api/src/chat/controllers/block.controller.ts +++ b/api/src/chat/controllers/block.controller.ts @@ -30,6 +30,8 @@ import { PluginName, PluginType } from '@/plugins/types'; import { UserService } from '@/user/services/user.service'; import { BaseController } from '@/utils/generics/base-controller'; import { DeleteResult } from '@/utils/generics/base-repository'; +import { PageQueryDto } from '@/utils/pagination/pagination-query.dto'; +import { PageQueryPipe } from '@/utils/pagination/pagination-query.pipe'; import { PopulatePipe } from '@/utils/pipes/populate.pipe'; import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe'; import { TFilterQuery } from '@/utils/types/filter.types'; @@ -63,23 +65,29 @@ export class BlockController extends BaseController< ) { super(blockService); } + /** * Finds blocks based on the provided query parameters. * @param populate - An array of fields to populate in the returned blocks. * @param filters - Query filters to apply to the block search. * @returns A Promise that resolves to an array of found blocks. */ - @Get() async find( @Query(PopulatePipe) populate: string[], @Query(new SearchFilterPipe({ allowedFields: ['category'] })) filters: TFilterQuery, + @Query(PageQueryPipe) pageQuery?: PageQueryDto, ): Promise { + if (pageQuery?.limit) { + return this.canPopulate(populate) + ? await this.blockService.findPageAndPopulate(filters, pageQuery) + : await this.blockService.findPage(filters, pageQuery); + } return this.canPopulate(populate) - ? await this.blockService.findAndPopulate(filters) - : await this.blockService.find(filters); + ? await this.blockService.findAndPopulate(filters, pageQuery?.sort) + : await this.blockService.find(filters, pageQuery?.sort); } /** diff --git a/api/src/chat/controllers/category.controller.ts b/api/src/chat/controllers/category.controller.ts index 4d90fc3..ff4c322 100644 --- a/api/src/chat/controllers/category.controller.ts +++ b/api/src/chat/controllers/category.controller.ts @@ -57,7 +57,11 @@ export class CategoryController extends BaseController { @Query(new SearchFilterPipe({ allowedFields: ['label'] })) filters: TFilterQuery, ) { - return await this.categoryService.findPage(filters, pageQuery); + if (pageQuery.limit) { + return await this.categoryService.findPage(filters, pageQuery); + } + + return await this.categoryService.find(filters, pageQuery.sort); } /** diff --git a/api/src/chat/controllers/context-var.controller.ts b/api/src/chat/controllers/context-var.controller.ts index 53db073..f03358e 100644 --- a/api/src/chat/controllers/context-var.controller.ts +++ b/api/src/chat/controllers/context-var.controller.ts @@ -60,7 +60,11 @@ export class ContextVarController extends BaseController { @Query(new SearchFilterPipe({ allowedFields: ['label'] })) filters: TFilterQuery, ): Promise { - return await this.contextVarService.findPage(filters, pageQuery); + if (pageQuery.limit) { + return await this.contextVarService.findPage(filters, pageQuery); + } + + return await this.contextVarService.find(filters, pageQuery.sort); } /** diff --git a/api/src/chat/controllers/label.controller.ts b/api/src/chat/controllers/label.controller.ts index b5b76fa..2179e35 100644 --- a/api/src/chat/controllers/label.controller.ts +++ b/api/src/chat/controllers/label.controller.ts @@ -62,9 +62,15 @@ export class LabelController extends BaseController< @Query(new SearchFilterPipe