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 { 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<ContextVar> {
constructor(readonly repository: ContextVarRepository) {
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 { 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<string, ContextVar>,
);
const contextVars =
await this.contextVarService.getContextVarsByBlock(next);
// Capture user entry in context vars
if (captureVars && next.capture_vars && next.capture_vars.length > 0) {