fix: CMS module issues

This commit is contained in:
yassinedorbozgithub
2025-01-07 11:37:03 +01:00
parent 2910de0058
commit 0101655e33
25 changed files with 235 additions and 207 deletions

View File

@@ -161,7 +161,7 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
payload: string | Payload,
block: BlockFull | Block,
): PayloadPattern | undefined {
const payloadPatterns = block.patterns.filter(
const payloadPatterns = block.patterns?.filter(
(p) => typeof p === 'object' && 'label' in p,
) as PayloadPattern[];
@@ -190,57 +190,56 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
block: Block | BlockFull,
): (RegExpMatchArray | string)[] | false {
// Filter text patterns & Instanciate Regex patterns
const patterns: (string | RegExp | Pattern)[] = block.patterns.map(
(pattern) => {
if (
typeof pattern === 'string' &&
pattern.endsWith('/') &&
pattern.startsWith('/')
) {
return new RegExp(pattern.slice(1, -1), 'i');
}
return pattern;
},
);
// Return first match
for (let i = 0; i < patterns.length; i++) {
const pattern = patterns[i];
if (pattern instanceof RegExp) {
if (pattern.test(text)) {
const matches = text.match(pattern);
if (matches) {
if (matches.length >= 2) {
// Remove global match if needed
matches.shift();
}
return matches;
}
}
continue;
} else if (
typeof pattern === 'object' &&
'label' in pattern &&
text.trim().toLowerCase() === pattern.label.toLowerCase()
) {
// Payload (quick reply)
return [text];
} else if (
const patterns: undefined | Pattern[] = block.patterns?.map((pattern) => {
if (
typeof pattern === 'string' &&
text.trim().toLowerCase() === pattern.toLowerCase()
pattern.endsWith('/') &&
pattern.startsWith('/')
) {
// Equals
return [text];
return new RegExp(pattern.slice(1, -1), 'i');
}
return pattern;
});
if (patterns?.length)
// Return first match
for (let i = 0; i < patterns.length; i++) {
const pattern = patterns[i];
if (pattern instanceof RegExp) {
if (pattern.test(text)) {
const matches = text.match(pattern);
if (matches) {
if (matches.length >= 2) {
// Remove global match if needed
matches.shift();
}
return matches;
}
}
continue;
} else if (
typeof pattern === 'object' &&
'label' in pattern &&
text.trim().toLowerCase() === pattern.label.toLowerCase()
) {
// Payload (quick reply)
return [text];
} else if (
typeof pattern === 'string' &&
text.trim().toLowerCase() === pattern.toLowerCase()
) {
// Equals
return [text];
}
// @deprecated
// else if (
// typeof pattern === 'string' &&
// Soundex(text) === Soundex(pattern)
// ) {
// // Sound like
// return [text];
// }
}
// @deprecated
// else if (
// typeof pattern === 'string' &&
// Soundex(text) === Soundex(pattern)
// ) {
// // Sound like
// return [text];
// }
}
// No match
return false;
}
@@ -262,7 +261,7 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
return undefined;
}
const nlpPatterns = block.patterns.filter((p) => {
const nlpPatterns = block.patterns?.filter((p) => {
return Array.isArray(p);
}) as NlpPattern[][];
@@ -437,10 +436,10 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
subscriberContext: SubscriberContext,
fallback = false,
conversationId?: string,
): Promise<StdOutgoingEnvelope> {
) {
const settings = await this.settingService.getSettings();
const blockMessage: BlockMessage =
fallback && block.options.fallback
fallback && block.options?.fallback
? [...block.options.fallback.message]
: Array.isArray(block.message)
? [...block.message]
@@ -557,7 +556,7 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
} else if (
blockMessage &&
'elements' in blockMessage &&
block.options.content
block.options?.content
) {
const contentBlockOptions = block.options.content;
// Hadnle pagination for list/carousel
@@ -599,7 +598,7 @@ export class BlockService extends BaseService<Block, BlockPopulate, BlockFull> {
);
// Process custom plugin block
try {
return await plugin.process(block, context, conversationId);
return await plugin?.process(block, context, conversationId);
} catch (e) {
this.logger.error('Plugin was unable to load/process ', e);
throw new Error(`Unknown plugin - ${JSON.stringify(blockMessage)}`);

View File

@@ -74,7 +74,7 @@ export class BotService {
await this.blockService.processMessage(
block,
context,
recipient.context,
recipient?.context,
fallback,
conservationId,
);
@@ -112,7 +112,7 @@ export class BotService {
// Apply updates : Assign block labels to user
const blockLabels = (block.assign_labels || []).map(({ id }) => id);
const assignTo = block.options.assignTo || null;
const assignTo = block.options?.assignTo || null;
await this.subscriberService.applyUpdates(
event.getSender(),
blockLabels,
@@ -223,13 +223,12 @@ export class BotService {
_id: { $in: nextIds },
});
let fallback = false;
const fallbackOptions =
convo.current && convo.current.options.fallback
? convo.current.options.fallback
: {
active: false,
max_attempts: 0,
};
const fallbackOptions = convo.current?.options?.fallback
? convo.current.options.fallback
: {
active: false,
max_attempts: 0,
};
// Find the next block that matches
const matchedBlock = await this.blockService.match(nextBlocks, event);
@@ -240,7 +239,8 @@ export class BotService {
!matchedBlock &&
event.getMessageType() === IncomingMessageType.message &&
fallbackOptions.active &&
convo.context.attempt < fallbackOptions.max_attempts
convo.context?.attempt &&
convo.context?.attempt < fallbackOptions.max_attempts
) {
// Trigger block fallback
// NOTE : current is not populated, this may cause some anomaly