JB123

<pre> import { HttpException, HttpStatus, Injectable, Logger, NotFoundException } from &#39;@nestjs/common&#39;; import { HttpService } from &#39;@nestjs/axios&#39;; import { Model } from &#39;mongoose&#39;; import { InjectModel } from &#39;@nestjs/mongoose&#39;; import { v4 as uuidv4 } from &#39;uuid&#39;; import UtilService from &#39;src/common/utility/common.util&#39;; import { ConfigService } from &#39;@nestjs/config&#39;; import { AxiosRequestConfig } from &#39;axios/index&#39;; import CreateProcessResponse from &#39;./model/create-process-response.model&#39;; import CreateProcessRequest from &#39;./model/create-process-request.model&#39;; import AxiosUtil from &#39;../applib/util/axios.util&#39;; @Injectable() export default class ProcessService { private agentsConfig: any; constructor( private readonly axiosUtil: AxiosUtil, private http: HttpService, private utilService: UtilService, private readonly configService: ConfigService, ) { this.agentsConfig = this.configService.get&lt;any[]&gt;(&#39;Agents&#39;); } // public async createProcess(request: CreateProcessRequest): Promise&lt;CreateProcessResponse&gt; { // let test = &quot;test&quot;; // return {statusCode:&#39;0000&#39; , statusDescription: &#39;Success&#39;, data: request}; // } getAgentConfig(agentName: string): any { return this.agentsConfig.find((agent) =&gt; agent.name === agentName); } storeData(agentName: string, intentName: string, intentData: any): void { const agentConfig = this.getAgentConfig(agentName); const intent = intentName; // TODO Capture the user responses to Intent based } async callExperienceApi(agentName: string, intentName: string, headers: any, processedData: any): Promise&lt;any&gt; { const agentConfig = this.getAgentConfig(agentName); const intentConfig = agentConfig.Intents[intentName]; const apiUrl = intentConfig.serviceUrl; const intentHeaders = intentConfig.headers || {}; headers = { ...intentHeaders, ...headers }; const headersToExclude = [&#39;content-length&#39;, &#39;host&#39;]; for (const header of headersToExclude) { delete headers[header]; } let payload = { ...intentConfig.request }; const replacePlaceholders = (obj: any, parent: any = null, keyInParent: any = null, actionData: any = null) =&gt; { for (const key in obj) { if (obj.hasOwnProperty(key)) { const value = obj[key]; if (key === &#39;fillMethod&#39;) continue; if (key === &#39;items&#39;) { // Extract intent from placeholder and get actionDataArray const intent = extractIntent(value[0]); const actionDataArray = getActionData(intent, intentName); // Check the fillMethod of the parent object const fillMethod = obj.fillMethod || &#39;latest&#39;; if (fillMethod === &#39;all&#39;) { parent[keyInParent] = actionDataArray.map((actionData) =&gt; { let newItem = JSON.parse(JSON.stringify(value[0])); replacePlaceholders(newItem, null, null, actionData); return newItem; }); } else { const latestActionData = getLatestActionData(intent, intentName); const newItem = JSON.parse(JSON.stringify(value[0])); replacePlaceholders(newItem, null, null, latestActionData); parent[keyInParent] = [newItem]; } continue; } if (typeof value === &#39;string&#39; &amp;&amp; value.startsWith(&#39;$&#39;)) { const [intent, field] = value.substr(1).split(&#39;.&#39;); obj[key] = actionData ? actionData[field] || null : getLatestActionData(intent, intentName)[field] || null; } else if (Array.isArray(value) || typeof value === &#39;object&#39;) { replacePlaceholders(value, obj, key, actionData); } } } }; const extractIntent = (item: any) =&gt; { for (const key in item) { if (typeof item[key] === &quot;string&quot; &amp;&amp; item[key].startsWith(&quot;$&quot;)) { return item[key].substr(1).split(&quot;.&quot;)[0]; } else if (Array.isArray(item[key]) || typeof item[key] === &quot;object&quot;) { return extractIntent(item[key]); } } }; const getActionData = (intent: string,intentName: string) =&gt; { return processedData[intentName]?.userActions[intent] || processedData[intent]?.userActions[intent] || []; }; const getLatestActionData = (intent: string, intentName: string) =&gt; { const actionsData = getActionData(intent, intentName); if (actionsData.length === 0) { return {}; } return actionsData.reduce( (prev, current) =&gt; (parseInt(prev.seqNum) &gt; parseInt(current.seqNum) ? prev : current), {}, ); }; // Replace Placeholders replacePlaceholders(payload); new Logger().log(payload); new Logger().log(headers); this.agentsConfig = null; return this.postService(headers, apiUrl, payload); } public async postService(headers: any, URI: string, payload: any): Promise&lt;any&gt; { console.log(&#39;url&#39;, URI); let response = null; try { // response = await this.axiosUtil.getInstance().post(URI, payload, {headers: { // &#39;Content-Type&#39;: &#39;application/json&#39;, // &#39;x-experienceId&#39;:&#39;03d41404-62e6-448c-a41a-c6a872810702&#39;, // &#39;token&#39;:&#39;49740c6678e94bcd816f564d57202d1211866605276253&#39; // }}); response = await this.axiosUtil.getInstance().post(URI, payload, { headers }); return response; } catch (error) { console.error(&#39;Error&#39;, error.response?.data, error.message); new Logger().log(error.message); throw error; } return response; } } </pre>