Skip to content

Commit

Permalink
Merge pull request #254 from Hexastack/revert-253-revert-250-feat/ref…
Browse files Browse the repository at this point in the history
…actor-helpers

feat/refactor helpers
  • Loading branch information
marrouchi authored Oct 21, 2024
2 parents b2c32fe + b7eef89 commit b3cafbc
Show file tree
Hide file tree
Showing 53 changed files with 901 additions and 731 deletions.
4 changes: 4 additions & 0 deletions api/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ node_modules
coverage*
README.md
test
*.spec.ts
*.mock.ts
__mock__
__test__
8 changes: 6 additions & 2 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

import path from 'path';

// eslint-disable-next-line import/order
import { MailerModule } from '@nestjs-modules/mailer';
// eslint-disable-next-line import/order
import { MjmlAdapter } from '@nestjs-modules/mailer/dist/adapters/mjml.adapter';
import { CacheModule } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { MongooseModule } from '@nestjs/mongoose';
import { MailerModule } from '@nestjs-modules/mailer';
import { MjmlAdapter } from '@nestjs-modules/mailer/dist/adapters/mjml.adapter';
import { CsrfGuard, CsrfModule } from '@tekuconcept/nestjs-csrf';
import {
AcceptLanguageResolver,
Expand All @@ -31,6 +33,7 @@ import { ChannelModule } from './channel/channel.module';
import { ChatModule } from './chat/chat.module';
import { CmsModule } from './cms/cms.module';
import { config } from './config';
import { HelperModule } from './helper/helper.module';
import { I18nModule } from './i18n/i18n.module';
import { LoggerModule } from './logger/logger.module';
import { NlpModule } from './nlp/nlp.module';
Expand Down Expand Up @@ -99,6 +102,7 @@ const i18nOptions: I18nOptions = {
ChatModule,
ChannelModule,
PluginsModule,
HelperModule,
LoggerModule,
WebsocketModule,
EventEmitterModule.forRoot({
Expand Down
3 changes: 1 addition & 2 deletions api/src/channel/channel.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { InjectDynamicProviders } from 'nestjs-dynamic-providers';
import { AttachmentModule } from '@/attachment/attachment.module';
import { ChatModule } from '@/chat/chat.module';
import { CmsModule } from '@/cms/cms.module';
import { NlpModule } from '@/nlp/nlp.module';

import { ChannelController } from './channel.controller';
import { ChannelMiddleware } from './channel.middleware';
Expand All @@ -29,7 +28,7 @@ export interface ChannelModuleOptions {
controllers: [WebhookController, ChannelController],
providers: [ChannelService],
exports: [ChannelService],
imports: [NlpModule, ChatModule, AttachmentModule, CmsModule, HttpModule],
imports: [ChatModule, AttachmentModule, CmsModule, HttpModule],
})
export class ChannelModule {
configure(consumer: MiddlewareConsumer) {
Expand Down
2 changes: 1 addition & 1 deletion api/src/channel/lib/EventWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
StdIncomingMessage,
} from '@/chat/schemas/types/message';
import { Payload } from '@/chat/schemas/types/quick-reply';
import { Nlp } from '@/nlp/lib/types';
import { Nlp } from '@/helper/types';

import ChannelHandler from './Handler';

Expand Down
27 changes: 8 additions & 19 deletions api/src/channel/lib/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import {
StdOutgoingMessage,
} from '@/chat/schemas/types/message';
import { LoggerService } from '@/logger/logger.service';
import BaseNlpHelper from '@/nlp/lib/BaseNlpHelper';
import { NlpService } from '@/nlp/services/nlp.service';
import { SettingService } from '@/setting/services/setting.service';
import { hyphenToUnderscore } from '@/utils/helpers/misc';
import { SocketRequest } from '@/websocket/utils/socket-request';
Expand All @@ -34,14 +32,11 @@ export default abstract class ChannelHandler<N extends string = string> {

private readonly settings: ChannelSetting<N>[];

protected NLP: BaseNlpHelper;

constructor(
name: N,
settings: ChannelSetting<N>[],
protected readonly settingService: SettingService,
private readonly channelService: ChannelService,
protected readonly nlpService: NlpService,
protected readonly logger: LoggerService,
) {
this.name = name;
Expand All @@ -56,10 +51,6 @@ export default abstract class ChannelHandler<N extends string = string> {
this.setup();
}

protected getGroup() {
return hyphenToUnderscore(this.getChannel()) as ChannelSetting<N>['group'];
}

async setup() {
await this.settingService.seedIfNotExist(
this.getChannel(),
Expand All @@ -68,19 +59,9 @@ export default abstract class ChannelHandler<N extends string = string> {
weight: i + 1,
})),
);
const nlp = this.nlpService.getNLP();
this.setNLP(nlp);
this.init();
}

setNLP(nlp: BaseNlpHelper) {
this.NLP = nlp;
}

getNLP() {
return this.NLP;
}

/**
* Returns the channel's name
* @returns Channel's name
Expand All @@ -89,6 +70,14 @@ export default abstract class ChannelHandler<N extends string = string> {
return this.name;
}

/**
* Returns the channel's group
* @returns Channel's group
*/
protected getGroup() {
return hyphenToUnderscore(this.getChannel()) as ChannelSetting<N>['group'];
}

/**
* Returns the channel's settings
* @returns Channel's settings
Expand Down
2 changes: 0 additions & 2 deletions api/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { MongooseModule } from '@nestjs/mongoose';
import { AttachmentModule } from '@/attachment/attachment.module';
import { ChannelModule } from '@/channel/channel.module';
import { CmsModule } from '@/cms/cms.module';
import { NlpModule } from '@/nlp/nlp.module';
import { UserModule } from '@/user/user.module';

import { BlockController } from './controllers/block.controller';
Expand Down Expand Up @@ -63,7 +62,6 @@ import { SubscriberService } from './services/subscriber.service';
forwardRef(() => ChannelModule),
CmsModule,
AttachmentModule,
NlpModule,
EventEmitter2,
UserModule,
],
Expand Down
46 changes: 1 addition & 45 deletions api/src/chat/repositories/message.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
* 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).
*/

import { Injectable, Optional } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { LanguageService } from '@/i18n/services/language.service';
import { LoggerService } from '@/logger/logger.service';
import { NlpSampleCreateDto } from '@/nlp/dto/nlp-sample.dto';
import { NlpSampleState } from '@/nlp/schemas/types';
import { NlpSampleService } from '@/nlp/services/nlp-sample.service';
import { BaseRepository } from '@/utils/generics/base-repository';

import {
Expand All @@ -33,18 +28,9 @@ export class MessageRepository extends BaseRepository<
MessagePopulate,
MessageFull
> {
private readonly nlpSampleService: NlpSampleService;

private readonly logger: LoggerService;

private readonly languageService: LanguageService;

constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Message.name) readonly model: Model<AnyMessage>,
@Optional() nlpSampleService?: NlpSampleService,
@Optional() logger?: LoggerService,
@Optional() languageService?: LanguageService,
) {
super(
eventEmitter,
Expand All @@ -53,9 +39,6 @@ export class MessageRepository extends BaseRepository<
MESSAGE_POPULATE,
MessageFull,
);
this.logger = logger;
this.nlpSampleService = nlpSampleService;
this.languageService = languageService;
}

/**
Expand All @@ -69,35 +52,8 @@ export class MessageRepository extends BaseRepository<
async preCreate(_doc: AnyMessage): Promise<void> {
if (_doc) {
if (!('sender' in _doc) && !('recipient' in _doc)) {
this.logger.error('Either sender or recipient must be provided!', _doc);
throw new Error('Either sender or recipient must be provided!');
}
// If message is sent by the user then add it as an inbox sample
if (
'sender' in _doc &&
_doc.sender &&
'message' in _doc &&
'text' in _doc.message
) {
const defaultLang = await this.languageService?.getDefaultLanguage();
const record: NlpSampleCreateDto = {
text: _doc.message.text,
type: NlpSampleState.inbox,
trained: false,
// @TODO : We need to define the language in the message entity
language: defaultLang.id,
};
try {
await this.nlpSampleService.findOneOrCreate(record, record);
this.logger.debug('User message saved as a inbox sample !');
} catch (err) {
this.logger.error(
'Unable to add message as a new inbox sample!',
err,
);
throw err;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion api/src/chat/schemas/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 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).
*/

import { Nlp } from '@/nlp/lib/types';
import { Nlp } from '@/helper/types';

import { Subscriber } from '../subscriber.schema';

Expand Down
2 changes: 1 addition & 1 deletion api/src/chat/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { Attachment } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import EventWrapper from '@/channel/lib/EventWrapper';
import { ContentService } from '@/cms/services/content.service';
import { Nlp } from '@/helper/types';
import { I18nService } from '@/i18n/services/i18n.service';
import { LanguageService } from '@/i18n/services/language.service';
import { LoggerService } from '@/logger/logger.service';
import { Nlp } from '@/nlp/lib/types';
import { PluginService } from '@/plugins/plugins.service';
import { PluginType } from '@/plugins/types';
import { SettingService } from '@/setting/services/setting.service';
Expand Down
31 changes: 5 additions & 26 deletions api/src/chat/services/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,12 @@ import { MenuService } from '@/cms/services/menu.service';
import { offlineEventText } from '@/extensions/channels/offline/__test__/events.mock';
import OfflineHandler from '@/extensions/channels/offline/index.channel';
import OfflineEventWrapper from '@/extensions/channels/offline/wrapper';
import { HelperService } from '@/helper/helper.service';
import { LanguageRepository } from '@/i18n/repositories/language.repository';
import { LanguageModel } from '@/i18n/schemas/language.schema';
import { I18nService } from '@/i18n/services/i18n.service';
import { LanguageService } from '@/i18n/services/language.service';
import { LoggerService } from '@/logger/logger.service';
import { NlpEntityRepository } from '@/nlp/repositories/nlp-entity.repository';
import { NlpSampleEntityRepository } from '@/nlp/repositories/nlp-sample-entity.repository';
import { NlpSampleRepository } from '@/nlp/repositories/nlp-sample.repository';
import { NlpValueRepository } from '@/nlp/repositories/nlp-value.repository';
import { NlpEntityModel } from '@/nlp/schemas/nlp-entity.schema';
import { NlpSampleEntityModel } from '@/nlp/schemas/nlp-sample-entity.schema';
import { NlpSampleModel } from '@/nlp/schemas/nlp-sample.schema';
import { NlpValueModel } from '@/nlp/schemas/nlp-value.schema';
import { NlpEntityService } from '@/nlp/services/nlp-entity.service';
import { NlpSampleEntityService } from '@/nlp/services/nlp-sample-entity.service';
import { NlpSampleService } from '@/nlp/services/nlp-sample.service';
import { NlpValueService } from '@/nlp/services/nlp-value.service';
import { NlpService } from '@/nlp/services/nlp.service';
import { PluginService } from '@/plugins/plugins.service';
import { SettingService } from '@/setting/services/setting.service';
import { installBlockFixtures } from '@/utils/test/fixtures/block';
Expand Down Expand Up @@ -109,10 +97,6 @@ describe('BlockService', () => {
SubscriberModel,
MessageModel,
MenuModel,
NlpValueModel,
NlpEntityModel,
NlpSampleEntityModel,
NlpSampleModel,
ContextVarModel,
LanguageModel,
]),
Expand All @@ -130,10 +114,6 @@ describe('BlockService', () => {
SubscriberRepository,
MessageRepository,
MenuRepository,
NlpValueRepository,
NlpEntityRepository,
NlpSampleEntityRepository,
NlpSampleRepository,
LanguageRepository,
BlockService,
CategoryService,
Expand All @@ -147,14 +127,13 @@ describe('BlockService', () => {
MessageService,
MenuService,
OfflineHandler,
NlpValueService,
NlpEntityService,
NlpSampleEntityService,
NlpSampleService,
NlpService,
ContextVarService,
ContextVarRepository,
LanguageService,
{
provide: HelperService,
useValue: {},
},
{
provide: PluginService,
useValue: {},
Expand Down
8 changes: 4 additions & 4 deletions api/src/chat/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';

import EventWrapper from '@/channel/lib/EventWrapper';
import { config } from '@/config';
import { HelperService } from '@/helper/helper.service';
import { LoggerService } from '@/logger/logger.service';
import { NlpService } from '@/nlp/services/nlp.service';
import { WebsocketGateway } from '@/websocket/websocket.gateway';

import { MessageCreateDto } from '../dto/message.dto';
Expand All @@ -35,7 +35,7 @@ export class ChatService {
private readonly subscriberService: SubscriberService,
private readonly botService: BotService,
private readonly websocketGateway: WebsocketGateway,
private readonly nlpService: NlpService,
private readonly helperService: HelperService,
) {}

/**
Expand Down Expand Up @@ -268,9 +268,9 @@ export class ChatService {
}

if (event.getText() && !event.getNLP()) {
const nlpAdapter = this.nlpService.getNLP();
try {
const nlp = await nlpAdapter.parse(event.getText());
const helper = await this.helperService.getDefaultNluHelper();
const nlp = await helper.predict(event.getText());
event.setNLP(nlp);
} catch (err) {
this.logger.error('Unable to perform NLP parse', err);
Expand Down
3 changes: 0 additions & 3 deletions api/src/extensions/channels/live-chat-tester/index.channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { SubscriberService } from '@/chat/services/subscriber.service';
import { MenuService } from '@/cms/services/menu.service';
import { I18nService } from '@/i18n/services/i18n.service';
import { LoggerService } from '@/logger/logger.service';
import { NlpService } from '@/nlp/services/nlp.service';
import { SettingService } from '@/setting/services/setting.service';
import { WebsocketGateway } from '@/websocket/websocket.gateway';

Expand All @@ -34,7 +33,6 @@ export default class LiveChatTesterHandler extends BaseWebChannelHandler<
constructor(
settingService: SettingService,
channelService: ChannelService,
nlpService: NlpService,
logger: LoggerService,
eventEmitter: EventEmitter2,
i18n: I18nService,
Expand All @@ -49,7 +47,6 @@ export default class LiveChatTesterHandler extends BaseWebChannelHandler<
DEFAULT_LIVE_CHAT_TEST_SETTINGS,
settingService,
channelService,
nlpService,
logger,
eventEmitter,
i18n,
Expand Down
Loading

0 comments on commit b3cafbc

Please sign in to comment.