2024-10-09 07:26:39 +00:00
|
|
|
import { type OnEventOptions } from '@nestjs/event-emitter/dist/interfaces';
|
2024-10-19 06:10:59 +00:00
|
|
|
import type { Listener, OnOptions } from 'eventemitter2';
|
|
|
|
import type {
|
|
|
|
Document,
|
|
|
|
Query,
|
|
|
|
TFilterQuery,
|
|
|
|
THydratedDocument,
|
|
|
|
} from 'mongoose';
|
2024-10-09 07:26:39 +00:00
|
|
|
import { type Socket } from 'socket.io';
|
|
|
|
|
|
|
|
import { type BotStats } from '@/analytics/schemas/bot-stats.schema';
|
|
|
|
import { type Attachment } from '@/attachment/schemas/attachment.schema';
|
|
|
|
import type EventWrapper from '@/channel/lib/EventWrapper';
|
2024-10-19 06:10:59 +00:00
|
|
|
import type { Block, BlockFull } from '@/chat/schemas/block.schema';
|
2024-10-09 07:26:39 +00:00
|
|
|
import { type Category } from '@/chat/schemas/category.schema';
|
|
|
|
import { type ContextVar } from '@/chat/schemas/context-var.schema';
|
|
|
|
import { type Conversation } from '@/chat/schemas/conversation.schema';
|
2024-10-17 05:55:51 +00:00
|
|
|
import type { Label, LabelDocument } from '@/chat/schemas/label.schema';
|
2024-10-09 07:26:39 +00:00
|
|
|
import { type Message } from '@/chat/schemas/message.schema';
|
|
|
|
import { type Subscriber } from '@/chat/schemas/subscriber.schema';
|
|
|
|
import { type ContentType } from '@/cms/schemas/content-type.schema';
|
|
|
|
import { type Content } from '@/cms/schemas/content.schema';
|
|
|
|
import { type Menu } from '@/cms/schemas/menu.schema';
|
|
|
|
import { type Language } from '@/i18n/schemas/language.schema';
|
|
|
|
import { type Translation } from '@/i18n/schemas/translation.schema';
|
|
|
|
import type {
|
|
|
|
NlpEntity,
|
|
|
|
NlpEntityDocument,
|
|
|
|
} from '@/nlp/schemas/nlp-entity.schema';
|
|
|
|
import { type NlpSampleEntity } from '@/nlp/schemas/nlp-sample-entity.schema';
|
|
|
|
import { type NlpSample } from '@/nlp/schemas/nlp-sample.schema';
|
2024-10-19 06:10:59 +00:00
|
|
|
import {
|
2024-10-09 07:26:39 +00:00
|
|
|
NlpValueDocument,
|
2024-10-19 06:10:59 +00:00
|
|
|
type NlpValue,
|
2024-10-09 07:26:39 +00:00
|
|
|
} from '@/nlp/schemas/nlp-value.schema';
|
|
|
|
import { type Setting } from '@/setting/schemas/setting.schema';
|
2024-10-09 09:31:40 +00:00
|
|
|
import type { CheckboxSetting, TextSetting } from '@/setting/schemas/types';
|
2024-10-09 07:26:39 +00:00
|
|
|
import { type Invitation } from '@/user/schemas/invitation.schema';
|
|
|
|
import { type Model } from '@/user/schemas/model.schema';
|
|
|
|
import { type Permission } from '@/user/schemas/permission.schema';
|
|
|
|
import { type Role } from '@/user/schemas/role.schema';
|
|
|
|
import { type User } from '@/user/schemas/user.schema';
|
|
|
|
import { EHook, type DeleteResult } from '@/utils/generics/base-repository';
|
|
|
|
|
|
|
|
import { type SubscriberUpdateDto } from './chat/dto/subscriber.dto';
|
2024-10-07 14:37:08 +00:00
|
|
|
|
|
|
|
import '@nestjs/event-emitter';
|
|
|
|
/**
|
|
|
|
* @description Module declaration that extends the NestJS EventEmitter with custom event types and methods.
|
|
|
|
*/
|
|
|
|
declare module '@nestjs/event-emitter' {
|
|
|
|
interface TDefinition<S, O = object> {
|
|
|
|
schema: S;
|
|
|
|
operations: O;
|
|
|
|
}
|
2024-10-09 07:26:39 +00:00
|
|
|
|
|
|
|
interface IHookExtensionsOperationMap {
|
2024-10-09 10:50:50 +00:00
|
|
|
messenger: TDefinition<
|
2024-10-09 07:26:39 +00:00
|
|
|
object,
|
|
|
|
{
|
2024-10-09 09:31:40 +00:00
|
|
|
get_started_button: Setting;
|
|
|
|
access_token: Setting;
|
|
|
|
composer_input_disabled: CheckboxSetting;
|
|
|
|
greeting_text: TextSetting;
|
2024-10-09 07:26:39 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IHookSettingsGroupLabelOperationMap {
|
|
|
|
chatbot_settings: TDefinition<
|
|
|
|
object,
|
|
|
|
{
|
2024-10-16 17:50:13 +00:00
|
|
|
global_fallback: Setting;
|
|
|
|
fallback_block: Setting;
|
|
|
|
fallback_message: Setting;
|
2024-10-09 07:26:39 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
contact: TDefinition<
|
|
|
|
object,
|
|
|
|
{
|
2024-10-16 17:50:13 +00:00
|
|
|
contact_email_recipient: Setting;
|
|
|
|
company_name: Setting;
|
|
|
|
company_phone: Setting;
|
|
|
|
company_email: Setting;
|
|
|
|
company_address1: Setting;
|
|
|
|
company_address2: Setting;
|
|
|
|
company_city: Setting;
|
|
|
|
company_zipcode: Setting;
|
|
|
|
company_state: Setting;
|
|
|
|
company_country: Setting;
|
2024-10-09 07:26:39 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
nlp_settings: TDefinition<
|
|
|
|
object,
|
|
|
|
{
|
2024-10-16 17:50:13 +00:00
|
|
|
provider: Setting;
|
|
|
|
endpoint: Setting;
|
|
|
|
token: Setting;
|
|
|
|
threshold: Setting;
|
2024-10-09 07:26:39 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* custom hooks */
|
|
|
|
interface IHookOperationMap
|
|
|
|
extends IHookSettingsGroupLabelOperationMap,
|
|
|
|
IHookExtensionsOperationMap {
|
2024-10-07 14:37:08 +00:00
|
|
|
analytics: TDefinition<
|
|
|
|
object,
|
|
|
|
{
|
|
|
|
block: BlockFull;
|
|
|
|
passation: Subscriber;
|
|
|
|
'fallback-local': BlockFull;
|
2024-10-09 07:26:39 +00:00
|
|
|
'fallback-global': EventWrapper<any, any>;
|
2024-10-07 14:37:08 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
chatbot: TDefinition<
|
|
|
|
object,
|
|
|
|
{
|
2024-10-09 07:29:05 +00:00
|
|
|
sent: unknown;
|
|
|
|
received: unknown;
|
2024-10-07 14:37:08 +00:00
|
|
|
message: unknown;
|
|
|
|
delivery: unknown;
|
|
|
|
read: unknown;
|
|
|
|
typing: unknown;
|
|
|
|
follow: unknown;
|
|
|
|
echo: unknown;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
websocket: TDefinition<
|
|
|
|
object,
|
|
|
|
{
|
|
|
|
connection: Socket;
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
}
|
2024-10-09 07:26:39 +00:00
|
|
|
|
|
|
|
/* entities hooks */
|
2024-10-07 14:37:08 +00:00
|
|
|
interface IHookEntityOperationMap extends IHookOperationMap {
|
|
|
|
stats: TDefinition<BotStats, { entry: string }>;
|
|
|
|
attachment: TDefinition<Attachment>;
|
|
|
|
block: TDefinition<Block>;
|
|
|
|
category: TDefinition<Category>;
|
|
|
|
contextVar: TDefinition<ContextVar>;
|
|
|
|
conversation: TDefinition<Conversation, { end: unknown; close: unknown }>;
|
|
|
|
label: TDefinition<
|
|
|
|
Label,
|
2024-10-09 07:26:39 +00:00
|
|
|
{ create: LabelDocument; delete: Label | Label[] }
|
2024-10-07 14:37:08 +00:00
|
|
|
>;
|
|
|
|
message: TDefinition<Message>;
|
|
|
|
subscriber: TDefinition<Subscriber, { assign: SubscriberUpdateDto }>;
|
|
|
|
contentType: TDefinition<ContentType>;
|
|
|
|
content: TDefinition<Content>;
|
|
|
|
menu: TDefinition<Menu>;
|
2024-10-09 07:26:39 +00:00
|
|
|
language: TDefinition<Language, { delete: Language | Language[] }>;
|
2024-10-07 14:37:08 +00:00
|
|
|
translation: TDefinition<Translation>;
|
|
|
|
nlpEntity: TDefinition<
|
|
|
|
NlpEntity,
|
|
|
|
{
|
2024-10-09 07:26:39 +00:00
|
|
|
create: NlpEntityDocument;
|
2024-10-07 14:37:08 +00:00
|
|
|
update: NlpEntity;
|
2024-10-09 07:26:39 +00:00
|
|
|
delete: NlpEntity | NlpEntity[];
|
2024-10-07 14:37:08 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
nlpSampleEntity: TDefinition<NlpSampleEntity>;
|
|
|
|
nlpSample: TDefinition<NlpSample>;
|
|
|
|
nlpValue: TDefinition<
|
|
|
|
NlpValue,
|
2024-10-09 07:26:39 +00:00
|
|
|
{
|
|
|
|
create: NlpValueDocument;
|
|
|
|
update: NlpValue;
|
|
|
|
delete: NlpValue | NlpValue[];
|
|
|
|
}
|
2024-10-07 14:37:08 +00:00
|
|
|
>;
|
|
|
|
setting: TDefinition<Setting>;
|
|
|
|
invitation: TDefinition<Invitation>;
|
|
|
|
model: TDefinition<Model>;
|
|
|
|
permission: TDefinition<Permission>;
|
|
|
|
role: TDefinition<Role>;
|
|
|
|
user: TDefinition<User, { lastvisit: Subscriber }>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description A constrained string type that allows specific string values while preserving type safety.
|
|
|
|
*/
|
|
|
|
type ConstrainedString = string & Record<never, never>;
|
2024-10-09 07:26:39 +00:00
|
|
|
type EventNamespaces = keyof IHookEntityOperationMap;
|
|
|
|
|
|
|
|
/* pre hooks */
|
2024-10-19 06:10:59 +00:00
|
|
|
type TPreValidate<T> = THydratedDocument<T>;
|
|
|
|
type TPreCreate<T> = THydratedDocument<T>;
|
2024-10-09 07:26:39 +00:00
|
|
|
type TPreUpdate<T> = TFilterQuery<T> & object;
|
2024-10-19 06:10:59 +00:00
|
|
|
type TPreDelete<T> = Query<
|
2024-10-09 07:26:39 +00:00
|
|
|
DeleteResult,
|
|
|
|
Document<T>,
|
|
|
|
unknown,
|
|
|
|
T,
|
|
|
|
'deleteOne',
|
|
|
|
Record<string, never>
|
|
|
|
>;
|
|
|
|
type TPreUnion<T> =
|
|
|
|
| TPreValidate<T>
|
|
|
|
| TPreCreate<T>
|
|
|
|
| TPreUpdate<T>
|
2024-10-19 06:10:59 +00:00
|
|
|
| TPreDelete<T>;
|
2024-10-09 07:26:39 +00:00
|
|
|
|
|
|
|
/* post hooks */
|
2024-10-19 06:10:59 +00:00
|
|
|
type TPostValidate<T> = THydratedDocument<T>;
|
|
|
|
type TPostCreate<T> = THydratedDocument<T>;
|
|
|
|
type TPostUpdate<T> = THydratedDocument<T>;
|
2024-10-09 07:26:39 +00:00
|
|
|
type TPostDelete = DeleteResult;
|
|
|
|
type TPostUnion<T> =
|
|
|
|
| TPostValidate<T>
|
|
|
|
| TPostCreate<T>
|
|
|
|
| TPostUpdate<T>
|
|
|
|
| TPostDelete;
|
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
type TCustomOperations<E extends keyof IHookEntityOperationMap> =
|
|
|
|
IHookEntityOperationMap[E]['operations'][keyof IHookEntityOperationMap[E]['operations']];
|
2024-10-09 07:26:39 +00:00
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
/* union hooks */
|
|
|
|
type TUnion<G, E> = E extends keyof IHookEntityOperationMap
|
|
|
|
? E extends keyof IHookOperationMap
|
|
|
|
? TCustomOperations<E>
|
|
|
|
: TPreUnion<G> | TPostUnion<G> | TCustomOperations<E>
|
2024-10-09 07:26:39 +00:00
|
|
|
: never;
|
|
|
|
|
|
|
|
/* Normalized hook */
|
|
|
|
enum EHookPrefix {
|
|
|
|
pre = 'pre',
|
|
|
|
post = 'post',
|
|
|
|
}
|
2024-10-07 14:37:08 +00:00
|
|
|
|
|
|
|
type TCompatibleHook<
|
2024-10-09 07:26:39 +00:00
|
|
|
P extends `${EHookPrefix}`,
|
2024-10-07 14:37:08 +00:00
|
|
|
T = `${EHook}`,
|
|
|
|
> = T extends `${P}${infer I}` ? `${P}${I}` : never;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
type TPreHook = TCompatibleHook<EHookPrefix.pre>;
|
|
|
|
type TPostHook = TCompatibleHook<EHookPrefix.post>;
|
2024-10-07 14:37:08 +00:00
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
type TNormalizedEvents = '*' | TPreHook | TPostHook;
|
2024-10-07 14:37:08 +00:00
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
type TNormalizedHooks<
|
|
|
|
E extends keyof IHookEntityOperationMap,
|
|
|
|
T = IHookEntityOperationMap[E]['schema'],
|
|
|
|
> =
|
|
|
|
| {
|
|
|
|
[EHook.preValidate]: TPreValidate<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.preCreate]: TPreCreate<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.preUpdate]: TPostUpdate<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.preDelete]: TPreDelete<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.postValidate]: TPostValidate<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.postCreate]: TPostCreate<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.postUpdate]: TPostUpdate<T>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
[EHook.postDelete]: TPostDelete;
|
|
|
|
};
|
|
|
|
|
|
|
|
type TNormalizedHook<E extends keyof IHookEntityOperationMap, O> = Extract<
|
|
|
|
TNormalizedHooks<E>,
|
|
|
|
{ [key in O]: unknown }
|
|
|
|
>[O];
|
2024-10-07 14:37:08 +00:00
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
/* Extended hook */
|
2024-10-19 06:10:59 +00:00
|
|
|
type TExtendedHook<
|
|
|
|
E extends keyof IHookEntityOperationMap,
|
|
|
|
O extends keyof IHookEntityOperationMap[E]['operations'],
|
|
|
|
> = IHookEntityOperationMap[E]['operations'][O];
|
2024-10-07 14:37:08 +00:00
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
type EventValueOf<G> = G extends `hook:${infer E}:${infer O}`
|
|
|
|
? O extends '*'
|
|
|
|
? TUnion<G, E>
|
|
|
|
: E extends keyof IHookEntityOperationMap
|
|
|
|
? O extends keyof IHookEntityOperationMap[E]['operations']
|
|
|
|
? TExtendedHook<E, O>
|
|
|
|
: TNormalizedHook<E, O>
|
|
|
|
: never
|
|
|
|
: never;
|
2024-10-07 14:37:08 +00:00
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
type IsHookEvent<G extends EventNamespaces | ConstrainedString> =
|
|
|
|
G extends EventNamespaces
|
|
|
|
? true
|
|
|
|
: G extends `hook:${infer N}:${string}`
|
|
|
|
? N extends keyof IHookEntityOperationMap
|
|
|
|
? true
|
|
|
|
: false
|
|
|
|
: false;
|
2024-10-07 14:37:08 +00:00
|
|
|
|
2024-10-19 06:10:59 +00:00
|
|
|
type TCustomEvents<G extends keyof IHookEntityOperationMap> =
|
|
|
|
keyof IHookEntityOperationMap[G]['operations'] & string;
|
|
|
|
|
|
|
|
type customEvent<G extends EventNamespaces | ConstrainedString> =
|
|
|
|
G extends EventNamespaces
|
|
|
|
? G extends `hook:${string}`
|
|
|
|
? G
|
|
|
|
: `hook:${G}:${TNormalizedEvents | TCustomEvents<G>}`
|
|
|
|
: never;
|
2024-10-07 14:37:08 +00:00
|
|
|
|
|
|
|
export interface ListenerFn<G extends EventNamespaces | ConstrainedString> {
|
|
|
|
(value: EventValueOf<G>, ...values: any[]): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EventEmitter2 {
|
2024-10-09 07:26:39 +00:00
|
|
|
emit<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
value: EventValueOf<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
...values: any[]
|
|
|
|
): boolean;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
emitAsync<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
value: EventValueOf<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
...values: any[]
|
|
|
|
): Promise<any[]>;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
addListener<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
): this | Listener;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
on<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
options?: boolean | OnOptions,
|
|
|
|
): this | Listener;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
once<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
options?: true | OnOptions,
|
|
|
|
): this | Listener;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
prependOnceListener<
|
|
|
|
G extends EventNamespaces | ConstrainedString,
|
|
|
|
H extends G,
|
|
|
|
>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
options?: boolean | OnOptions,
|
|
|
|
): this | Listener;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
many<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
|
|
|
timesToListen: number,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
options?: boolean | OnOptions,
|
|
|
|
): this | Listener;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
prependMany<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
|
|
|
timesToListen: number,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
options?: boolean | OnOptions,
|
|
|
|
): this | Listener;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
removeListener<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
): this;
|
|
|
|
|
2024-10-09 07:26:39 +00:00
|
|
|
off<G extends EventNamespaces | ConstrainedString, H extends G>(
|
2024-10-07 14:37:08 +00:00
|
|
|
customEvent: customEvent<G>,
|
2024-10-09 07:26:39 +00:00
|
|
|
listener: ListenerFn<H>,
|
2024-10-07 14:37:08 +00:00
|
|
|
): this;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare type OnEventMethodDecorator<
|
|
|
|
G extends EventNamespaces | ConstrainedString,
|
|
|
|
> = <T, K extends keyof T>(
|
|
|
|
target: IsHookEvent<G> extends true
|
|
|
|
? [T[K]] extends [(params: EventValueOf<G>, ...rest: any[]) => any]
|
|
|
|
? T
|
|
|
|
: never
|
|
|
|
: T,
|
|
|
|
propertyKey: K,
|
|
|
|
) => void;
|
|
|
|
|
|
|
|
export declare function OnEvent<
|
|
|
|
G extends EventNamespaces | ConstrainedString,
|
2024-10-09 07:26:39 +00:00
|
|
|
H extends G,
|
2024-10-07 14:37:08 +00:00
|
|
|
>(
|
|
|
|
event: customEvent<G>,
|
|
|
|
options?: OnEventOptions | undefined,
|
2024-10-09 07:26:39 +00:00
|
|
|
): OnEventMethodDecorator<H>;
|
2024-10-07 14:37:08 +00:00
|
|
|
}
|