fix: pagination

This commit is contained in:
Mohamed Marrouchi
2024-11-22 16:05:30 +01:00
parent 2d4b00b9c0
commit 8ec4a33d94
19 changed files with 132 additions and 45 deletions

View File

@@ -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);
});
});

View File

@@ -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<Block>({ allowedFields: ['category'] }))
filters: TFilterQuery<Block>,
@Query(PageQueryPipe) pageQuery?: PageQueryDto<Block>,
): Promise<Block[] | BlockFull[]> {
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);
}
/**

View File

@@ -57,7 +57,11 @@ export class CategoryController extends BaseController<Category> {
@Query(new SearchFilterPipe<Category>({ allowedFields: ['label'] }))
filters: TFilterQuery<Category>,
) {
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);
}
/**

View File

@@ -60,7 +60,11 @@ export class ContextVarController extends BaseController<ContextVar> {
@Query(new SearchFilterPipe<ContextVar>({ allowedFields: ['label'] }))
filters: TFilterQuery<ContextVar>,
): Promise<ContextVar[]> {
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);
}
/**

View File

@@ -62,9 +62,15 @@ export class LabelController extends BaseController<
@Query(new SearchFilterPipe<Label>({ allowedFields: ['name', 'title'] }))
filters: TFilterQuery<Label>,
) {
if (pageQuery.limit) {
return this.canPopulate(populate)
? await this.labelService.findPageAndPopulate(filters, pageQuery)
: await this.labelService.findPage(filters, pageQuery);
}
return this.canPopulate(populate)
? await this.labelService.findPageAndPopulate(filters, pageQuery)
: await this.labelService.findPage(filters, pageQuery);
? await this.labelService.findAndPopulate(filters, pageQuery.sort)
: await this.labelService.find(filters, pageQuery.sort);
}
/**

View File

@@ -42,6 +42,7 @@ import {
MessageStub,
} from '../schemas/message.schema';
import {
AnyMessage,
OutgoingMessage,
OutgoingMessageFormat,
StdOutgoingEnvelope,
@@ -71,7 +72,7 @@ export class MessageController extends BaseController<
@Get()
async findPage(
@Query(PageQueryPipe) pageQuery: PageQueryDto<Message>,
@Query(PageQueryPipe) pageQuery: PageQueryDto<AnyMessage>,
@Query(PopulatePipe)
populate: string[],
@Query(
@@ -79,9 +80,15 @@ export class MessageController extends BaseController<
)
filters: TFilterQuery<Message>,
) {
if (pageQuery.limit) {
return this.canPopulate(populate)
? await this.messageService.findPageAndPopulate(filters, pageQuery)
: await this.messageService.findPage(filters, pageQuery);
}
return this.canPopulate(populate)
? await this.messageService.findPageAndPopulate(filters, pageQuery)
: await this.messageService.findPage(filters, pageQuery);
? await this.messageService.findAndPopulate(filters, pageQuery.sort)
: await this.messageService.find(filters, pageQuery.sort);
}
/**

View File

@@ -73,9 +73,15 @@ export class SubscriberController extends BaseController<
)
filters: TFilterQuery<Subscriber>,
) {
if (pageQuery.limit) {
return this.canPopulate(populate)
? await this.subscriberService.findPageAndPopulate(filters, pageQuery)
: await this.subscriberService.findPage(filters, pageQuery);
}
return this.canPopulate(populate)
? await this.subscriberService.findPageAndPopulate(filters, pageQuery)
: await this.subscriberService.findPage(filters, pageQuery);
? await this.subscriberService.findAndPopulate(filters, pageQuery.sort)
: await this.subscriberService.find(filters, pageQuery.sort);
}
/**