import { Injectable } from '@nestjs/common'; @Injectable() export class FlowVariableService { // Replace {{variableName}} with values from session variables interpolate(template: string, variables: Record): string { return template.replace(/\{\{([\w.]+)\}\}/g, (match, path) => { // Support dot notation: {{bookingResult.appointmentId}} const parts = path.split('.'); let value: any = variables; for (const part of parts) { value = value?.[part]; if (value === undefined) return match; } if (value === null) return match; if (typeof value === 'object') return JSON.stringify(value); return String(value); }); } // Interpolate all string values in an object interpolateObject(obj: Record, variables: Record): Record { const result: Record = {}; for (const [key, value] of Object.entries(obj)) { result[key] = this.interpolate(value, variables); } return result; } // Execute expressions for SetVariableBlock evaluateExpression(expression: string, value: string, variables: Record): any { switch (expression) { case 'extract_id': { // Extract second segment: "doc:{uuid}:{name}" → uuid, "dept:{name}" → name const parts = value.split(':'); return parts.length >= 2 ? parts[1] : value; } case 'extract_datetime': { // Extract datetime from "slot:{doctorId}:{datetime}" → "2026-04-21T14:00:00" const parts = value.split(':'); // Rejoin from index 2 onwards (datetime contains colons: 2026-04-21T14:00:00) return parts.length >= 3 ? parts.slice(2).join(':') : value; } case 'date_tomorrow': { const d = new Date(Date.now() + 86400000); return d.toISOString().split('T')[0]; } case 'date_day_after': { const d = new Date(Date.now() + 2 * 86400000); return d.toISOString().split('T')[0]; } default: return this.interpolate(value, variables); } } }