From 92003c471eaa7061de2f0ebc186e16a4ccfce280 Mon Sep 17 00:00:00 2001 From: medtaher Date: Mon, 23 Sep 2024 14:54:16 +0100 Subject: [PATCH] refactor: move getContextVarsByBlock to ContextVarService --- api/src/chat/services/context-var.service.ts | 14 ++++++++++++++ api/src/chat/services/conversation.service.ts | 14 ++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/api/src/chat/services/context-var.service.ts b/api/src/chat/services/context-var.service.ts index a347bb9..00578af 100644 --- a/api/src/chat/services/context-var.service.ts +++ b/api/src/chat/services/context-var.service.ts @@ -12,6 +12,7 @@ import { Injectable } from '@nestjs/common'; import { BaseService } from '@/utils/generics/base-service'; import { ContextVarRepository } from '../repositories/context-var.repository'; +import { Block, BlockFull } from '../schemas/block.schema'; import { ContextVar } from '../schemas/context-var.schema'; @Injectable() @@ -19,4 +20,17 @@ export class ContextVarService extends BaseService { constructor(readonly repository: ContextVarRepository) { super(repository); } + + async getContextVarsByBlock( + block: Block | BlockFull, + ): Promise> { + return ( + await this.find({ + name: { $in: block.capture_vars.map((cv) => cv.context_var) }, + }) + ).reduce((acc, cv) => { + acc[cv.name] = cv; + return acc; + }, {}); + } } diff --git a/api/src/chat/services/conversation.service.ts b/api/src/chat/services/conversation.service.ts index 319d31e..9fde8b2 100644 --- a/api/src/chat/services/conversation.service.ts +++ b/api/src/chat/services/conversation.service.ts @@ -19,7 +19,6 @@ import { SubscriberService } from './subscriber.service'; import { VIEW_MORE_PAYLOAD } from '../helpers/constants'; import { ConversationRepository } from '../repositories/conversation.repository'; import { Block, BlockFull } from '../schemas/block.schema'; -import { ContextVar } from '../schemas/context-var.schema'; import { Conversation, ConversationFull, @@ -92,17 +91,8 @@ export class ConversationService extends BaseService< convo.context.nlp = event.getNLP(); convo.context.vars = convo.context.vars || {}; - const contextVars = ( - await this.contextVarService.find({ - name: { $in: next.capture_vars.map((c) => c.context_var) }, - }) - ).reduce( - (acc, cv) => { - acc[cv.name] = cv; - return acc; - }, - {} as Record, - ); + const contextVars = + await this.contextVarService.getContextVarsByBlock(next); // Capture user entry in context vars if (captureVars && next.capture_vars && next.capture_vars.length > 0) {