tt
<p>private async getIntentAIPartialFiller(<br />
userText: string,<br />
highLevelIntent: string,<br />
prescriptions: any[],<br />
configPrefix: string,<br />
masterIntentResponse: MasterIntentClassifierResponse<br />
) {<br />
const startTime = performance.now();</p>
<p> // Extract drug names<br />
const drugResp = this.extractDrugNames(prescriptions, highLevelIntent);<br />
masterIntentResponse.drugList = drugResp?.drugList;</p>
<p> // Process all drug types in one iteration<br />
const drugCategories = ['refillable', 'expeditedPrescriptionEligible', 'transferPrescriptionEligible', 'cancelPrescriptionEligible'];<br />
const categorizedDrugs = this.extractAndProcessDrugsByEligibility(prescriptions, drugCategories);</p>
<p> masterIntentResponse.refillEligibleDrugs = categorizedDrugs.refillable;<br />
masterIntentResponse.expediteEligibleDrugs = categorizedDrugs.expeditedPrescriptionEligible;<br />
masterIntentResponse.transferEligibleDrugs = categorizedDrugs.transferPrescriptionEligible;<br />
masterIntentResponse.cancelEligibleDrugs = categorizedDrugs.cancelPrescriptionEligible;</p>
<p> // Consolidated logging for better efficiency<br />
this.logger.addFunctionalTags('drugLists', JSON.stringify({<br />
drugList: masterIntentResponse.drugList,<br />
categorizedDrugs,<br />
}));</p>
<p> // Prepare AI request<br />
const intentAIRequest: any = {<br />
additional_data: {<br />
data: JSON.stringify(drugResp),<br />
query: userText,<br />
},<br />
prompt_id: this.configService.get(`client.${configPrefix}.LLMClassifier.promptConfig.partialFiller`),<br />
};</p>
<p> this.logger.addFunctionalTags('getIntentAIPartialFiller', intentAIRequest);</p>
<p> // API call - can be parallelized if needed<br />
const clientName = `${configPrefix}.intentAI`;<br />
const uri = this.configService.get<string>(`client.${clientName}.uri`);<br />
<br />
const intentAIResponse = await this.axiosUtil.getInstance(clientName).post(uri, intentAIRequest);</p>
<p> const duration = performance.now() - startTime;<br />
this.logger.addFunctionalTags('IntentAIPartialFillerTimeTaken', `${duration}ms`);<br />
this.logger.addFunctionalTags('IntentAIPartialFillerResponse', intentAIResponse?.chat_response);</p>
<p> return intentAIResponse;<br />
}</p>
<p>// **Optimized method to extract & process drugs in a single iteration**<br />
private extractAndProcessDrugsByEligibility(<br />
prescriptions: any[],<br />
criteria: string[]<br />
): Record<string, string[]> {<br />
if (!prescriptions || !Array.isArray(prescriptions)) return {};</p>
<p> return prescriptions.reduce((acc, prescription) => {<br />
criteria.forEach((criterion) => {<br />
if (prescription[criterion]) {<br />
if (!acc[criterion]) acc[criterion] = [];<br />
let processedDrug = this.processDrugName(prescription.drugName);<br />
acc[criterion].push(processedDrug);<br />
}<br />
});<br />
return acc;<br />
}, {} as Record<string, string[]>);<br />
}</p>
<p>// **Process a single drug name (abbreviation & capitalization in one function)**<br />
private processDrugName(drugName: string): string {<br />
if (!drugName) return '';<br />
const replacements = this.configService.get('replacements');</p>
<p> return drugName<br />
.split(' ')<br />
.map((word) => {<br />
const lowerCaseWord = word.toLowerCase();<br />
return replacements[lowerCaseWord] ? replacements[lowerCaseWord] : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();<br />
})<br />
.join(' ');<br />
}</p>
<p>// **Extract drug names while processing them efficiently**<br />
private extractDrugNames(prescriptions: any[], intent: string) {<br />
const vaccineList = this.configService.get('vaccineList');</p>
<p> if (!prescriptions || !Array.isArray(prescriptions)) return { intent, drugList: [], vaccineList };</p>
<p> const drugList = prescriptions<br />
.filter(prescription => prescription.drugName)<br />
.map(prescription => this.processDrugName(prescription.drugName));</p>
<p> return { intent, drugList, vaccineList };<br />
}<br />
</p>