Merge branch 'main' into fix/cleanup-languageId-nlp-sample

This commit is contained in:
yassinedorbozgithub
2025-04-12 06:51:32 +01:00
45 changed files with 590 additions and 290 deletions

View File

@@ -93,8 +93,9 @@ export class BotStatsService extends BaseService<BotStats> {
) {
this.eventEmitter.emit(
'hook:stats:entry',
'retention',
BotStatsType.retention,
'Retentioned users',
subscriber,
);
}
}
@@ -106,7 +107,11 @@ export class BotStatsService extends BaseService<BotStats> {
* @param name - The name or identifier of the statistics entry (e.g., a specific feature or component being tracked).
*/
@OnEvent('hook:stats:entry')
async handleStatEntry(type: BotStatsType, name: string): Promise<void> {
async handleStatEntry(
type: BotStatsType,
name: string,
_subscriber: Subscriber,
): Promise<void> {
const day = new Date();
day.setMilliseconds(0);
day.setSeconds(0);

View File

@@ -361,4 +361,30 @@ describe('BlockController', () => {
).toBeDefined();
expect(result.patterns).toEqual(updateBlock.patterns);
});
it('should update the block trigger with a content payloadType payload', async () => {
jest.spyOn(blockService, 'updateOne');
const updateBlock: BlockUpdateDto = {
patterns: [
{
label: 'Content label',
value: 'Content value',
type: PayloadType.content,
},
],
};
const result = await blockController.updateOne(block.id, updateBlock);
expect(blockService.updateOne).toHaveBeenCalledWith(block.id, updateBlock);
expect(
result.patterns.find(
(pattern) =>
typeof pattern === 'object' &&
'type' in pattern &&
pattern.type === PayloadType.content &&
pattern,
),
).toBeDefined();
expect(result.patterns).toEqual(updateBlock.patterns);
});
});

View File

