improved defensive coding and added proper dates to Notes

This commit is contained in:
Peter Morton 2025-02-03 21:36:12 -06:00
parent e50064a781
commit 74f6c2d471

View File

@ -1,27 +1,34 @@
// Test update
(async () => { (async () => {
try { try {
console.log("Request: [" + JSON.stringify(req.body) + "]") console.log("Request: [" + JSON.stringify(req.body) + "]");
const conversationId = req.body.conversationId;
const caseId = req.body.caseId; const caseId = req.body.caseId;
const conversationId = req.body.conversationId;
const externalSettings = req.body.settings; const externalSettings = req.body.settings;
const externalModelName = req.body.modelName; const externalModelName = req.body.modelName;
const modelName = externalModelName ? externalModelName : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.modelName; const modelName = externalModelName ? externalModelName : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.modelName;
// with Email to Case - the 1st/recent note is the body of the email // with Email to Case - the 1st/recent note is the body of the email
var caseInstance = await CACaseInterface().getCase(caseId); var caseInstance = await CACaseInterface().getCase(caseId);
console.log(`Case Note: [" + ${caseInstance.recentNotes[0].content}]`); var inboundEmailBody = ""
if (caseInstance?.recentNotes?.length === 0)
console.log(`No body found in case notes`);
else
inboundEmailBody = caseInstance.recentNotes[0].content;
const modelResponse = await axios({ const modelResponse = await axios({
url: `${inferenceSettings_64f77978cbc0b66fc62838e4.nlu.apiBaseURL}Model/run/${req.params.workspaceId}/${req.params.branch}/${modelName}`, url: `${inferenceSettings_64f77978cbc0b66fc62838e4.nlu.apiBaseURL}Model/run/${req.params.workspaceId}/${req.params.branch}/${modelName}`,
method: "post", method: "post",
data: { data: {
input: caseInstance.recentNotes[0].content, input: inboundEmailBody,
conversationId: conversationId ? conversationId : undefined, conversationId: conversationId ? conversationId : undefined,
settings: externalSettings ? externalSettings : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.settings settings: externalSettings ? externalSettings : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.settings
} }
}); });
console.log("modelResponse.data: [" + JSON.stringify(modelResponse.data) + "]")
await redis.set(`${req.body?.conversationId}`, `${modelResponse?.data?.conversationId}`, { await redis.set(`${req.body?.conversationId}`, `${modelResponse?.data?.conversationId}`, {
EX: 3600 EX: 3600
}); });
@ -60,8 +67,8 @@
// Create the ACK against the Notifying Case // Create the ACK against the Notifying Case
var notes = [ var notes = [
{ {
"createdBy": "IVA Studio conversationId:" + conversationId, "createdBy": "IVA conversationId:" + modelResponse?.data?.conversationId,
"createdOn": "2022-06-21T13:38:12Z", // TODO: create timestamp "createdOn": `${(new Date().toISOString())}`,
"content": "IVA Acknowledging receipt of email" "content": "IVA Acknowledging receipt of email"
} }
@ -76,20 +83,31 @@
} }
]; ];
var body = response.answers[0];
var body;
if (response.answers?.length === 0) {
body = "Answer not available";
} else {
body = "";
response.answers.forEach(answer => {
body += "\n" + answer;
})
}
var entities = { var entities = {
"InboundEmail": { "InboundEmail": {
"@type": "vde:InboundEmail", "@type": "vde:InboundEmail",
"subject": "TODO: get this from the case", "subject": req.body.subject,
"body": caseInstance.recentNotes[0].content "body": caseInstance.recentNotes[0].content
}, },
"EmailResponse": { "EmailResponse": {
"@type": "vde:EmailResponse", "@type": "vde:EmailResponse",
"subject": "LIMITATION: CA Case Send Email does not allow setting of subject by dynamic entities", "subject": "RE:" + req.body.subject,
"body": body "bodyRichText": body,
"conversationId" : `${modelResponse?.data?.conversationId}`
} }
}; };
// TODO: Replace Hardcoded Customer Reference when search API is working again
var createCase = await CACaseInterface().createCase("vcs:IVAEmailAutoReply", "CC1000050", "IVA requesting send of email", notes, entities); var createCase = await CACaseInterface().createCase("vcs:IVAEmailAutoReply", "CC1000050", "IVA requesting send of email", notes, entities);
response.caseData = createCase; response.caseData = createCase;