mirror of
https://github.com/hexastack/hexabot
synced 2024-11-24 04:53:41 +00:00
commit
cf154bfe32
@ -48,13 +48,13 @@ import { BlockCreateDto, BlockUpdateDto } from '../dto/block.dto';
|
|||||||
import { BlockRepository } from '../repositories/block.repository';
|
import { BlockRepository } from '../repositories/block.repository';
|
||||||
import { CategoryRepository } from '../repositories/category.repository';
|
import { CategoryRepository } from '../repositories/category.repository';
|
||||||
import { LabelRepository } from '../repositories/label.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 { LabelModel } from '../schemas/label.schema';
|
||||||
import { BlockService } from '../services/block.service';
|
import { BlockService } from '../services/block.service';
|
||||||
import { CategoryService } from '../services/category.service';
|
import { CategoryService } from '../services/category.service';
|
||||||
import { LabelService } from '../services/label.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';
|
import { BlockController } from './block.controller';
|
||||||
|
|
||||||
describe('BlockController', () => {
|
describe('BlockController', () => {
|
||||||
@ -167,7 +167,7 @@ describe('BlockController', () => {
|
|||||||
blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks.id] : [],
|
blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks.id] : [],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
expect(blockService.find).toHaveBeenCalledWith({});
|
expect(blockService.find).toHaveBeenCalledWith({}, undefined);
|
||||||
expect(result).toEqualPayload(blocksWithCategory, [
|
expect(result).toEqualPayload(blocksWithCategory, [
|
||||||
...IGNORED_TEST_FIELDS,
|
...IGNORED_TEST_FIELDS,
|
||||||
'attachedToBlock',
|
'attachedToBlock',
|
||||||
@ -187,7 +187,7 @@ describe('BlockController', () => {
|
|||||||
blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks] : [],
|
blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks] : [],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
expect(blockService.findAndPopulate).toHaveBeenCalledWith({});
|
expect(blockService.findAndPopulate).toHaveBeenCalledWith({}, undefined);
|
||||||
expect(result).toEqualPayload(blocksWithCategory);
|
expect(result).toEqualPayload(blocksWithCategory);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -30,6 +30,8 @@ import { PluginName, PluginType } from '@/plugins/types';
|
|||||||
import { UserService } from '@/user/services/user.service';
|
import { UserService } from '@/user/services/user.service';
|
||||||
import { BaseController } from '@/utils/generics/base-controller';
|
import { BaseController } from '@/utils/generics/base-controller';
|
||||||
import { DeleteResult } from '@/utils/generics/base-repository';
|
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 { PopulatePipe } from '@/utils/pipes/populate.pipe';
|
||||||
import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
|
import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
|
||||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
@ -63,23 +65,29 @@ export class BlockController extends BaseController<
|
|||||||
) {
|
) {
|
||||||
super(blockService);
|
super(blockService);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds blocks based on the provided query parameters.
|
* Finds blocks based on the provided query parameters.
|
||||||
* @param populate - An array of fields to populate in the returned blocks.
|
* @param populate - An array of fields to populate in the returned blocks.
|
||||||
* @param filters - Query filters to apply to the block search.
|
* @param filters - Query filters to apply to the block search.
|
||||||
* @returns A Promise that resolves to an array of found blocks.
|
* @returns A Promise that resolves to an array of found blocks.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async find(
|
async find(
|
||||||
@Query(PopulatePipe)
|
@Query(PopulatePipe)
|
||||||
populate: string[],
|
populate: string[],
|
||||||
@Query(new SearchFilterPipe<Block>({ allowedFields: ['category'] }))
|
@Query(new SearchFilterPipe<Block>({ allowedFields: ['category'] }))
|
||||||
filters: TFilterQuery<Block>,
|
filters: TFilterQuery<Block>,
|
||||||
|
@Query(PageQueryPipe) pageQuery?: PageQueryDto<Block>,
|
||||||
): Promise<Block[] | BlockFull[]> {
|
): 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)
|
return this.canPopulate(populate)
|
||||||
? await this.blockService.findAndPopulate(filters)
|
? await this.blockService.findAndPopulate(filters, pageQuery?.sort)
|
||||||
: await this.blockService.find(filters);
|
: await this.blockService.find(filters, pageQuery?.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,7 +57,11 @@ export class CategoryController extends BaseController<Category> {
|
|||||||
@Query(new SearchFilterPipe<Category>({ allowedFields: ['label'] }))
|
@Query(new SearchFilterPipe<Category>({ allowedFields: ['label'] }))
|
||||||
filters: TFilterQuery<Category>,
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,7 +60,11 @@ export class ContextVarController extends BaseController<ContextVar> {
|
|||||||
@Query(new SearchFilterPipe<ContextVar>({ allowedFields: ['label'] }))
|
@Query(new SearchFilterPipe<ContextVar>({ allowedFields: ['label'] }))
|
||||||
filters: TFilterQuery<ContextVar>,
|
filters: TFilterQuery<ContextVar>,
|
||||||
): Promise<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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,9 +62,15 @@ export class LabelController extends BaseController<
|
|||||||
@Query(new SearchFilterPipe<Label>({ allowedFields: ['name', 'title'] }))
|
@Query(new SearchFilterPipe<Label>({ allowedFields: ['name', 'title'] }))
|
||||||
filters: TFilterQuery<Label>,
|
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)
|
return this.canPopulate(populate)
|
||||||
? await this.labelService.findPageAndPopulate(filters, pageQuery)
|
? await this.labelService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.labelService.findPage(filters, pageQuery);
|
: await this.labelService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,6 +42,7 @@ import {
|
|||||||
MessageStub,
|
MessageStub,
|
||||||
} from '../schemas/message.schema';
|
} from '../schemas/message.schema';
|
||||||
import {
|
import {
|
||||||
|
AnyMessage,
|
||||||
OutgoingMessage,
|
OutgoingMessage,
|
||||||
OutgoingMessageFormat,
|
OutgoingMessageFormat,
|
||||||
StdOutgoingEnvelope,
|
StdOutgoingEnvelope,
|
||||||
@ -71,7 +72,7 @@ export class MessageController extends BaseController<
|
|||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async findPage(
|
async findPage(
|
||||||
@Query(PageQueryPipe) pageQuery: PageQueryDto<Message>,
|
@Query(PageQueryPipe) pageQuery: PageQueryDto<AnyMessage>,
|
||||||
@Query(PopulatePipe)
|
@Query(PopulatePipe)
|
||||||
populate: string[],
|
populate: string[],
|
||||||
@Query(
|
@Query(
|
||||||
@ -79,9 +80,15 @@ export class MessageController extends BaseController<
|
|||||||
)
|
)
|
||||||
filters: TFilterQuery<Message>,
|
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)
|
return this.canPopulate(populate)
|
||||||
? await this.messageService.findPageAndPopulate(filters, pageQuery)
|
? await this.messageService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.messageService.findPage(filters, pageQuery);
|
: await this.messageService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,9 +73,15 @@ export class SubscriberController extends BaseController<
|
|||||||
)
|
)
|
||||||
filters: TFilterQuery<Subscriber>,
|
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)
|
return this.canPopulate(populate)
|
||||||
? await this.subscriberService.findPageAndPopulate(filters, pageQuery)
|
? await this.subscriberService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.subscriberService.findPage(filters, pageQuery);
|
: await this.subscriberService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,7 +75,11 @@ export class ContentTypeController extends BaseController<ContentType> {
|
|||||||
@Query(new SearchFilterPipe<ContentType>({ allowedFields: ['name'] }))
|
@Query(new SearchFilterPipe<ContentType>({ allowedFields: ['name'] }))
|
||||||
filters: TFilterQuery<ContentType>,
|
filters: TFilterQuery<ContentType>,
|
||||||
) {
|
) {
|
||||||
return await this.contentTypeService.findPage(filters, pageQuery);
|
if (pageQuery.limit) {
|
||||||
|
return await this.contentTypeService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.contentTypeService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -194,9 +194,14 @@ export class ContentController extends BaseController<
|
|||||||
)
|
)
|
||||||
filters: TFilterQuery<Content>,
|
filters: TFilterQuery<Content>,
|
||||||
) {
|
) {
|
||||||
|
if (pageQuery.limit) {
|
||||||
|
return this.canPopulate(populate)
|
||||||
|
? await this.contentService.findPageAndPopulate(filters, pageQuery)
|
||||||
|
: await this.contentService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
return this.canPopulate(populate)
|
return this.canPopulate(populate)
|
||||||
? await this.contentService.findPageAndPopulate(filters, pageQuery)
|
? await this.contentService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.contentService.findPage(filters, pageQuery);
|
: await this.contentService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,7 +76,11 @@ export class MenuController extends BaseController<
|
|||||||
@Query(new SearchFilterPipe<Menu>({ allowedFields: ['parent'] }))
|
@Query(new SearchFilterPipe<Menu>({ allowedFields: ['parent'] }))
|
||||||
filters: TFilterQuery<Menu>,
|
filters: TFilterQuery<Menu>,
|
||||||
) {
|
) {
|
||||||
return await this.menuService.findPage(filters, pageQuery);
|
if (pageQuery.limit) {
|
||||||
|
return await this.menuService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.menuService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,7 +57,11 @@ export class LanguageController extends BaseController<Language> {
|
|||||||
@Query(new SearchFilterPipe<Language>({ allowedFields: ['title', 'code'] }))
|
@Query(new SearchFilterPipe<Language>({ allowedFields: ['title', 'code'] }))
|
||||||
filters: TFilterQuery<Language>,
|
filters: TFilterQuery<Language>,
|
||||||
) {
|
) {
|
||||||
return await this.languageService.findPage(filters, pageQuery);
|
if (pageQuery.limit) {
|
||||||
|
return await this.languageService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.languageService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,7 +55,10 @@ export class TranslationController extends BaseController<Translation> {
|
|||||||
@Query(new SearchFilterPipe<Translation>({ allowedFields: ['str'] }))
|
@Query(new SearchFilterPipe<Translation>({ allowedFields: ['str'] }))
|
||||||
filters: TFilterQuery<Translation>,
|
filters: TFilterQuery<Translation>,
|
||||||
) {
|
) {
|
||||||
return await this.translationService.findPage(filters, pageQuery);
|
if (pageQuery.limit) {
|
||||||
|
return await this.translationService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
return await this.translationService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,9 +135,15 @@ export class NlpEntityController extends BaseController<
|
|||||||
@Query(new SearchFilterPipe<NlpEntity>({ allowedFields: ['name', 'doc'] }))
|
@Query(new SearchFilterPipe<NlpEntity>({ allowedFields: ['name', 'doc'] }))
|
||||||
filters: TFilterQuery<NlpEntity>,
|
filters: TFilterQuery<NlpEntity>,
|
||||||
) {
|
) {
|
||||||
|
if (pageQuery.limit) {
|
||||||
|
return this.canPopulate(populate)
|
||||||
|
? await this.nlpEntityService.findPageAndPopulate(filters, pageQuery)
|
||||||
|
: await this.nlpEntityService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
return this.canPopulate(populate)
|
return this.canPopulate(populate)
|
||||||
? await this.nlpEntityService.findPageAndPopulate(filters, pageQuery)
|
? await this.nlpEntityService.findAndPopulate(filters)
|
||||||
: await this.nlpEntityService.findPage(filters, pageQuery);
|
: await this.nlpEntityService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -280,9 +280,15 @@ export class NlpSampleController extends BaseController<
|
|||||||
)
|
)
|
||||||
filters: TFilterQuery<NlpSample>,
|
filters: TFilterQuery<NlpSample>,
|
||||||
) {
|
) {
|
||||||
|
if (pageQuery.limit) {
|
||||||
|
return this.canPopulate(populate)
|
||||||
|
? await this.nlpSampleService.findPageAndPopulate(filters, pageQuery)
|
||||||
|
: await this.nlpSampleService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
return this.canPopulate(populate)
|
return this.canPopulate(populate)
|
||||||
? await this.nlpSampleService.findPageAndPopulate(filters, pageQuery)
|
? await this.nlpSampleService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.nlpSampleService.findPage(filters, pageQuery);
|
: await this.nlpSampleService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,9 +146,15 @@ export class NlpValueController extends BaseController<
|
|||||||
)
|
)
|
||||||
filters: TFilterQuery<NlpValue>,
|
filters: TFilterQuery<NlpValue>,
|
||||||
) {
|
) {
|
||||||
|
if (pageQuery.limit) {
|
||||||
|
return this.canPopulate(populate)
|
||||||
|
? await this.nlpValueService.findPageAndPopulate(filters, pageQuery)
|
||||||
|
: await this.nlpValueService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
return this.canPopulate(populate)
|
return this.canPopulate(populate)
|
||||||
? await this.nlpValueService.findPageAndPopulate(filters, pageQuery)
|
? await this.nlpValueService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.nlpValueService.findPage(filters, pageQuery);
|
: await this.nlpValueService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,9 +68,15 @@ export class RoleController extends BaseController<
|
|||||||
@Query(new SearchFilterPipe<Role>({ allowedFields: ['name'] }))
|
@Query(new SearchFilterPipe<Role>({ allowedFields: ['name'] }))
|
||||||
filters: TFilterQuery<Role>,
|
filters: TFilterQuery<Role>,
|
||||||
) {
|
) {
|
||||||
|
if (pageQuery.limit) {
|
||||||
|
return this.canPopulate(populate)
|
||||||
|
? await this.roleService.findPageAndPopulate(filters, pageQuery)
|
||||||
|
: await this.roleService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
return this.canPopulate(populate)
|
return this.canPopulate(populate)
|
||||||
? await this.roleService.findPageAndPopulate(filters, pageQuery)
|
? await this.roleService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.roleService.findPage(filters, pageQuery);
|
: await this.roleService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -168,9 +168,15 @@ export class ReadOnlyUserController extends BaseController<
|
|||||||
)
|
)
|
||||||
filters: TFilterQuery<User>,
|
filters: TFilterQuery<User>,
|
||||||
) {
|
) {
|
||||||
|
if (pageQuery.limit) {
|
||||||
|
return this.canPopulate(populate)
|
||||||
|
? await this.userService.findPageAndPopulate(filters, pageQuery)
|
||||||
|
: await this.userService.findPage(filters, pageQuery);
|
||||||
|
}
|
||||||
|
|
||||||
return this.canPopulate(populate)
|
return this.canPopulate(populate)
|
||||||
? await this.userService.findPageAndPopulate(filters, pageQuery)
|
? await this.userService.findAndPopulate(filters, pageQuery.sort)
|
||||||
: await this.userService.find(filters);
|
: await this.userService.find(filters, pageQuery.sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,7 +16,7 @@ export type QuerySortDto<T> = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export type PageQueryDto<T> = {
|
export type PageQueryDto<T> = {
|
||||||
skip: number;
|
skip: number | undefined;
|
||||||
limit: number;
|
limit: number | undefined;
|
||||||
sort: QuerySortDto<T>;
|
sort: QuerySortDto<T>;
|
||||||
};
|
};
|
||||||
|
@ -14,19 +14,21 @@ import { PageQueryDto } from './pagination-query.dto';
|
|||||||
|
|
||||||
const sortTypes = ['asc', 'desc'];
|
const sortTypes = ['asc', 'desc'];
|
||||||
|
|
||||||
|
export type PageQueryParams = { skip?: string; limit?: string; sort?: string };
|
||||||
|
|
||||||
export class PageQueryPipe<T>
|
export class PageQueryPipe<T>
|
||||||
implements
|
implements PipeTransform<PageQueryParams, PageQueryDto<T>>
|
||||||
PipeTransform<
|
|
||||||
{ skip: string; limit: string; sort: string },
|
|
||||||
PageQueryDto<T>
|
|
||||||
>
|
|
||||||
{
|
{
|
||||||
transform(value: { skip: string; limit: string; sort: string }) {
|
transform(value: PageQueryParams) {
|
||||||
const skip = parseInt(value.skip) > -1 ? parseInt(value.skip) : 0;
|
let skip: number | undefined = undefined;
|
||||||
const limit =
|
let limit: number | undefined = undefined;
|
||||||
parseInt(value.limit) > 0
|
if ('limit' in value) {
|
||||||
? parseInt(value.limit)
|
skip = parseInt(value.skip) > -1 ? parseInt(value.skip) : 0;
|
||||||
: config.pagination.limit;
|
limit =
|
||||||
|
parseInt(value.limit) > 0
|
||||||
|
? parseInt(value.limit)
|
||||||
|
: config.pagination.limit;
|
||||||
|
}
|
||||||
const [sortName = 'createdAt', sortType = 'desc'] =
|
const [sortName = 'createdAt', sortType = 'desc'] =
|
||||||
value.sort?.split(' ') || [];
|
value.sort?.split(' ') || [];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user