@@ -171,42 +171,38 @@ describe('BlockRepository', () => {
describe('prepareBlocksInCategoryUpdateScope', () => {
it('should update blocks within the scope based on category and ids', async () => {
jest.spyOn(blockRepository, 'findOne').mockResolvedValue({
id: blockValidIds[0],
category: 'oldCategory',
nextBlocks: [blockValidIds[1]],
attachedBlock: blockValidIds[1],
} as Block);
const mockUpdateOne = jest.spyOn(blockRepository, 'updateOne');
jest.spyOn(blockRepository, 'find').mockResolvedValue([
{
id: blockValidIds[0],
category: category.id,
nextBlocks: [blockValidIds[1]],
attachedBlock: blockValidIds[2],
},
] as Block[]);
jest.spyOn(blockRepository, 'updateOne');
await blockRepository.prepareBlocksInCategoryUpdateScope(
validCategory,
blockValidIds,
);
expect(mockUpdateOne).toHaveBeenCalledWith(blockValidIds[0], {
expect(blockRepository.updateOne).toHaveBeenCalledWith(blockValidIds[0], {
nextBlocks: [blockValidIds[1]],
attachedBlock: blockValidIds[1],
attachedBlock: blockValidIds[2],
});
});
it('should not update blocks if the category already matches', async () => {
jest.spyOn(blockRepository, 'findOne').mockResolvedValue({
id: validIds[0],
category: validCategory,
nextBlocks: [],
attachedBlock: null,
} as unknown as Block);
const mockUpdateOne = jest.spyOn(blockRepository, 'updateOne');
jest.spyOn(blockRepository, 'find').mockResolvedValue([]);
jest.spyOn(blockRepository, 'updateOne');
await blockRepository.prepareBlocksInCategoryUpdateScope(
validCategory,
validIds,
category.id,
blockValidIds,
);
expect(mockUpdateOne).not.toHaveBeenCalled();
expect(blockRepository.find).toHaveBeenCalled();
expect(blockRepository.updateOne).not.toHaveBeenCalled();
});
});

View File

@@ -172,22 +172,24 @@ export class BlockRepository extends BaseRepository<
category: string,
ids: string[],
): Promise<void> {
for (const id of ids) {
const oldState = await this.findOne(id);
if (oldState && oldState.category !== category) {
const updatedNextBlocks = oldState.nextBlocks?.filter((nextBlock) =>
ids.includes(nextBlock),
);
const blocks = await this.find({
_id: { $in: ids },
category: { $ne: category },
});
const updatedAttachedBlock = ids.includes(oldState.attachedBlock || '')
? oldState.attachedBlock
: null;
for (const { id, nextBlocks, attachedBlock } of blocks) {
const updatedNextBlocks = nextBlocks.filter((nextBlock) =>
ids.includes(nextBlock),
);
await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
}
const updatedAttachedBlock = ids.includes(attachedBlock || '')
? attachedBlock
: null;
await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
}
}

View File

@@ -16,6 +16,7 @@ import {
UpdateWithAggregationPipeline,
} from 'mongoose';
import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import { BaseRepository } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
@@ -47,7 +48,7 @@ export class SubscriberRepository extends BaseRepository<
async postCreate(created: SubscriberDocument): Promise<void> {
this.eventEmitter.emit(
'hook:stats:entry',
'new_users',
BotStatsType.new_users,
'New users',
created,
);

View File

@@ -42,4 +42,5 @@ export enum PayloadType {
button = 'button',
outcome = 'outcome',
menu = 'menu',
content = 'content',
}

View File

@@ -243,8 +243,8 @@ describe('BlockService', () => {
await botService.startConversation(event, block);
expect(hasBotSpoken).toEqual(true);
expect(triggeredEvents).toEqual([
['popular', 'hasNextBlocks'],
['new_conversations', 'New conversations'],
['popular', 'hasNextBlocks', webSubscriber],
['new_conversations', 'New conversations', webSubscriber],
]);
clearMock.mockClear();
});
@@ -301,7 +301,7 @@ describe('BlockService', () => {
const captured = await botService.processConversationMessage(event);
expect(captured).toBe(true);
expect(triggeredEvents).toEqual([
['existing_conversations', 'Existing conversations'],
['existing_conversations', 'Existing conversations', webSubscriber],
]);
clearMock.mockClear();
});

View File

@@ -9,6 +9,7 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import EventWrapper from '@/channel/lib/EventWrapper';
import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
@@ -65,8 +66,18 @@ export class BotService {
.getHandler()
.sendMessage(event, envelope, options, context);
this.eventEmitter.emit('hook:stats:entry', 'outgoing', 'Outgoing');
this.eventEmitter.emit('hook:stats:entry', 'all_messages', 'All Messages');
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.outgoing,
'Outgoing',
recipient,
);
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.all_messages,
'All Messages',
recipient,
);
// Trigger sent message event
const sentMessage: MessageCreateDto = {
@@ -293,7 +304,12 @@ export class BotService {
if (next) {
// Increment stats about popular blocks
this.eventEmitter.emit('hook:stats:entry', 'popular', next.name);
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.popular,
next.name,
convo.sender,
);
// Go next!
this.logger.debug('Respond to nested conversion! Go next ', next.id);
try {
@@ -352,8 +368,9 @@ export class BotService {
this.eventEmitter.emit(
'hook:stats:entry',
'existing_conversations',
BotStatsType.existing_conversations,
'Existing conversations',
subscriber,
);
this.logger.debug('Conversation has been captured! Responding ...');
return await this.handleIncomingMessage(conversation, event);
@@ -373,10 +390,15 @@ export class BotService {
* @param block - Starting block
*/
async startConversation(event: EventWrapper<any, any>, block: BlockFull) {
// Increment popular stats
this.eventEmitter.emit('hook:stats:entry', 'popular', block.name);
// Launching a new conversation
const subscriber = event.getSender();
// Increment popular stats
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.popular,
block.name,
subscriber,
);
try {
const convo = await this.conversationService.create({
@@ -384,8 +406,9 @@ export class BotService {
});
this.eventEmitter.emit(
'hook:stats:entry',
'new_conversations',
BotStatsType.new_conversations,
'New conversations',
subscriber,
);
try {

View File

@@ -11,6 +11,7 @@ import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import mime from 'mime';
import { v4 as uuidv4 } from 'uuid';
import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import {
AttachmentAccess,
@@ -149,11 +150,17 @@ export class ChatService {
}
this.websocketGateway.broadcastMessageReceived(populatedMsg, subscriber);
this.eventEmitter.emit('hook:stats:entry', 'incoming', 'Incoming');
this.eventEmitter.emit(
'hook:stats:entry',
'all_messages',
BotStatsType.incoming,
'Incoming',
subscriber,
);
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.all_messages,
'All Messages',
subscriber,
);
} catch (err) {
this.logger.error('Unable to log received message.', err, event);
@@ -248,7 +255,7 @@ export class ChatService {
};
this.eventEmitter.emit('hook:chatbot:sent', sentMessage, event);
this.eventEmitter.emit('hook:stats:entry', 'echo', 'Echo');
this.eventEmitter.emit('hook:stats:entry', 'echo', 'Echo', recipient);
} catch (err) {
this.logger.error('Unable to log echo message', err, event);
}

View File

@@ -13,6 +13,7 @@ import { AttachmentRepository } from '@/attachment/repositories/attachment.repos
import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { BlockService } from '@/chat/services/block.service';
import { FieldType } from '@/setting/schemas/types';
import { NOT_FOUND_ID } from '@/utils/constants/mock';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import { installContentFixtures } from '@/utils/test/fixtures/content';
@@ -100,27 +101,27 @@ describe('ContentTypeController', () => {
{
name: 'address',
label: 'Address',
type: 'text',
type: FieldType.text,
},
{
name: 'image',
label: 'Image',
type: 'file',
type: FieldType.file,
},
{
name: 'description',
label: 'Description',
type: 'html',
type: FieldType.html,
},
{
name: 'rooms',
label: 'Rooms',
type: 'file',
type: FieldType.file,
},
{
name: 'price',
label: 'Price',
type: 'file',
type: FieldType.file,
},
],
};

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -19,11 +19,12 @@ import {
ValidateNested,
} from 'class-validator';
import { FieldType } from '@/setting/schemas/types';
import { DtoConfig } from '@/utils/types/dto.types';
import { ValidateRequiredFields } from '../validators/validate-required-fields.validator';
export class FieldType {
export class ContentField {
@IsString()
@IsNotEmpty()
@Matches(/^[a-z][a-z_0-9]*$/)
@@ -34,11 +35,12 @@ export class FieldType {
label: string;
@IsString()
@IsEnum(['text', 'url', 'textarea', 'checkbox', 'file', 'html'], {
@IsNotEmpty()
@IsEnum(FieldType, {
message:
"type must be one of the following values: 'text', 'url', 'textarea', 'checkbox', 'file', 'html'",
})
type: string;
type: FieldType;
}
export class ContentTypeCreateDto {
@@ -47,13 +49,16 @@ export class ContentTypeCreateDto {
@IsNotEmpty()
name: string;
@ApiPropertyOptional({ description: 'Content type fields', type: FieldType })
@ApiPropertyOptional({
description: 'Content type fields',
type: ContentField,
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Validate(ValidateRequiredFields)
@Type(() => FieldType)
fields?: FieldType[];
@Type(() => ContentField)
fields?: ContentField[];
}
export class ContentTypeUpdateDto extends PartialType(ContentTypeCreateDto) {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -9,9 +9,12 @@
import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import mongoose from 'mongoose';
import { FieldType } from '@/setting/schemas/types';
import { BaseSchema } from '@/utils/generics/base-schema';
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager';
import { ContentField } from '../dto/contentType.dto';
@Schema({ timestamps: true })
export class ContentType extends BaseSchema {
/**
@@ -30,20 +33,16 @@ export class ContentType extends BaseSchema {
{
name: 'title',
label: 'Title',
type: 'text',
type: FieldType.text,
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
type: FieldType.checkbox,
},
],
})
fields: {
name: string;
label: string;
type: string;
}[];
fields: ContentField[];
}
export const ContentTypeModel: ModelDefinition = LifecycleHookManager.attach({

View File

@@ -145,7 +145,7 @@ export class ContentService extends BaseService<
...acc,
{
title: String(title),
status: Boolean(status),
status: status.trim().toLowerCase() === 'true',
entity: targetContentType,
dynamicFields: Object.keys(rest)
.filter((key) =>

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -12,24 +12,26 @@ import {
ValidatorConstraintInterface,
} from 'class-validator';
import { FieldType } from '../dto/contentType.dto';
import { FieldType } from '@/setting/schemas/types';
import { ContentField } from '../dto/contentType.dto';
@ValidatorConstraint({ name: 'validateRequiredFields', async: false })
export class ValidateRequiredFields implements ValidatorConstraintInterface {
private readonly REQUIRED_FIELDS: FieldType[] = [
private readonly REQUIRED_FIELDS: ContentField[] = [
{
name: 'title',
label: 'Title',
type: 'text',
type: FieldType.text,
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
type: FieldType.checkbox,
},
];
validate(fields: FieldType[]): boolean {
validate(fields: ContentField[]): boolean {
const errors: string[] = [];
this.REQUIRED_FIELDS.forEach((requiredField, index) => {

View File

@@ -12,6 +12,7 @@ import { OnEvent } from '@nestjs/event-emitter';
import { I18nService } from '@/i18n/services/i18n.service';
import { PluginService } from '@/plugins/plugins.service';
import { PluginType } from '@/plugins/types';
import { SettingType } from '@/setting/schemas/types';
import { SettingService } from '@/setting/services/setting.service';
import { BaseService } from '@/utils/generics/base-service';
@@ -57,21 +58,35 @@ export class TranslationService extends BaseService<Translation> {
PluginType.block,
block.message.plugin,
);
const defaultSettings = await plugin?.getDefaultSettings();
const defaultSettings = (await plugin?.getDefaultSettings()) || [];
const filteredSettings = defaultSettings.filter(
({ translatable, type }) =>
[
SettingType.text,
SettingType.textarea,
SettingType.multiple_text,
].includes(type) &&
(translatable === undefined || translatable === true),
);
const settingTypeMap = new Map(
filteredSettings.map((setting) => [setting.label, setting.type]),
);
// plugin
Object.entries(block.message.args).forEach(([l, arg]) => {
const setting = defaultSettings?.find(({ label }) => label === l);
if (setting?.translatable) {
if (Array.isArray(arg)) {
// array of text
strings = strings.concat(arg);
} else if (typeof arg === 'string') {
// text
strings.push(arg);
}
for (const [key, value] of Object.entries(block.message.args)) {
const settingType = settingTypeMap.get(key);
switch (settingType) {
case SettingType.multiple_text:
strings = strings.concat(value);
break;
case SettingType.text:
case SettingType.textarea:
strings.push(value);
break;
default:
break;
}
});
}
} else if ('text' in block.message && Array.isArray(block.message.text)) {
// array of text
strings = strings.concat(block.message.text);

View File

@@ -50,11 +50,11 @@ async function bootstrap() {
const settingService = app.get<SettingService>(SettingService);
app.enableCors({
origin: (origin, callback) => {
settingService
origin: async (origin, callback) => {
await settingService
.getAllowedOrigins()
.then((allowedOrigins) => {
if (!origin || allowedOrigins.has(origin)) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));

View File

@@ -10,17 +10,14 @@ import { BadRequestException, NotFoundException } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { getUpdateOneError } from '@/utils/test/errors/messages';
import { nlpEntityFixtures } from '@/utils/test/fixtures/nlpentity';
import {
installNlpValueFixtures,
nlpValueFixtures,
} from '@/utils/test/fixtures/nlpvalue';
import { getPageQuery } from '@/utils/test/pagination';
import {
closeInMongodConnection,
rootMongooseTestModule,
} from '@/utils/test/test';
import { TFixtures } from '@/utils/test/types';
import { buildTestingMocks } from '@/utils/test/utils';
import { NlpValueCreateDto } from '../dto/nlp-value.dto';
@@ -29,11 +26,7 @@ import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.rep
import { NlpValueRepository } from '../repositories/nlp-value.repository';
import { NlpEntityModel } from '../schemas/nlp-entity.schema';
import { NlpSampleEntityModel } from '../schemas/nlp-sample-entity.schema';
import {
NlpValue,
NlpValueFull,
NlpValueModel,
} from '../schemas/nlp-value.schema';
import { NlpValue, NlpValueModel } from '../schemas/nlp-value.schema';
import { NlpEntityService } from '../services/nlp-entity.service';
import { NlpValueService } from '../services/nlp-value.service';
@@ -80,63 +73,6 @@ describe('NlpValueController', () => {
afterEach(jest.clearAllMocks);
describe('findPage', () => {
it('should find nlp Values, and foreach nlp value populate the corresponding entity', async () => {
const pageQuery = getPageQuery<NlpValue>({
sort: ['value', 'desc'],
});
const result = await nlpValueController.findPage(
pageQuery,
['entity'],
{},
);
const nlpValueFixturesWithEntities = nlpValueFixtures.reduce(
(acc, curr) => {
acc.push({
...curr,
entity: nlpEntityFixtures[
parseInt(curr.entity!)
] as NlpValueFull['entity'],
builtin: curr.builtin!,
expressions: curr.expressions!,
metadata: curr.metadata!,
});
return acc;
},
[] as TFixtures<NlpValueFull>[],
);
expect(result).toEqualPayload(nlpValueFixturesWithEntities);
});
it('should find nlp Values', async () => {
const pageQuery = getPageQuery<NlpValue>({
sort: ['value', 'desc'],
});
const result = await nlpValueController.findPage(
pageQuery,
['invalidCriteria'],
{},
);
const nlpEntities = await nlpEntityService.findAll();
const nlpValueFixturesWithEntities = nlpValueFixtures.reduce(
(acc, curr) => {
const ValueWithEntities = {
...curr,
entity: curr.entity ? nlpEntities[parseInt(curr.entity!)].id : null,
expressions: curr.expressions!,
metadata: curr.metadata!,
builtin: curr.builtin!,
};
acc.push(ValueWithEntities);
return acc;
},
[] as TFixtures<NlpValueCreateDto>[],
);
expect(result).toEqualPayload(nlpValueFixturesWithEntities);
});
});
describe('count', () => {
it('should count the nlp Values', async () => {
const result = await nlpValueController.filterCount();

View File

@@ -30,6 +30,7 @@ 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';
import { Format } from '@/utils/types/format.types';
import { NlpValueCreateDto, NlpValueUpdateDto } from '../dto/nlp-value.dto';
import {
@@ -126,7 +127,7 @@ export class NlpValueController extends BaseController<
}
/**
* Retrieves a paginated list of NLP values.
* Retrieves a paginated list of NLP values with NLP Samples count.
*
* Supports filtering, pagination, and optional population of related entities.
*
@@ -134,10 +135,10 @@ export class NlpValueController extends BaseController<
* @param populate - An array of related entities to populate.
* @param filters - Filters to apply when retrieving the NLP values.
*
* @returns A promise resolving to a paginated list of NLP values.
* @returns A promise resolving to a paginated list of NLP values with NLP Samples count.
*/
@Get()
async findPage(
async findWithCount(
@Query(PageQueryPipe) pageQuery: PageQueryDto<NlpValue>,
@Query(PopulatePipe) populate: string[],
@Query(
@@ -147,9 +148,11 @@ export class NlpValueController extends BaseController<
)
filters: TFilterQuery<NlpValue>,
) {
return this.canPopulate(populate)
? await this.nlpValueService.findAndPopulate(filters, pageQuery)
: await this.nlpValueService.find(filters, pageQuery);
return await this.nlpValueService.findWithCount(
this.canPopulate(populate) ? Format.FULL : Format.STUB,
pageQuery,
filters,
);
}
/**

View File

@@ -8,10 +8,20 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
import { plainToInstance } from 'class-transformer';
import {
Document,
Model,
PipelineStage,
Query,
SortOrder,
Types,
} from 'mongoose';
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { TFilterQuery } from '@/utils/types/filter.types';
import { Format } from '@/utils/types/format.types';
import { NlpValueDto } from '../dto/nlp-value.dto';
import {
@@ -19,7 +29,10 @@ import {
NlpValue,
NlpValueDocument,
NlpValueFull,
NlpValueFullWithCount,
NlpValuePopulate,
NlpValueWithCount,
TNlpValueCount,
} from '../schemas/nlp-value.schema';
import { NlpSampleEntityRepository } from './nlp-sample-entity.repository';
@@ -106,4 +119,139 @@ export class NlpValueRepository extends BaseRepository<
throw new Error('Attempted to delete a NLP value using unknown criteria');
}
}
private getSortDirection(sortOrder: SortOrder) {
return typeof sortOrder === 'number'
? sortOrder
: sortOrder.toString().toLowerCase() === 'desc'
? -1
: 1;
}
/**
* Performs an aggregation to retrieve NLP values with their sample counts.
*
* @param format - The format can be full or stub
* @param pageQuery - The pagination parameters
* @param filterQuery - The filter criteria
* @returns Aggregated Nlp Value results with sample counts
*/
private async aggregateWithCount<F extends Format>(
format: F,
{
limit = 10,
skip = 0,
sort = ['createdAt', 'desc'],
}: PageQueryDto<NlpValue>,
{ $and = [], ...rest }: TFilterQuery<NlpValue>,
): Promise<TNlpValueCount<F>[]> {
const pipeline: PipelineStage[] = [
{
$match: {
...rest,
...($and.length
? {
$and: $and.map(({ entity, ...rest }) => ({
...rest,
...(entity
? { entity: new Types.ObjectId(String(entity)) }
: {}),
})),
}
: {}),
},
},
{
$skip: skip,
},
{
$limit: limit,
},
{
$lookup: {
from: 'nlpsampleentities',
localField: '_id',
foreignField: 'value',
as: '_sampleEntities',
},
},
{
$unwind: {
path: '$_sampleEntities',
preserveNullAndEmptyArrays: true,
},
},
{
$group: {
_id: '$_id',
_originalDoc: {
$first: {
$unsetField: { input: '$$ROOT', field: 'nlpSamplesCount' },
},
},
nlpSamplesCount: {
$sum: { $cond: [{ $ifNull: ['$_sampleEntities', false] }, 1, 0] },
},
},
},
{
$replaceWith: {
$mergeObjects: [
'$_originalDoc',
{ nlpSamplesCount: '$nlpSamplesCount' },
],
},
},
...(format === Format.FULL
? [
{
$lookup: {
from: 'nlpentities',
localField: 'entity',
foreignField: '_id',
as: 'entity',
},
},
{
$unwind: '$entity',
},
]
: []),
{
$sort: {
[sort[0]]: this.getSortDirection(sort[1]),
_id: this.getSortDirection(sort[1]),
},
},
];
return await this.model.aggregate<TNlpValueCount<F>>(pipeline).exec();
}
async findWithCount<F extends Format>(
format: F,
pageQuery: PageQueryDto<NlpValue>,
filterQuery: TFilterQuery<NlpValue>,
): Promise<TNlpValueCount<F>[]> {
try {
const aggregatedResults = await this.aggregateWithCount(
format,
pageQuery,
filterQuery,
);
if (format === Format.FULL) {
return plainToInstance(NlpValueFullWithCount, aggregatedResults, {
excludePrefixes: ['_'],
}) as TNlpValueCount<F>[];
}
return plainToInstance(NlpValueWithCount, aggregatedResults, {
excludePrefixes: ['_'],
}) as TNlpValueCount<F>[];
} catch (error) {
this.logger.error(`Error in findWithCount: ${error.message}`, error);
throw error;
}
}
}

View File

@@ -16,6 +16,7 @@ import {
TFilterPopulateFields,
THydratedDocument,
} from '@/utils/types/filter.types';
import { TStubOrFull } from '@/utils/types/format.types';
import { NlpEntity, NlpEntityFull } from './nlp-entity.schema';
import { NlpValueMap } from './types';
@@ -106,6 +107,14 @@ export class NlpValueFull extends NlpValueStub {
entity: NlpEntity;
}
export class NlpValueWithCount extends NlpValue {
nlpSamplesCount: number;
}
export class NlpValueFullWithCount extends NlpValueFull {
nlpSamplesCount: number;
}
export type NlpValueDocument = THydratedDocument<NlpValue>;
export const NlpValueModel: ModelDefinition = LifecycleHookManager.attach({
@@ -121,3 +130,9 @@ export type NlpValuePopulate = keyof TFilterPopulateFields<
>;
export const NLP_VALUE_POPULATE: NlpValuePopulate[] = ['entity'];
export type TNlpValueCount<T> = TStubOrFull<
T,
NlpValueWithCount,
NlpValueFullWithCount
>;

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -10,6 +10,9 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { DeleteResult } from '@/utils/generics/base-repository';
import { BaseService } from '@/utils/generics/base-service';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { TFilterQuery } from '@/utils/types/filter.types';
import { Format } from '@/utils/types/format.types';
import { NlpValueCreateDto, NlpValueDto } from '../dto/nlp-value.dto';
import { NlpValueRepository } from '../repositories/nlp-value.repository';
@@ -18,6 +21,7 @@ import {
NlpValue,
NlpValueFull,
NlpValuePopulate,
TNlpValueCount,
} from '../schemas/nlp-value.schema';
import { NlpSampleEntityValue } from '../schemas/types';
@@ -218,4 +222,12 @@ export class NlpValueService extends BaseService<
});
return Promise.all(promises);
}
async findWithCount<F extends Format>(
format: F,
pageQuery: PageQueryDto<NlpValue>,
filters: TFilterQuery<NlpValue>,
): Promise<TNlpValueCount<F>[]> {
return await this.repository.findWithCount(format, pageQuery, filters);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -20,6 +20,15 @@ export enum SettingType {
multiple_attachment = 'multiple_attachment',
}
export enum FieldType {
text = 'text',
url = 'url',
textarea = 'textarea',
checkbox = 'checkbox',
file = 'file',
html = 'html',
}
/**
* The following interfaces are declared, and currently not used
* TextSetting

View File

@@ -161,14 +161,12 @@ describe('SettingService', () => {
expect(settingService.find).toHaveBeenCalledWith({
label: 'allowed_domains',
});
expect(result).toEqual(
new Set([
'*',
'https://example.com',
'https://test.com',
'https://another.com',
]),
);
expect(result).toEqual([
'*',
'https://example.com',
'https://test.com',
'https://another.com',
]);
});
it('should return the config allowed cors only if no settings are found', async () => {
@@ -179,7 +177,7 @@ describe('SettingService', () => {
expect(settingService.find).toHaveBeenCalledWith({
label: 'allowed_domains',
});
expect(result).toEqual(new Set(['*']));
expect(result).toEqual(['*']);
});
it('should handle settings with empty values', async () => {
@@ -195,7 +193,7 @@ describe('SettingService', () => {
expect(settingService.find).toHaveBeenCalledWith({
label: 'allowed_domains',
});
expect(result).toEqual(new Set(['*', 'https://example.com']));
expect(result).toEqual(['*', 'https://example.com']);
});
});
});

View File

@@ -135,7 +135,7 @@ export class SettingService extends BaseService<Setting> {
* @returns A promise that resolves to a set of allowed origins
*/
@Cacheable(ALLOWED_ORIGINS_CACHE_KEY)
async getAllowedOrigins() {
async getAllowedOrigins(): Promise<string[]> {
const settings = (await this.find({
label: 'allowed_domains',
})) as TextSetting[];
@@ -150,7 +150,7 @@ export class SettingService extends BaseService<Setting> {
...allowedDomains,
]);
return uniqueOrigins;
return Array.from(uniqueOrigins);
}
/**

View File

@@ -13,6 +13,7 @@ import {
ContentType,
ContentTypeModel,
} from '@/cms/schemas/content-type.schema';
import { FieldType } from '@/setting/schemas/types';
import { getFixturesWithDefaultValues } from '../defaultValues';
import { FixturesTypeBuilder } from '../types';
@@ -27,12 +28,12 @@ export const contentTypeDefaultValues: TContentTypeFixtures['defaultValues'] = {
{
name: 'title',
label: 'Title',
type: 'text',
type: FieldType.text,
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
type: FieldType.checkbox,
},
],
};
@@ -44,27 +45,27 @@ const contentTypes: TContentTypeFixtures['values'][] = [
{
name: 'title',
label: 'Title',
type: 'text',
type: FieldType.text,
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
type: FieldType.checkbox,
},
{
name: 'description',
label: 'Description',
type: 'text',
type: FieldType.text,
},
{
name: 'image',
label: 'Image',
type: 'file',
type: FieldType.file,
},
{
name: 'subtitle',
label: 'Image',
type: 'file',
type: FieldType.file,
},
],
},
@@ -74,22 +75,22 @@ const contentTypes: TContentTypeFixtures['values'][] = [
{
name: 'title',
label: 'Title',
type: 'text',
type: FieldType.text,
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
type: FieldType.checkbox,
},
{
name: 'address',
label: 'Address',
type: 'text',
type: FieldType.text,
},
{
name: 'image',
label: 'Image',
type: 'file',
type: FieldType.file,
},
],
},
@@ -99,22 +100,22 @@ const contentTypes: TContentTypeFixtures['values'][] = [
{
name: 'title',
label: 'Title',
type: 'text',
type: FieldType.text,
},
{
name: 'status',
label: 'Status',
type: 'checkbox',
type: FieldType.checkbox,
},
{
name: 'address',
label: 'Address',
type: 'text',
type: FieldType.text,
},
{
name: 'image',
label: 'Image',
type: 'file',
type: FieldType.file,
},
],
},

View File

@@ -0,0 +1,18 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
export enum Format {
NONE = 0,
STUB = 1,
BASIC = 2,
FULL = 3,
}
export type TStubOrFull<TF, TStub, TFull> = TF extends Format.STUB
? TStub
: TFull;

View File

@@ -54,15 +54,15 @@ export const buildWebSocketGatewayOptions = (): Partial<ServerOptions> => {
...(config.sockets.cookie && { cookie: config.sockets.cookie }),
...(config.sockets.onlyAllowOrigins && {
cors: {
origin: (origin, cb) => {
origin: async (origin, cb) => {
// Retrieve the allowed origins from the settings
const app = AppInstance.getApp();
const settingService = app.get<SettingService>(SettingService);
settingService
await settingService
.getAllowedOrigins()
.then((allowedOrigins) => {
if (origin && allowedOrigins.has(origin)) {
if (origin && allowedOrigins.includes(origin)) {
cb(null, true);
} else {
// eslint-disable-next-line no-console