refactor: move getContextVarsByBlock to ContextVarService

This commit is contained in:
medtaher 2024-09-23 14:54:16 +01:00
parent 6e8a2a3d48
commit 92003c471e
2 changed files with 16 additions and 12 deletions

View File

@ -12,6 +12,7 @@ import { Injectable } from '@nestjs/common';
import { BaseService } from '@/utils/generics/base-service'; import { BaseService } from '@/utils/generics/base-service';
import { ContextVarRepository } from '../repositories/context-var.repository'; import { ContextVarRepository } from '../repositories/context-var.repository';
import { Block, BlockFull } from '../schemas/block.schema';
import { ContextVar } from '../schemas/context-var.schema'; import { ContextVar } from '../schemas/context-var.schema';
@Injectable() @Injectable()
@ -19,4 +20,17 @@ export class ContextVarService extends BaseService<ContextVar> {
constructor(readonly repository: ContextVarRepository) { constructor(readonly repository: ContextVarRepository) {
super(repository); super(repository);
} }
async getContextVarsByBlock(
block: Block | BlockFull,
): Promise<Record<string, ContextVar>> {
return (
await this.find({
name: { $in: block.capture_vars.map((cv) => cv.context_var) },
})
).reduce((acc, cv) => {
acc[cv.name] = cv;
return acc;
}, {});
}
} }

View File

@ -19,7 +19,6 @@ import { SubscriberService } from './subscriber.service';
import { VIEW_MORE_PAYLOAD } from '../helpers/constants'; import { VIEW_MORE_PAYLOAD } from '../helpers/constants';
import { ConversationRepository } from '../repositories/conversation.repository'; import { ConversationRepository } from '../repositories/conversation.repository';
import { Block, BlockFull } from '../schemas/block.schema'; import { Block, BlockFull } from '../schemas/block.schema';
import { ContextVar } from '../schemas/context-var.schema';
import { import {
Conversation, Conversation,
ConversationFull, ConversationFull,
@ -92,17 +91,8 @@ export class ConversationService extends BaseService<
convo.context.nlp = event.getNLP(); convo.context.nlp = event.getNLP();
convo.context.vars = convo.context.vars || {}; convo.context.vars = convo.context.vars || {};
const contextVars = ( const contextVars =
await this.contextVarService.find({ await this.contextVarService.getContextVarsByBlock(next);
name: { $in: next.capture_vars.map((c) => c.context_var) },
})
).reduce(
(acc, cv) => {
acc[cv.name] = cv;
return acc;
},
{} as Record<string, ContextVar>,
);
// Capture user entry in context vars // Capture user entry in context vars
if (captureVars && next.capture_vars && next.capture_vars.length > 0) { if (captureVars && next.capture_vars && next.capture_vars.length > 0) {