@app-connect/core 1.7.25 → 1.7.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/.env.test +5 -5
  2. package/README.md +441 -441
  3. package/connector/developerPortal.js +31 -31
  4. package/connector/mock.js +84 -77
  5. package/connector/proxy/engine.js +164 -164
  6. package/connector/proxy/index.js +500 -500
  7. package/connector/registry.js +252 -252
  8. package/docs/README.md +50 -50
  9. package/docs/architecture.md +93 -93
  10. package/docs/connectors.md +116 -116
  11. package/docs/handlers.md +125 -125
  12. package/docs/libraries.md +101 -101
  13. package/docs/models.md +144 -144
  14. package/docs/routes.md +115 -115
  15. package/docs/tests.md +73 -73
  16. package/handlers/admin.js +523 -523
  17. package/handlers/appointment.js +193 -0
  18. package/handlers/auth.js +320 -296
  19. package/handlers/calldown.js +99 -99
  20. package/handlers/contact.js +280 -280
  21. package/handlers/disposition.js +82 -80
  22. package/handlers/log.js +990 -973
  23. package/handlers/managedAuth.js +446 -446
  24. package/handlers/managedOAuth.js +247 -0
  25. package/handlers/plugin.js +208 -208
  26. package/handlers/user.js +142 -142
  27. package/index.js +3162 -2652
  28. package/jest.config.js +56 -56
  29. package/lib/analytics.js +54 -54
  30. package/lib/authSession.js +109 -109
  31. package/lib/cacheCleanup.js +21 -0
  32. package/lib/callLogComposer.js +898 -898
  33. package/lib/callLogLookup.js +34 -0
  34. package/lib/constants.js +8 -8
  35. package/lib/debugTracer.js +177 -177
  36. package/lib/encode.js +30 -30
  37. package/lib/errorHandler.js +218 -206
  38. package/lib/generalErrorMessage.js +41 -41
  39. package/lib/jwt.js +18 -18
  40. package/lib/logger.js +190 -190
  41. package/lib/migrateCallLogsSchema.js +116 -0
  42. package/lib/ringcentral.js +266 -266
  43. package/lib/s3ErrorLogReport.js +65 -65
  44. package/lib/sharedSMSComposer.js +471 -471
  45. package/lib/util.js +67 -67
  46. package/mcp/README.md +412 -395
  47. package/mcp/lib/validator.js +91 -91
  48. package/mcp/mcpHandler.js +425 -425
  49. package/mcp/tools/cancelAppointment.js +101 -0
  50. package/mcp/tools/checkAuthStatus.js +105 -105
  51. package/mcp/tools/confirmAppointment.js +101 -0
  52. package/mcp/tools/createAppointment.js +157 -0
  53. package/mcp/tools/createCallLog.js +327 -316
  54. package/mcp/tools/createContact.js +117 -117
  55. package/mcp/tools/createMessageLog.js +287 -287
  56. package/mcp/tools/doAuth.js +60 -60
  57. package/mcp/tools/findContactByName.js +93 -93
  58. package/mcp/tools/findContactByPhone.js +101 -101
  59. package/mcp/tools/getCallLog.js +111 -102
  60. package/mcp/tools/getGoogleFilePicker.js +99 -99
  61. package/mcp/tools/getHelp.js +43 -43
  62. package/mcp/tools/getPublicConnectors.js +94 -94
  63. package/mcp/tools/getSessionInfo.js +90 -90
  64. package/mcp/tools/index.js +51 -41
  65. package/mcp/tools/listAppointments.js +163 -0
  66. package/mcp/tools/logout.js +99 -96
  67. package/mcp/tools/rcGetCallLogs.js +65 -65
  68. package/mcp/tools/updateAppointment.js +154 -0
  69. package/mcp/tools/updateCallLog.js +130 -126
  70. package/mcp/ui/App/App.tsx +358 -358
  71. package/mcp/ui/App/components/AuthInfoForm.tsx +113 -113
  72. package/mcp/ui/App/components/AuthSuccess.tsx +22 -22
  73. package/mcp/ui/App/components/ConnectorList.tsx +82 -82
  74. package/mcp/ui/App/components/DebugPanel.tsx +43 -43
  75. package/mcp/ui/App/components/OAuthConnect.tsx +270 -270
  76. package/mcp/ui/App/lib/callTool.ts +130 -130
  77. package/mcp/ui/App/lib/debugLog.ts +41 -41
  78. package/mcp/ui/App/lib/developerPortal.ts +111 -111
  79. package/mcp/ui/App/main.css +5 -5
  80. package/mcp/ui/App/root.tsx +13 -13
  81. package/mcp/ui/index.html +13 -13
  82. package/mcp/ui/package-lock.json +6356 -6356
  83. package/mcp/ui/package.json +25 -25
  84. package/mcp/ui/tsconfig.json +26 -26
  85. package/mcp/ui/vite.config.ts +16 -16
  86. package/models/accountDataModel.js +33 -33
  87. package/models/adminConfigModel.js +35 -35
  88. package/models/cacheModel.js +30 -26
  89. package/models/callDownListModel.js +34 -34
  90. package/models/callLogModel.js +33 -27
  91. package/models/dynamo/connectorSchema.js +146 -146
  92. package/models/dynamo/lockSchema.js +24 -24
  93. package/models/dynamo/noteCacheSchema.js +29 -29
  94. package/models/llmSessionModel.js +17 -17
  95. package/models/messageLogModel.js +25 -25
  96. package/models/sequelize.js +16 -16
  97. package/models/userModel.js +45 -45
  98. package/package.json +1 -1
  99. package/releaseNotes.json +1101 -1081
  100. package/test/connector/proxy/engine.test.js +126 -126
  101. package/test/connector/proxy/index.test.js +279 -279
  102. package/test/connector/proxy/sample.json +161 -161
  103. package/test/connector/registry.test.js +415 -415
  104. package/test/handlers/admin.test.js +616 -616
  105. package/test/handlers/auth.test.js +1092 -1018
  106. package/test/handlers/contact.test.js +1014 -1014
  107. package/test/handlers/log.test.js +1298 -1160
  108. package/test/handlers/managedAuth.test.js +457 -457
  109. package/test/handlers/plugin.test.js +380 -380
  110. package/test/index.test.js +105 -105
  111. package/test/lib/cacheCleanup.test.js +42 -0
  112. package/test/lib/callLogComposer.test.js +1231 -1231
  113. package/test/lib/debugTracer.test.js +328 -328
  114. package/test/lib/jwt.test.js +176 -176
  115. package/test/lib/logger.test.js +206 -206
  116. package/test/lib/oauth.test.js +359 -359
  117. package/test/lib/ringcentral.test.js +467 -467
  118. package/test/lib/sharedSMSComposer.test.js +1084 -1084
  119. package/test/lib/util.test.js +329 -329
  120. package/test/mcp/tools/checkAuthStatus.test.js +83 -83
  121. package/test/mcp/tools/createCallLog.test.js +436 -436
  122. package/test/mcp/tools/createContact.test.js +58 -58
  123. package/test/mcp/tools/createMessageLog.test.js +595 -595
  124. package/test/mcp/tools/doAuth.test.js +113 -113
  125. package/test/mcp/tools/findContactByName.test.js +275 -275
  126. package/test/mcp/tools/findContactByPhone.test.js +296 -296
  127. package/test/mcp/tools/getCallLog.test.js +298 -298
  128. package/test/mcp/tools/getGoogleFilePicker.test.js +281 -281
  129. package/test/mcp/tools/getPublicConnectors.test.js +107 -107
  130. package/test/mcp/tools/getSessionInfo.test.js +127 -127
  131. package/test/mcp/tools/logout.test.js +233 -233
  132. package/test/mcp/tools/rcGetCallLogs.test.js +56 -56
  133. package/test/mcp/tools/updateCallLog.test.js +360 -360
  134. package/test/models/accountDataModel.test.js +98 -98
  135. package/test/models/dynamo/connectorSchema.test.js +189 -189
  136. package/test/models/models.test.js +568 -539
  137. package/test/routes/managedAuthRoutes.test.js +235 -104
  138. package/test/setup.js +178 -178
@@ -1,898 +1,898 @@
1
- const moment = require('moment-timezone');
2
- const { secondsToHoursMinutesSeconds } = require('./util');
3
- const connectorRegistry = require('../connector/registry');
4
- const { LOG_DETAILS_FORMAT_TYPE } = require('./constants');
5
-
6
- /**
7
- * Centralized call log composition module
8
- * Supports both plain text and HTML formats used across different CRM connectors
9
- */
10
-
11
- /**
12
- * Compose call log details based on user settings and format type
13
- * @param {Object} params - Composition parameters
14
- * @param {string} params.logFormat - logFormat type: 'plainText' or 'html'
15
- * @param {string} params.existingBody - Existing log body (for updates)
16
- * @param {Object} params.callLog - Call log information
17
- * @param {Object} params.contactInfo - Contact information
18
- * @param {Object} params.user - User information
19
- * @param {string} params.note - User note
20
- * @param {string} params.aiNote - AI generated note
21
- * @param {string} params.transcript - Call transcript
22
- * @param {string} params.recordingLink - Recording link
23
- * @param {string} params.subject - Call subject
24
- * @param {Date} params.startTime - Call start time
25
- * @param {number} params.duration - Call duration in seconds
26
- * @param {string} params.result - Call result
27
- * @returns {Promise<string>} Composed log body
28
- */
29
- function composeCallLog(params) {
30
- const {
31
- logFormat = LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
32
- existingBody = '',
33
- callLog,
34
- contactInfo,
35
- user,
36
- note,
37
- aiNote,
38
- transcript,
39
- recordingLink,
40
- subject,
41
- startTime,
42
- duration,
43
- result,
44
- ringSenseTranscript,
45
- ringSenseSummary,
46
- ringSenseAIScore,
47
- ringSenseBulletedSummary,
48
- ringSenseLink
49
- } = params;
50
-
51
- let body = existingBody;
52
- const userSettings = user.userSettings || {};
53
- // Determine timezone handling
54
- let resolvedStartTime = startTime || callLog?.startTime;
55
- let timezoneOffset = user.timezoneOffset;
56
- if (resolvedStartTime) {
57
- resolvedStartTime = moment(resolvedStartTime);
58
- }
59
- // Apply upsert functions based on user settings
60
- if (note && (userSettings?.addCallLogNote?.value ?? true)) {
61
- body = upsertCallAgentNote({ body, note, logFormat });
62
- }
63
-
64
- if (callLog?.sessionId && (userSettings?.addCallSessionId?.value ?? false)) {
65
- body = upsertCallSessionId({ body, id: callLog.sessionId, logFormat });
66
- }
67
-
68
- if (userSettings?.addRingCentralUserName?.value) {
69
- const ringcentralUsername = (callLog.direction === 'Inbound' ? callLog?.to?.name : callLog?.from?.name) ?? null;
70
- if (ringcentralUsername) {
71
- body = upsertRingCentralUserName({ body, userName: ringcentralUsername, logFormat });
72
- }
73
- }
74
-
75
- if (userSettings?.addRingCentralNumber?.value ?? false) {
76
- const ringcentralNumber = callLog.direction === 'Inbound' ? callLog?.to?.phoneNumber : callLog?.from?.phoneNumber;
77
- if (ringcentralNumber) {
78
- const ringcentralExtensionNumber = callLog.direction === 'Inbound' ? callLog?.from?.extensionNumber : callLog?.to?.extensionNumber;
79
- body = upsertRingCentralNumberAndExtension({ body, number: ringcentralNumber, extension: ringcentralExtensionNumber ?? '', logFormat });
80
- }
81
- }
82
-
83
- if (subject && (userSettings?.addCallLogSubject?.value ?? true)) {
84
- body = upsertCallSubject({ body, subject, logFormat });
85
- }
86
-
87
- if (contactInfo?.phoneNumber && (userSettings?.addCallLogContactNumber?.value ?? false)) {
88
- body = upsertContactPhoneNumber({
89
- body,
90
- phoneNumber: contactInfo.phoneNumber,
91
- direction: callLog?.direction,
92
- logFormat
93
- });
94
- }
95
-
96
- if (resolvedStartTime && (userSettings?.addCallLogDateTime?.value ?? true)) {
97
- body = upsertCallDateTime({
98
- body,
99
- startTime: resolvedStartTime,
100
- timezoneOffset,
101
- logDateFormat: userSettings?.logDateFormat?.value ?? 'YYYY-MM-DD hh:mm:ss A',
102
- logFormat
103
- });
104
- }
105
-
106
- if (typeof duration !== 'undefined' && (userSettings?.addCallLogDuration?.value ?? true)) {
107
- body = upsertCallDuration({ body, duration, logFormat });
108
- }
109
-
110
- if (result && (userSettings?.addCallLogResult?.value ?? true)) {
111
- body = upsertCallResult({ body, result, logFormat });
112
- }
113
-
114
- if (recordingLink && (userSettings?.addCallLogRecording?.value ?? true)) {
115
- body = upsertCallRecording({ body, recordingLink, logFormat });
116
- }
117
-
118
- if (aiNote && (userSettings?.addCallLogAiNote?.value ?? true)) {
119
- body = upsertAiNote({ body, aiNote, logFormat });
120
- }
121
-
122
- if (transcript && (userSettings?.addCallLogTranscript?.value ?? true)) {
123
- body = upsertTranscript({ body, transcript, logFormat });
124
- }
125
-
126
- if (ringSenseTranscript && (userSettings?.addCallLogRingSenseRecordingTranscript?.value ?? true)) {
127
- body = upsertRingSenseTranscript({ body, transcript: ringSenseTranscript, logFormat });
128
- }
129
-
130
- if (ringSenseSummary && (userSettings?.addCallLogRingSenseRecordingSummary?.value ?? true)) {
131
- body = upsertRingSenseSummary({ body, summary: ringSenseSummary, logFormat });
132
- }
133
-
134
- if (ringSenseAIScore && (userSettings?.addCallLogRingSenseRecordingAIScore?.value ?? true)) {
135
- body = upsertRingSenseAIScore({ body, score: ringSenseAIScore, logFormat });
136
- }
137
-
138
- if (ringSenseBulletedSummary && (userSettings?.addCallLogRingSenseRecordingBulletedSummary?.value ?? true)) {
139
- body = upsertRingSenseBulletedSummary({ body, summary: ringSenseBulletedSummary, logFormat });
140
- }
141
-
142
- if (ringSenseLink && (userSettings?.addCallLogRingSenseRecordingLink?.value ?? true)) {
143
- body = upsertRingSenseLink({ body, link: ringSenseLink, logFormat });
144
- }
145
-
146
- if (callLog?.legs && (userSettings?.addCallLogLegs?.value ?? true)) {
147
- body = upsertLegs({ body, legs: callLog.legs, logFormat });
148
- }
149
-
150
- return body;
151
- }
152
-
153
- /**
154
- * Upsert functions for different log components
155
- */
156
-
157
- function upsertCallAgentNote({ body, note, logFormat }) {
158
- if (!note) return body;
159
-
160
- let noteRegex = null;
161
- switch (logFormat) {
162
- case LOG_DETAILS_FORMAT_TYPE.HTML:
163
- // HTML logFormat with proper Agent notes section handling
164
- noteRegex = RegExp('<b>Agent notes</b>([\\s\\S]+?)Call details</b>');
165
- if (noteRegex.test(body)) {
166
- return body.replace(noteRegex, `<b>Agent notes</b><br>${note}<br><br><b>Call details</b>`);
167
- }
168
- return `<b>Agent notes</b><br>${note}<br><br><b>Call details</b><br>` + body;
169
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
170
- // Markdown logFormat with proper Agent notes section handling
171
- noteRegex = /## Agent notes\n([\s\S]*?)\n## Call details/;
172
- if (noteRegex.test(body)) {
173
- return body.replace(noteRegex, `## Agent notes\n${note}\n\n## Call details`);
174
- }
175
- if (body.startsWith('## Call details')) {
176
- return `## Agent notes\n${note}\n\n` + body;
177
- }
178
- return `## Agent notes\n${note}\n\n## Call details\n` + body;
179
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
180
- // Plain text logFormat - FIXED REGEX for multi-line notes with blank lines
181
- noteRegex = /- (?:Note|Agent notes): ([\s\S]*?)(?=\n- [A-Z][a-zA-Z\s/]*:|\n$|$)/;
182
- if (noteRegex.test(body)) {
183
- return body.replace(noteRegex, `- Note: ${note}`);
184
- }
185
- return `- Note: ${note}\n` + body;
186
- }
187
- }
188
-
189
- function upsertCallSessionId({ body, id, logFormat }) {
190
- if (!id) return body;
191
-
192
- let idRegex = null;
193
- switch (logFormat) {
194
- case LOG_DETAILS_FORMAT_TYPE.HTML:
195
- // More flexible regex that handles both <li> wrapped and unwrapped content
196
- idRegex = /(?:<li>)?<b>Session Id<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
197
- if (idRegex.test(body)) {
198
- return body.replace(idRegex, `<li><b>Session Id</b>: ${id}</li>`);
199
- }
200
- return body + `<li><b>Session Id</b>: ${id}</li>`;
201
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
202
- // Markdown format: **Session Id**: value
203
- idRegex = /\*\*Session Id\*\*: [^\n]*\n*/;
204
- if (idRegex.test(body)) {
205
- return body.replace(idRegex, `**Session Id**: ${id}\n`);
206
- }
207
- return body + `**Session Id**: ${id}\n`;
208
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
209
- // Match Session Id field and any trailing newlines, replace with single newline
210
- idRegex = /- Session Id: [^\n]*\n*/;
211
- if (idRegex.test(body)) {
212
- return body.replace(idRegex, `- Session Id: ${id}\n`);
213
- }
214
- return body + `- Session Id: ${id}\n`;
215
- }
216
- }
217
-
218
- function upsertRingCentralUserName({ body, userName, logFormat }) {
219
- if (!userName) return body;
220
-
221
- let userNameRegex = null;
222
- let match = null;
223
- switch (logFormat) {
224
- case LOG_DETAILS_FORMAT_TYPE.HTML:
225
- userNameRegex = /(?:<li>)?<b>RingCentral user name<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
226
- match = body.match(userNameRegex);
227
- if (match) {
228
- // Only replace if existing value is (pending...)
229
- if (match[1].trim() === '(pending...)') {
230
- return body.replace(userNameRegex, `<li><b>RingCentral user name</b>: ${userName}</li>`);
231
- }
232
- return body;
233
- } else {
234
- return body + `<li><b>RingCentral user name</b>: ${userName}</li>`;
235
- }
236
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
237
- userNameRegex = /\*\*RingCentral user name\*\*: ([^\n]*)\n*/i;
238
- match = body.match(userNameRegex);
239
- if (match) {
240
- // Only replace if existing value is (pending...)
241
- if (match[1].trim() === '(pending...)') {
242
- return body.replace(userNameRegex, `**RingCentral user name**: ${userName}\n`);
243
- }
244
- return body;
245
- } else {
246
- return body + `**RingCentral user name**: ${userName}\n`;
247
- }
248
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
249
- userNameRegex = /- RingCentral user name: ([^\n]*)\n*/;
250
- match = body.match(userNameRegex);
251
- if (match) {
252
- // Only replace if existing value is (pending...)
253
- if (match[1].trim() === '(pending...)') {
254
- return body.replace(userNameRegex, `- RingCentral user name: ${userName}\n`);
255
- }
256
- return body;
257
- } else {
258
- return body + `- RingCentral user name: ${userName}\n`;
259
- }
260
- }
261
- }
262
-
263
- function upsertRingCentralNumberAndExtension({ body, number, extension, logFormat }) {
264
- if (!number && !extension) return body;
265
-
266
- let numberAndExtensionRegex = null;
267
- switch (logFormat) {
268
- case LOG_DETAILS_FORMAT_TYPE.HTML:
269
- numberAndExtensionRegex = /(?:<li>)?<b>RingCentral number and extension<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
270
- if (numberAndExtensionRegex.test(body)) {
271
- return body.replace(numberAndExtensionRegex, `<li><b>RingCentral number and extension</b>: ${number} ${extension}</li>`);
272
- }
273
- return body + `<li><b>RingCentral number and extension</b>: ${number} ${extension}</li>`;
274
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
275
- numberAndExtensionRegex = /\*\*RingCentral number and extension\*\*: [^\n]*\n*/i;
276
- if (numberAndExtensionRegex.test(body)) {
277
- return body.replace(numberAndExtensionRegex, `**RingCentral number and extension**: ${number} ${extension}\n`);
278
- }
279
- return body + `**RingCentral number and extension**: ${number} ${extension}\n`;
280
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
281
- numberAndExtensionRegex = /- RingCentral number and extension: [^\n]*\n*/;
282
- if (numberAndExtensionRegex.test(body)) {
283
- return body.replace(numberAndExtensionRegex, `- RingCentral number and extension: ${number} ${extension}\n`);
284
- }
285
- return body + `- RingCentral number and extension: ${number} ${extension}\n`;
286
- }
287
- }
288
-
289
- function upsertCallSubject({ body, subject, logFormat }) {
290
- if (!subject) return body;
291
-
292
- let subjectRegex = null;
293
- switch (logFormat) {
294
- case LOG_DETAILS_FORMAT_TYPE.HTML:
295
- // More flexible regex that handles both <li> wrapped and unwrapped content
296
- subjectRegex = /(?:<li>)?<b>Summary<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
297
- if (subjectRegex.test(body)) {
298
- return body.replace(subjectRegex, `<li><b>Summary</b>: ${subject}</li>`);
299
- }
300
- return body + `<li><b>Summary</b>: ${subject}</li>`;
301
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
302
- // Markdown format: **Summary**: value
303
- subjectRegex = /\*\*Summary\*\*: [^\n]*\n*/;
304
- if (subjectRegex.test(body)) {
305
- return body.replace(subjectRegex, `**Summary**: ${subject}\n`);
306
- }
307
- return body + `**Summary**: ${subject}\n`;
308
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
309
- // Match Summary field and any trailing newlines, replace with single newline
310
- subjectRegex = /- Summary: [^\n]*\n*/;
311
- if (subjectRegex.test(body)) {
312
- return body.replace(subjectRegex, `- Summary: ${subject}\n`);
313
- }
314
- return body + `- Summary: ${subject}\n`;
315
- }
316
- }
317
-
318
- function upsertContactPhoneNumber({ body, phoneNumber, direction, logFormat }) {
319
- if (!phoneNumber) return body;
320
-
321
- const label = direction === 'Outbound' ? 'Recipient' : 'Caller';
322
- let result = body;
323
-
324
- let phoneNumberRegex = null;
325
- switch (logFormat) {
326
- case LOG_DETAILS_FORMAT_TYPE.HTML:
327
- // More flexible regex that handles both <li> wrapped and unwrapped content
328
- phoneNumberRegex = new RegExp(`(?:<li>)?<b>${label} phone number</b>:\\s*([^<\\n]+)(?:</li>|(?=<|$))`, 'i');
329
- if (phoneNumberRegex.test(result)) {
330
- result = result.replace(phoneNumberRegex, `<li><b>${label} phone number</b>: ${phoneNumber}</li>`);
331
- } else {
332
- result += `<li><b>${label} phone number</b>: ${phoneNumber}</li>`;
333
- }
334
- break;
335
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
336
- // Markdown format: **Contact Number**: value
337
- phoneNumberRegex = /\*\*Contact Number\*\*: [^\n]*\n*/;
338
- if (phoneNumberRegex.test(result)) {
339
- result = result.replace(phoneNumberRegex, `**Contact Number**: ${phoneNumber}\n`);
340
- } else {
341
- result += `**Contact Number**: ${phoneNumber}\n`;
342
- }
343
- break;
344
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
345
- // More flexible regex that handles both with and without newlines
346
- phoneNumberRegex = /- Contact Number: ([^\n-]+)(?=\n-|\n|$)/;
347
- if (phoneNumberRegex.test(result)) {
348
- result = result.replace(phoneNumberRegex, `- Contact Number: ${phoneNumber}\n`);
349
- } else {
350
- result += `- Contact Number: ${phoneNumber}\n`;
351
- }
352
- break;
353
- }
354
- return result;
355
- }
356
-
357
- function upsertCallDateTime({ body, startTime, timezoneOffset, logFormat, logDateFormat }) {
358
- if (!startTime) return body;
359
-
360
- // Simple approach: convert to moment and apply timezone offset
361
- let momentTime = moment(startTime);
362
- if (timezoneOffset) {
363
- // Handle both string offsets ('+05:30') and numeric offsets (330 minutes or 5.5 hours)
364
- if (typeof timezoneOffset === 'string' && timezoneOffset.includes(':')) {
365
- // String logFormat like '+05:30' or '-05:00'
366
- momentTime = momentTime.utcOffset(timezoneOffset);
367
- } else {
368
- // Numeric logFormat (minutes or hours)
369
- momentTime = momentTime.utcOffset(Number(timezoneOffset));
370
- }
371
- }
372
- const formattedDateTime = momentTime.format(logDateFormat || 'YYYY-MM-DD hh:mm:ss A');
373
- let result = body;
374
-
375
- let dateTimeRegex = null;
376
- switch (logFormat) {
377
- case LOG_DETAILS_FORMAT_TYPE.HTML:
378
- // More flexible regex that handles both <li> wrapped and unwrapped content
379
- dateTimeRegex = /(?:<li>)?<b>Date\/time<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
380
- if (dateTimeRegex.test(result)) {
381
- result = result.replace(dateTimeRegex, `<li><b>Date/time</b>: ${formattedDateTime}</li>`);
382
- } else {
383
- result += `<li><b>Date/time</b>: ${formattedDateTime}</li>`;
384
- }
385
- break;
386
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
387
- // Markdown format: **Date/Time**: value
388
- dateTimeRegex = /\*\*Date\/Time\*\*: [^\n]*\n*/;
389
- if (dateTimeRegex.test(result)) {
390
- result = result.replace(dateTimeRegex, `**Date/Time**: ${formattedDateTime}\n`);
391
- } else {
392
- result += `**Date/Time**: ${formattedDateTime}\n`;
393
- }
394
- break;
395
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
396
- // Handle duplicated Date/Time entries and match complete date/time values
397
- dateTimeRegex = /^(- Date\/Time:).*$/m;
398
- if (dateTimeRegex.test(result)) {
399
- result = result.replace(dateTimeRegex, `- Date/Time: ${formattedDateTime}`);
400
- } else {
401
- result += `- Date/Time: ${formattedDateTime}\n`;
402
- }
403
- break;
404
- }
405
- return result;
406
- }
407
-
408
- function upsertCallDuration({ body, duration, logFormat }) {
409
- if (typeof duration === 'undefined') return body;
410
-
411
- const formattedDuration = secondsToHoursMinutesSeconds(duration);
412
- let result = body;
413
- let durationRegex = null;
414
- switch (logFormat) {
415
- case LOG_DETAILS_FORMAT_TYPE.HTML:
416
- // More flexible regex that handles both <li> wrapped and unwrapped content
417
- durationRegex = /(?:<li>)?<b>Duration<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
418
- if (durationRegex.test(result)) {
419
- result = result.replace(durationRegex, `<li><b>Duration</b>: ${formattedDuration}</li>`);
420
- } else {
421
- result += `<li><b>Duration</b>: ${formattedDuration}</li>`;
422
- }
423
- break;
424
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
425
- // Markdown format: **Duration**: value
426
- durationRegex = /\*\*Duration\*\*: [^\n]*\n*/;
427
- if (durationRegex.test(result)) {
428
- result = result.replace(durationRegex, `**Duration**: ${formattedDuration}\n`);
429
- } else {
430
- result += `**Duration**: ${formattedDuration}\n`;
431
- }
432
- break;
433
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
434
- // More flexible regex that handles both with and without newlines
435
- durationRegex = /- Duration: ([^\n-]+)(?=\n-|\n|$)/;
436
- if (durationRegex.test(result)) {
437
- result = result.replace(durationRegex, `- Duration: ${formattedDuration}`);
438
- } else {
439
- result += `- Duration: ${formattedDuration}\n`;
440
- }
441
- break;
442
- }
443
- return result;
444
- }
445
-
446
- function upsertCallResult({ body, result, logFormat }) {
447
- if (!result) return body;
448
-
449
- let bodyResult = body;
450
-
451
- let resultRegex = null;
452
- switch (logFormat) {
453
- case LOG_DETAILS_FORMAT_TYPE.HTML:
454
- // More flexible regex that handles both <li> wrapped and unwrapped content
455
- resultRegex = /(?:<li>)?<b>Result<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
456
- if (resultRegex.test(bodyResult)) {
457
- bodyResult = bodyResult.replace(resultRegex, `<li><b>Result</b>: ${result}</li>`);
458
- } else {
459
- bodyResult += `<li><b>Result</b>: ${result}</li>`;
460
- }
461
- break;
462
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
463
- // Markdown format: **Result**: value
464
- resultRegex = /\*\*Result\*\*: [^\n]*\n*/;
465
- if (resultRegex.test(bodyResult)) {
466
- bodyResult = bodyResult.replace(resultRegex, `**Result**: ${result}\n`);
467
- } else {
468
- bodyResult += `**Result**: ${result}\n`;
469
- }
470
- break;
471
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
472
- // More flexible regex that handles both with and without newlines
473
- resultRegex = /- Result: ([^\n-]+)(?=\n-|\n|$)/;
474
- if (resultRegex.test(bodyResult)) {
475
- bodyResult = bodyResult.replace(resultRegex, `- Result: ${result}`);
476
- } else {
477
- bodyResult += `- Result: ${result}\n`;
478
- }
479
- break;
480
- }
481
- return bodyResult;
482
- }
483
-
484
- function upsertCallRecording({ body, recordingLink, logFormat }) {
485
- if (!recordingLink) return body;
486
-
487
- let result = body;
488
- let recordingLinkRegex = null;
489
-
490
- switch (logFormat) {
491
- case LOG_DETAILS_FORMAT_TYPE.HTML:
492
- // More flexible regex that handles both <li> wrapped and unwrapped content, and existing <a> anchors
493
- recordingLinkRegex = /(?:<li>)?<b>Call recording link<\/b>:\s*(?:<a[^>]*>[^<]*<\/a>|[^<]+)(?:<\/li>|(?=<|$))/i;
494
- if (recordingLinkRegex.test(result)) {
495
- if (recordingLink.startsWith('http')) {
496
- result = result.replace(recordingLinkRegex, `<li><b>Call recording link</b>: <a target="_blank" href="${recordingLink}">open</a></li>`);
497
- } else {
498
- result = result.replace(recordingLinkRegex, `<li><b>Call recording link</b>: (pending...)</li>`);
499
- }
500
- } else {
501
- let text = '';
502
- if (recordingLink.startsWith('http')) {
503
- text = `<li><b>Call recording link</b>: <a target="_blank" href="${recordingLink}">open</a></li>`;
504
- } else {
505
- text = '<li><b>Call recording link</b>: (pending...)</li>';
506
- }
507
- if (result.indexOf('</ul>') === -1) {
508
- result += text;
509
- } else {
510
- result = result.replace('</ul>', `${text}</ul>`);
511
- }
512
- }
513
- break;
514
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
515
- // Markdown format: **Call recording link**: value
516
- recordingLinkRegex = /\*\*Call recording link\*\*: [^\n]*\n*/;
517
- if (recordingLinkRegex.test(result)) {
518
- result = result.replace(recordingLinkRegex, `**Call recording link**: ${recordingLink}\n`);
519
- } else {
520
- result += `**Call recording link**: ${recordingLink}\n`;
521
- }
522
- break;
523
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
524
- // Match recording link field and any trailing content, replace with single newline
525
- recordingLinkRegex = /- Call recording link: [^\n]*\n*/;
526
- if (recordingLinkRegex.test(result)) {
527
- result = result.replace(recordingLinkRegex, `- Call recording link: ${recordingLink}\n`);
528
- } else {
529
- if (result && !result.endsWith('\n')) {
530
- result += '\n';
531
- }
532
- result += `- Call recording link: ${recordingLink}\n`;
533
- }
534
- break;
535
- }
536
- return result;
537
- }
538
-
539
- function upsertAiNote({ body, aiNote, logFormat }) {
540
- if (!aiNote) return body;
541
-
542
- const clearedAiNote = aiNote.replace(/\n+$/, '');
543
- let result = body;
544
- let aiNoteRegex = null;
545
-
546
- switch (logFormat) {
547
- case LOG_DETAILS_FORMAT_TYPE.HTML:
548
- const formattedAiNote = clearedAiNote.replace(/(?:\r\n|\r|\n)/g, '<br>');
549
- aiNoteRegex = /<div><b>AI Note<\/b><br>(.+?)<\/div>/;
550
- if (aiNoteRegex.test(result)) {
551
- result = result.replace(aiNoteRegex, `<div><b>AI Note</b><br>${formattedAiNote}</div>`);
552
- } else {
553
- result += `<div><b>AI Note</b><br>${formattedAiNote}</div><br>`;
554
- }
555
- break;
556
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
557
- // Markdown format: ### AI Note
558
- aiNoteRegex = /### AI Note\n([\s\S]*?)(?=\n### |\n$|$)/;
559
- if (aiNoteRegex.test(result)) {
560
- result = result.replace(aiNoteRegex, `### AI Note\n${clearedAiNote}\n`);
561
- } else {
562
- result += `### AI Note\n${clearedAiNote}\n`;
563
- }
564
- break;
565
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
566
- aiNoteRegex = /- AI Note:([\s\S]*?)--- END/;
567
- if (aiNoteRegex.test(result)) {
568
- result = result.replace(aiNoteRegex, `- AI Note:\n${clearedAiNote}\n--- END`);
569
- } else {
570
- result += `\n- AI Note:\n${clearedAiNote}\n--- END\n`;
571
- }
572
- break;
573
- }
574
- return result;
575
- }
576
-
577
- function upsertTranscript({ body, transcript, logFormat }) {
578
- if (!transcript) return body;
579
-
580
- let result = body;
581
- let transcriptRegex = null;
582
-
583
- switch (logFormat) {
584
- case LOG_DETAILS_FORMAT_TYPE.HTML:
585
- const formattedTranscript = transcript.replace(/(?:\r\n|\r|\n)/g, '<br>');
586
- transcriptRegex = /<div><b>Transcript<\/b><br>(.+?)<\/div>/;
587
- if (transcriptRegex.test(result)) {
588
- result = result.replace(transcriptRegex, `<div><b>Transcript</b><br>${formattedTranscript}</div>`);
589
- } else {
590
- result += `<div><b>Transcript</b><br>${formattedTranscript}</div><br>`;
591
- }
592
- break;
593
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
594
- // Markdown format: ### Transcript
595
- transcriptRegex = /### Transcript\n([\s\S]*?)(?=\n### |\n$|$)/;
596
- if (transcriptRegex.test(result)) {
597
- result = result.replace(transcriptRegex, `### Transcript\n${transcript}\n`);
598
- } else {
599
- result += `### Transcript\n${transcript}\n`;
600
- }
601
- break;
602
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
603
- transcriptRegex = /- Transcript:([\s\S]*?)--- END/;
604
- if (transcriptRegex.test(result)) {
605
- result = result.replace(transcriptRegex, `- Transcript:\n${transcript}\n--- END`);
606
- } else {
607
- result += `\n- Transcript:\n${transcript}\n--- END\n`;
608
- }
609
- break;
610
- }
611
- return result;
612
- }
613
-
614
- function getLegPartyInfo(info) {
615
- let phoneNumber = info.phoneNumber;
616
- let extensionNumber = info.extensionNumber;
617
- let numberInfo = phoneNumber;
618
- if (!phoneNumber && !extensionNumber) {
619
- return '';
620
- }
621
- if (extensionNumber && phoneNumber) {
622
- numberInfo = `${phoneNumber}, ext ${extensionNumber}`;
623
- }
624
- if (phoneNumber && !extensionNumber) {
625
- numberInfo = phoneNumber;
626
- }
627
- if (!phoneNumber && extensionNumber) {
628
- numberInfo = `ext ${extensionNumber}`;
629
- }
630
- if (info.name) {
631
- return `${info.name}, ${numberInfo}`;
632
- }
633
- return numberInfo;
634
- }
635
-
636
- function getLegsJourney(legs) {
637
- return legs.map((leg, index) => {
638
- if (index === 0) {
639
- if (leg.direction === 'Outbound') {
640
- return `Made call from ${getLegPartyInfo(leg.from)}`;
641
- } else {
642
- return `Received call at ${getLegPartyInfo(leg.to)}`;
643
- }
644
- }
645
- if (leg.direction === 'Outbound') {
646
- let party = leg.from;
647
- if (leg.legType === 'PstnToSip') {
648
- party = leg.to;
649
- }
650
- return `Transferred to ${getLegPartyInfo(party)}, duration: ${leg.duration} second${leg.duration > 1 ? 's' : ''}`;
651
- } else {
652
- return `Transferred to ${getLegPartyInfo(leg.to)}, duration: ${leg.duration} second${leg.duration > 1 ? 's' : ''}`;
653
- }
654
- }).join('\n');
655
- }
656
-
657
- function upsertLegs({ body, legs, logFormat }) {
658
- if (!legs || legs.length === 0) return body;
659
-
660
- let result = body;
661
- let legsJourney = getLegsJourney(legs);
662
- let legsRegex = null;
663
-
664
- switch (logFormat) {
665
- case LOG_DETAILS_FORMAT_TYPE.HTML:
666
- legsJourney = legsJourney.replace(/(?:\r\n|\r|\n)/g, '<br>');
667
- legsRegex = /<div><b>Call journey<\/b><br>(.+?)<\/div>/;
668
- if (legsRegex.test(result)) {
669
- result = result.replace(legsRegex, `<div><b>Call journey</b><br>${legsJourney}</div>`);
670
- } else {
671
- result += `<div><b>Call journey</b><br>${legsJourney}</div>`;
672
- }
673
- break;
674
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
675
- legsRegex = /### Call journey\n([\s\S]*?)(?=\n### |\n$|$)/;
676
- if (legsRegex.test(result)) {
677
- result = result.replace(legsRegex, `### Call journey\n${legsJourney}\n`);
678
- } else {
679
- result += `### Call journey\n${legsJourney}\n`;
680
- }
681
- break;
682
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
683
- legsRegex = /- Call journey:([\s\S]*?)--- JOURNEY END/;
684
- if (legsRegex.test(result)) {
685
- result = result.replace(legsRegex, `- Call journey:\n${legsJourney}\n--- JOURNEY END`);
686
- } else {
687
- result += `- Call journey:\n${legsJourney}\n--- JOURNEY END\n`;
688
- }
689
- break;
690
- }
691
-
692
- return result;
693
- }
694
-
695
- function upsertRingSenseTranscript({ body, transcript, logFormat }) {
696
- if (!transcript) return body;
697
-
698
- let result = body;
699
- const clearedTranscript = transcript.replace(/\n+$/, '');
700
- let transcriptRegex = null;
701
-
702
- switch (logFormat) {
703
- case LOG_DETAILS_FORMAT_TYPE.HTML:
704
- const formattedTranscript = clearedTranscript.replace(/(?:\r\n|\r|\n)/g, '<br>');
705
- transcriptRegex = /<div><b>ACE transcript<\/b><br>(.+?)<\/div>/;
706
- if (transcriptRegex.test(result)) {
707
- result = result.replace(transcriptRegex, `<div><b>ACE transcript</b><br>${formattedTranscript}</div>`);
708
- } else {
709
- result += `<div><b>ACE transcript</b><br>${formattedTranscript}</div>`;
710
- }
711
- break;
712
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
713
- transcriptRegex = /### ACE transcript\n([\s\S]*?)(?=\n### |\n$|$)/;
714
- if (transcriptRegex.test(result)) {
715
- result = result.replace(transcriptRegex, `### ACE transcript\n${clearedTranscript}\n`);
716
- } else {
717
- result += `### ACE transcript\n${clearedTranscript}\n`;
718
- }
719
- break;
720
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
721
- transcriptRegex = /- ACE transcript:([\s\S]*?)--- END/;
722
- if (transcriptRegex.test(result)) {
723
- result = result.replace(transcriptRegex, `- ACE transcript:\n${clearedTranscript}\n--- END`);
724
- } else {
725
- result += `\n- ACE transcript:\n${clearedTranscript}\n--- END\n`;
726
- }
727
- break;
728
- }
729
- return result;
730
- }
731
-
732
- function upsertRingSenseSummary({ body, summary, logFormat }) {
733
- if (!summary) return body;
734
-
735
- let result = body;
736
- // remove new line in last line of summary
737
- const clearedSummary = summary.replace(/\n+$/, '');
738
- let summaryRegex = null;
739
-
740
- switch (logFormat) {
741
- case LOG_DETAILS_FORMAT_TYPE.HTML:
742
- summaryRegex = /<div><b>ACE summary<\/b><br>(.+?)<\/div>/;
743
- const formattedSummary = clearedSummary.replace(/(?:\r\n|\r|\n)/g, '<br>');
744
- if (summaryRegex.test(result)) {
745
- result = result.replace(summaryRegex, `<div><b>ACE summary</b><br>${formattedSummary}</div>`);
746
- } else {
747
- result += `<div><b>ACE summary</b><br>${formattedSummary}</div>`;
748
- }
749
- break;
750
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
751
- summaryRegex = /### ACE summary\n([\s\S]*?)(?=\n### |\n$|$)/;
752
- if (summaryRegex.test(result)) {
753
- result = result.replace(summaryRegex, `### ACE summary\n${summary}\n`);
754
- } else {
755
- result += `### ACE summary\n${summary}\n`;
756
- }
757
- break;
758
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
759
- summaryRegex = /- ACE summary:([\s\S]*?)--- END/;
760
- if (summaryRegex.test(result)) {
761
- result = result.replace(summaryRegex, `- ACE summary:\n${summary}\n--- END`);
762
- } else {
763
- result += `\n- ACE summary:\n${summary}\n--- END\n`;
764
- }
765
- break;
766
- }
767
- return result;
768
- }
769
-
770
- function upsertRingSenseAIScore({ body, score, logFormat }) {
771
- if (!score) return body;
772
-
773
- let result = body;
774
- let scoreRegex = null;
775
-
776
- switch (logFormat) {
777
- case LOG_DETAILS_FORMAT_TYPE.HTML:
778
- scoreRegex = /(?:<li>)?<b>Call score<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
779
- if (scoreRegex.test(result)) {
780
- result = result.replace(scoreRegex, `<li><b>Call score</b>: ${score}</li>`);
781
- } else {
782
- result += `<li><b>Call score</b>: ${score}</li>`;
783
- }
784
- break;
785
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
786
- scoreRegex = /\*\*Call score\*\*: [^\n]*\n*/;
787
- if (scoreRegex.test(result)) {
788
- result = result.replace(scoreRegex, `**Call score**: ${score}\n`);
789
- } else {
790
- result += `**Call score**: ${score}\n`;
791
- }
792
- break;
793
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
794
- scoreRegex = /- Call score:\s*([^<\n]+)(?=\n|$)/i;
795
- if (scoreRegex.test(result)) {
796
- result = result.replace(scoreRegex, `- Call score: ${score}`);
797
- } else {
798
- result += `- Call score: ${score}\n`;
799
- }
800
- break;
801
- }
802
- return result;
803
- }
804
-
805
- function upsertRingSenseBulletedSummary({ body, summary, logFormat }) {
806
- if (!summary) return body;
807
-
808
- let result = body;
809
- const clearedSummary = summary.replace(/\n+$/, '');
810
- let summaryRegex = null;
811
-
812
- switch (logFormat) {
813
- case LOG_DETAILS_FORMAT_TYPE.HTML:
814
- summaryRegex = /<div><b>ACE bulleted summary<\/b><br>(.+?)<\/div>/;
815
- const formattedSummary = clearedSummary.replace(/(?:\r\n|\r|\n)/g, '<br>');
816
- if (summaryRegex.test(result)) {
817
- result = result.replace(summaryRegex, `<div><b>ACE bulleted summary</b><br>${formattedSummary}</div>`);
818
- } else {
819
- result += `<div><b>ACE bulleted summary</b><br>${formattedSummary}</div>`;
820
- }
821
- break;
822
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
823
- summaryRegex = /### ACE bulleted summary\n([\s\S]*?)(?=\n### |\n$|$)/;
824
- if (summaryRegex.test(result)) {
825
- result = result.replace(summaryRegex, `### ACE bulleted summary\n${summary}\n`);
826
- } else {
827
- result += `### ACE bulleted summary\n${summary}\n`;
828
- }
829
- break;
830
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
831
- summaryRegex = /- ACE bulleted summary:\s*([^<\n]+)(?=\n|$)/i;
832
- if (summaryRegex.test(result)) {
833
- result = result.replace(summaryRegex, `- ACE bulleted summary:\n${summary}\n--- END`);
834
- } else {
835
- result += `\n- ACE bulleted summary:\n${summary}\n--- END\n`;
836
- }
837
- break;
838
- }
839
- return result;
840
- }
841
-
842
- function upsertRingSenseLink({ body, link, logFormat }) {
843
- if (!link) return body;
844
-
845
- let result = body;
846
- let linkRegex = null;
847
-
848
- switch (logFormat) {
849
- case LOG_DETAILS_FORMAT_TYPE.HTML:
850
- linkRegex = /(?:<li>)?<b>ACE recording link<\/b>:\s*(?:<a[^>]*>[^<]*<\/a>|[^<]+)(?:<\/li>|(?=<|$))/i;
851
- if (linkRegex.test(result)) {
852
- result = result.replace(linkRegex, `<li><b>ACE recording link</b>: <a target="_blank" href="${link}">open</a></li>`);
853
- } else {
854
- result += `<li><b>ACE recording link</b>: <a target="_blank" href="${link}">open</a></li>`;
855
- }
856
- break;
857
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
858
- linkRegex = /\*\*ACE recording link\*\*:\s*([^<\n]+)(?=\n|$)/i;
859
- if (linkRegex.test(result)) {
860
- result = result.replace(linkRegex, `**ACE recording link**: ${link}\n`);
861
- } else {
862
- result += `**ACE recording link**: ${link}\n`;
863
- }
864
- break;
865
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
866
- linkRegex = /- ACE recording link:\s*([^<\n]+)(?=\n|$)/i;
867
- if (linkRegex.test(result)) {
868
- result = result.replace(linkRegex, `- ACE recording link: ${link}`);
869
- } else {
870
- result += `- ACE recording link: ${link}\n`;
871
- }
872
- break;
873
- }
874
- return result;
875
- }
876
-
877
- module.exports = {
878
- composeCallLog,
879
- // Export individual upsert functions for backward compatibility
880
- upsertCallAgentNote,
881
- upsertCallSessionId,
882
- upsertRingCentralUserName,
883
- upsertRingCentralNumberAndExtension,
884
- upsertCallSubject,
885
- upsertContactPhoneNumber,
886
- upsertCallDateTime,
887
- upsertCallDuration,
888
- upsertCallResult,
889
- upsertCallRecording,
890
- upsertAiNote,
891
- upsertTranscript,
892
- upsertLegs,
893
- upsertRingSenseTranscript,
894
- upsertRingSenseSummary,
895
- upsertRingSenseAIScore,
896
- upsertRingSenseBulletedSummary,
897
- upsertRingSenseLink,
898
- };
1
+ const moment = require('moment-timezone');
2
+ const { secondsToHoursMinutesSeconds } = require('./util');
3
+ const connectorRegistry = require('../connector/registry');
4
+ const { LOG_DETAILS_FORMAT_TYPE } = require('./constants');
5
+
6
+ /**
7
+ * Centralized call log composition module
8
+ * Supports both plain text and HTML formats used across different CRM connectors
9
+ */
10
+
11
+ /**
12
+ * Compose call log details based on user settings and format type
13
+ * @param {Object} params - Composition parameters
14
+ * @param {string} params.logFormat - logFormat type: 'plainText' or 'html'
15
+ * @param {string} params.existingBody - Existing log body (for updates)
16
+ * @param {Object} params.callLog - Call log information
17
+ * @param {Object} params.contactInfo - Contact information
18
+ * @param {Object} params.user - User information
19
+ * @param {string} params.note - User note
20
+ * @param {string} params.aiNote - AI generated note
21
+ * @param {string} params.transcript - Call transcript
22
+ * @param {string} params.recordingLink - Recording link
23
+ * @param {string} params.subject - Call subject
24
+ * @param {Date} params.startTime - Call start time
25
+ * @param {number} params.duration - Call duration in seconds
26
+ * @param {string} params.result - Call result
27
+ * @returns {Promise<string>} Composed log body
28
+ */
29
+ function composeCallLog(params) {
30
+ const {
31
+ logFormat = LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT,
32
+ existingBody = '',
33
+ callLog,
34
+ contactInfo,
35
+ user,
36
+ note,
37
+ aiNote,
38
+ transcript,
39
+ recordingLink,
40
+ subject,
41
+ startTime,
42
+ duration,
43
+ result,
44
+ ringSenseTranscript,
45
+ ringSenseSummary,
46
+ ringSenseAIScore,
47
+ ringSenseBulletedSummary,
48
+ ringSenseLink
49
+ } = params;
50
+
51
+ let body = existingBody;
52
+ const userSettings = user.userSettings || {};
53
+ // Determine timezone handling
54
+ let resolvedStartTime = startTime || callLog?.startTime;
55
+ let timezoneOffset = user.timezoneOffset;
56
+ if (resolvedStartTime) {
57
+ resolvedStartTime = moment(resolvedStartTime);
58
+ }
59
+ // Apply upsert functions based on user settings
60
+ if (note && (userSettings?.addCallLogNote?.value ?? true)) {
61
+ body = upsertCallAgentNote({ body, note, logFormat });
62
+ }
63
+
64
+ if (callLog?.sessionId && (userSettings?.addCallSessionId?.value ?? false)) {
65
+ body = upsertCallSessionId({ body, id: callLog.sessionId, logFormat });
66
+ }
67
+
68
+ if (userSettings?.addRingCentralUserName?.value) {
69
+ const ringcentralUsername = (callLog.direction === 'Inbound' ? callLog?.to?.name : callLog?.from?.name) ?? null;
70
+ if (ringcentralUsername) {
71
+ body = upsertRingCentralUserName({ body, userName: ringcentralUsername, logFormat });
72
+ }
73
+ }
74
+
75
+ if (userSettings?.addRingCentralNumber?.value ?? false) {
76
+ const ringcentralNumber = callLog.direction === 'Inbound' ? callLog?.to?.phoneNumber : callLog?.from?.phoneNumber;
77
+ if (ringcentralNumber) {
78
+ const ringcentralExtensionNumber = callLog.direction === 'Inbound' ? callLog?.from?.extensionNumber : callLog?.to?.extensionNumber;
79
+ body = upsertRingCentralNumberAndExtension({ body, number: ringcentralNumber, extension: ringcentralExtensionNumber ?? '', logFormat });
80
+ }
81
+ }
82
+
83
+ if (subject && (userSettings?.addCallLogSubject?.value ?? true)) {
84
+ body = upsertCallSubject({ body, subject, logFormat });
85
+ }
86
+
87
+ if (contactInfo?.phoneNumber && (userSettings?.addCallLogContactNumber?.value ?? false)) {
88
+ body = upsertContactPhoneNumber({
89
+ body,
90
+ phoneNumber: contactInfo.phoneNumber,
91
+ direction: callLog?.direction,
92
+ logFormat
93
+ });
94
+ }
95
+
96
+ if (resolvedStartTime && (userSettings?.addCallLogDateTime?.value ?? true)) {
97
+ body = upsertCallDateTime({
98
+ body,
99
+ startTime: resolvedStartTime,
100
+ timezoneOffset,
101
+ logDateFormat: userSettings?.logDateFormat?.value ?? 'YYYY-MM-DD hh:mm:ss A',
102
+ logFormat
103
+ });
104
+ }
105
+
106
+ if (typeof duration !== 'undefined' && (userSettings?.addCallLogDuration?.value ?? true)) {
107
+ body = upsertCallDuration({ body, duration, logFormat });
108
+ }
109
+
110
+ if (result && (userSettings?.addCallLogResult?.value ?? true)) {
111
+ body = upsertCallResult({ body, result, logFormat });
112
+ }
113
+
114
+ if (recordingLink && (userSettings?.addCallLogRecording?.value ?? true)) {
115
+ body = upsertCallRecording({ body, recordingLink, logFormat });
116
+ }
117
+
118
+ if (aiNote && (userSettings?.addCallLogAiNote?.value ?? true)) {
119
+ body = upsertAiNote({ body, aiNote, logFormat });
120
+ }
121
+
122
+ if (transcript && (userSettings?.addCallLogTranscript?.value ?? true)) {
123
+ body = upsertTranscript({ body, transcript, logFormat });
124
+ }
125
+
126
+ if (ringSenseTranscript && (userSettings?.addCallLogRingSenseRecordingTranscript?.value ?? true)) {
127
+ body = upsertRingSenseTranscript({ body, transcript: ringSenseTranscript, logFormat });
128
+ }
129
+
130
+ if (ringSenseSummary && (userSettings?.addCallLogRingSenseRecordingSummary?.value ?? true)) {
131
+ body = upsertRingSenseSummary({ body, summary: ringSenseSummary, logFormat });
132
+ }
133
+
134
+ if (ringSenseAIScore && (userSettings?.addCallLogRingSenseRecordingAIScore?.value ?? true)) {
135
+ body = upsertRingSenseAIScore({ body, score: ringSenseAIScore, logFormat });
136
+ }
137
+
138
+ if (ringSenseBulletedSummary && (userSettings?.addCallLogRingSenseRecordingBulletedSummary?.value ?? true)) {
139
+ body = upsertRingSenseBulletedSummary({ body, summary: ringSenseBulletedSummary, logFormat });
140
+ }
141
+
142
+ if (ringSenseLink && (userSettings?.addCallLogRingSenseRecordingLink?.value ?? true)) {
143
+ body = upsertRingSenseLink({ body, link: ringSenseLink, logFormat });
144
+ }
145
+
146
+ if (callLog?.legs && (userSettings?.addCallLogLegs?.value ?? true)) {
147
+ body = upsertLegs({ body, legs: callLog.legs, logFormat });
148
+ }
149
+
150
+ return body;
151
+ }
152
+
153
+ /**
154
+ * Upsert functions for different log components
155
+ */
156
+
157
+ function upsertCallAgentNote({ body, note, logFormat }) {
158
+ if (!note) return body;
159
+
160
+ let noteRegex = null;
161
+ switch (logFormat) {
162
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
163
+ // HTML logFormat with proper Agent notes section handling
164
+ noteRegex = RegExp('<b>Agent notes</b>([\\s\\S]+?)Call details</b>');
165
+ if (noteRegex.test(body)) {
166
+ return body.replace(noteRegex, `<b>Agent notes</b><br>${note}<br><br><b>Call details</b>`);
167
+ }
168
+ return `<b>Agent notes</b><br>${note}<br><br><b>Call details</b><br>` + body;
169
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
170
+ // Markdown logFormat with proper Agent notes section handling
171
+ noteRegex = /## Agent notes\n([\s\S]*?)\n## Call details/;
172
+ if (noteRegex.test(body)) {
173
+ return body.replace(noteRegex, `## Agent notes\n${note}\n\n## Call details`);
174
+ }
175
+ if (body.startsWith('## Call details')) {
176
+ return `## Agent notes\n${note}\n\n` + body;
177
+ }
178
+ return `## Agent notes\n${note}\n\n## Call details\n` + body;
179
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
180
+ // Plain text logFormat - FIXED REGEX for multi-line notes with blank lines
181
+ noteRegex = /- (?:Note|Agent notes): ([\s\S]*?)(?=\n- [A-Z][a-zA-Z\s/]*:|\n$|$)/;
182
+ if (noteRegex.test(body)) {
183
+ return body.replace(noteRegex, `- Note: ${note}`);
184
+ }
185
+ return `- Note: ${note}\n` + body;
186
+ }
187
+ }
188
+
189
+ function upsertCallSessionId({ body, id, logFormat }) {
190
+ if (!id) return body;
191
+
192
+ let idRegex = null;
193
+ switch (logFormat) {
194
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
195
+ // More flexible regex that handles both <li> wrapped and unwrapped content
196
+ idRegex = /(?:<li>)?<b>Session Id<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
197
+ if (idRegex.test(body)) {
198
+ return body.replace(idRegex, `<li><b>Session Id</b>: ${id}</li>`);
199
+ }
200
+ return body + `<li><b>Session Id</b>: ${id}</li>`;
201
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
202
+ // Markdown format: **Session Id**: value
203
+ idRegex = /\*\*Session Id\*\*: [^\n]*\n*/;
204
+ if (idRegex.test(body)) {
205
+ return body.replace(idRegex, `**Session Id**: ${id}\n`);
206
+ }
207
+ return body + `**Session Id**: ${id}\n`;
208
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
209
+ // Match Session Id field and any trailing newlines, replace with single newline
210
+ idRegex = /- Session Id: [^\n]*\n*/;
211
+ if (idRegex.test(body)) {
212
+ return body.replace(idRegex, `- Session Id: ${id}\n`);
213
+ }
214
+ return body + `- Session Id: ${id}\n`;
215
+ }
216
+ }
217
+
218
+ function upsertRingCentralUserName({ body, userName, logFormat }) {
219
+ if (!userName) return body;
220
+
221
+ let userNameRegex = null;
222
+ let match = null;
223
+ switch (logFormat) {
224
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
225
+ userNameRegex = /(?:<li>)?<b>RingCentral user name<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
226
+ match = body.match(userNameRegex);
227
+ if (match) {
228
+ // Only replace if existing value is (pending...)
229
+ if (match[1].trim() === '(pending...)') {
230
+ return body.replace(userNameRegex, `<li><b>RingCentral user name</b>: ${userName}</li>`);
231
+ }
232
+ return body;
233
+ } else {
234
+ return body + `<li><b>RingCentral user name</b>: ${userName}</li>`;
235
+ }
236
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
237
+ userNameRegex = /\*\*RingCentral user name\*\*: ([^\n]*)\n*/i;
238
+ match = body.match(userNameRegex);
239
+ if (match) {
240
+ // Only replace if existing value is (pending...)
241
+ if (match[1].trim() === '(pending...)') {
242
+ return body.replace(userNameRegex, `**RingCentral user name**: ${userName}\n`);
243
+ }
244
+ return body;
245
+ } else {
246
+ return body + `**RingCentral user name**: ${userName}\n`;
247
+ }
248
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
249
+ userNameRegex = /- RingCentral user name: ([^\n]*)\n*/;
250
+ match = body.match(userNameRegex);
251
+ if (match) {
252
+ // Only replace if existing value is (pending...)
253
+ if (match[1].trim() === '(pending...)') {
254
+ return body.replace(userNameRegex, `- RingCentral user name: ${userName}\n`);
255
+ }
256
+ return body;
257
+ } else {
258
+ return body + `- RingCentral user name: ${userName}\n`;
259
+ }
260
+ }
261
+ }
262
+
263
+ function upsertRingCentralNumberAndExtension({ body, number, extension, logFormat }) {
264
+ if (!number && !extension) return body;
265
+
266
+ let numberAndExtensionRegex = null;
267
+ switch (logFormat) {
268
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
269
+ numberAndExtensionRegex = /(?:<li>)?<b>RingCentral number and extension<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
270
+ if (numberAndExtensionRegex.test(body)) {
271
+ return body.replace(numberAndExtensionRegex, `<li><b>RingCentral number and extension</b>: ${number} ${extension}</li>`);
272
+ }
273
+ return body + `<li><b>RingCentral number and extension</b>: ${number} ${extension}</li>`;
274
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
275
+ numberAndExtensionRegex = /\*\*RingCentral number and extension\*\*: [^\n]*\n*/i;
276
+ if (numberAndExtensionRegex.test(body)) {
277
+ return body.replace(numberAndExtensionRegex, `**RingCentral number and extension**: ${number} ${extension}\n`);
278
+ }
279
+ return body + `**RingCentral number and extension**: ${number} ${extension}\n`;
280
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
281
+ numberAndExtensionRegex = /- RingCentral number and extension: [^\n]*\n*/;
282
+ if (numberAndExtensionRegex.test(body)) {
283
+ return body.replace(numberAndExtensionRegex, `- RingCentral number and extension: ${number} ${extension}\n`);
284
+ }
285
+ return body + `- RingCentral number and extension: ${number} ${extension}\n`;
286
+ }
287
+ }
288
+
289
+ function upsertCallSubject({ body, subject, logFormat }) {
290
+ if (!subject) return body;
291
+
292
+ let subjectRegex = null;
293
+ switch (logFormat) {
294
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
295
+ // More flexible regex that handles both <li> wrapped and unwrapped content
296
+ subjectRegex = /(?:<li>)?<b>Summary<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
297
+ if (subjectRegex.test(body)) {
298
+ return body.replace(subjectRegex, `<li><b>Summary</b>: ${subject}</li>`);
299
+ }
300
+ return body + `<li><b>Summary</b>: ${subject}</li>`;
301
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
302
+ // Markdown format: **Summary**: value
303
+ subjectRegex = /\*\*Summary\*\*: [^\n]*\n*/;
304
+ if (subjectRegex.test(body)) {
305
+ return body.replace(subjectRegex, `**Summary**: ${subject}\n`);
306
+ }
307
+ return body + `**Summary**: ${subject}\n`;
308
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
309
+ // Match Summary field and any trailing newlines, replace with single newline
310
+ subjectRegex = /- Summary: [^\n]*\n*/;
311
+ if (subjectRegex.test(body)) {
312
+ return body.replace(subjectRegex, `- Summary: ${subject}\n`);
313
+ }
314
+ return body + `- Summary: ${subject}\n`;
315
+ }
316
+ }
317
+
318
+ function upsertContactPhoneNumber({ body, phoneNumber, direction, logFormat }) {
319
+ if (!phoneNumber) return body;
320
+
321
+ const label = direction === 'Outbound' ? 'Recipient' : 'Caller';
322
+ let result = body;
323
+
324
+ let phoneNumberRegex = null;
325
+ switch (logFormat) {
326
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
327
+ // More flexible regex that handles both <li> wrapped and unwrapped content
328
+ phoneNumberRegex = new RegExp(`(?:<li>)?<b>${label} phone number</b>:\\s*([^<\\n]+)(?:</li>|(?=<|$))`, 'i');
329
+ if (phoneNumberRegex.test(result)) {
330
+ result = result.replace(phoneNumberRegex, `<li><b>${label} phone number</b>: ${phoneNumber}</li>`);
331
+ } else {
332
+ result += `<li><b>${label} phone number</b>: ${phoneNumber}</li>`;
333
+ }
334
+ break;
335
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
336
+ // Markdown format: **Contact Number**: value
337
+ phoneNumberRegex = /\*\*Contact Number\*\*: [^\n]*\n*/;
338
+ if (phoneNumberRegex.test(result)) {
339
+ result = result.replace(phoneNumberRegex, `**Contact Number**: ${phoneNumber}\n`);
340
+ } else {
341
+ result += `**Contact Number**: ${phoneNumber}\n`;
342
+ }
343
+ break;
344
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
345
+ // More flexible regex that handles both with and without newlines
346
+ phoneNumberRegex = /- Contact Number: ([^\n-]+)(?=\n-|\n|$)/;
347
+ if (phoneNumberRegex.test(result)) {
348
+ result = result.replace(phoneNumberRegex, `- Contact Number: ${phoneNumber}\n`);
349
+ } else {
350
+ result += `- Contact Number: ${phoneNumber}\n`;
351
+ }
352
+ break;
353
+ }
354
+ return result;
355
+ }
356
+
357
+ function upsertCallDateTime({ body, startTime, timezoneOffset, logFormat, logDateFormat }) {
358
+ if (!startTime) return body;
359
+
360
+ // Simple approach: convert to moment and apply timezone offset
361
+ let momentTime = moment(startTime);
362
+ if (timezoneOffset) {
363
+ // Handle both string offsets ('+05:30') and numeric offsets (330 minutes or 5.5 hours)
364
+ if (typeof timezoneOffset === 'string' && timezoneOffset.includes(':')) {
365
+ // String logFormat like '+05:30' or '-05:00'
366
+ momentTime = momentTime.utcOffset(timezoneOffset);
367
+ } else {
368
+ // Numeric logFormat (minutes or hours)
369
+ momentTime = momentTime.utcOffset(Number(timezoneOffset));
370
+ }
371
+ }
372
+ const formattedDateTime = momentTime.format(logDateFormat || 'YYYY-MM-DD hh:mm:ss A');
373
+ let result = body;
374
+
375
+ let dateTimeRegex = null;
376
+ switch (logFormat) {
377
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
378
+ // More flexible regex that handles both <li> wrapped and unwrapped content
379
+ dateTimeRegex = /(?:<li>)?<b>Date\/time<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
380
+ if (dateTimeRegex.test(result)) {
381
+ result = result.replace(dateTimeRegex, `<li><b>Date/time</b>: ${formattedDateTime}</li>`);
382
+ } else {
383
+ result += `<li><b>Date/time</b>: ${formattedDateTime}</li>`;
384
+ }
385
+ break;
386
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
387
+ // Markdown format: **Date/Time**: value
388
+ dateTimeRegex = /\*\*Date\/Time\*\*: [^\n]*\n*/;
389
+ if (dateTimeRegex.test(result)) {
390
+ result = result.replace(dateTimeRegex, `**Date/Time**: ${formattedDateTime}\n`);
391
+ } else {
392
+ result += `**Date/Time**: ${formattedDateTime}\n`;
393
+ }
394
+ break;
395
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
396
+ // Handle duplicated Date/Time entries and match complete date/time values
397
+ dateTimeRegex = /^(- Date\/Time:).*$/m;
398
+ if (dateTimeRegex.test(result)) {
399
+ result = result.replace(dateTimeRegex, `- Date/Time: ${formattedDateTime}`);
400
+ } else {
401
+ result += `- Date/Time: ${formattedDateTime}\n`;
402
+ }
403
+ break;
404
+ }
405
+ return result;
406
+ }
407
+
408
+ function upsertCallDuration({ body, duration, logFormat }) {
409
+ if (typeof duration === 'undefined') return body;
410
+
411
+ const formattedDuration = secondsToHoursMinutesSeconds(duration);
412
+ let result = body;
413
+ let durationRegex = null;
414
+ switch (logFormat) {
415
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
416
+ // More flexible regex that handles both <li> wrapped and unwrapped content
417
+ durationRegex = /(?:<li>)?<b>Duration<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
418
+ if (durationRegex.test(result)) {
419
+ result = result.replace(durationRegex, `<li><b>Duration</b>: ${formattedDuration}</li>`);
420
+ } else {
421
+ result += `<li><b>Duration</b>: ${formattedDuration}</li>`;
422
+ }
423
+ break;
424
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
425
+ // Markdown format: **Duration**: value
426
+ durationRegex = /\*\*Duration\*\*: [^\n]*\n*/;
427
+ if (durationRegex.test(result)) {
428
+ result = result.replace(durationRegex, `**Duration**: ${formattedDuration}\n`);
429
+ } else {
430
+ result += `**Duration**: ${formattedDuration}\n`;
431
+ }
432
+ break;
433
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
434
+ // More flexible regex that handles both with and without newlines
435
+ durationRegex = /- Duration: ([^\n-]+)(?=\n-|\n|$)/;
436
+ if (durationRegex.test(result)) {
437
+ result = result.replace(durationRegex, `- Duration: ${formattedDuration}`);
438
+ } else {
439
+ result += `- Duration: ${formattedDuration}\n`;
440
+ }
441
+ break;
442
+ }
443
+ return result;
444
+ }
445
+
446
+ function upsertCallResult({ body, result, logFormat }) {
447
+ if (!result) return body;
448
+
449
+ let bodyResult = body;
450
+
451
+ let resultRegex = null;
452
+ switch (logFormat) {
453
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
454
+ // More flexible regex that handles both <li> wrapped and unwrapped content
455
+ resultRegex = /(?:<li>)?<b>Result<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
456
+ if (resultRegex.test(bodyResult)) {
457
+ bodyResult = bodyResult.replace(resultRegex, `<li><b>Result</b>: ${result}</li>`);
458
+ } else {
459
+ bodyResult += `<li><b>Result</b>: ${result}</li>`;
460
+ }
461
+ break;
462
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
463
+ // Markdown format: **Result**: value
464
+ resultRegex = /\*\*Result\*\*: [^\n]*\n*/;
465
+ if (resultRegex.test(bodyResult)) {
466
+ bodyResult = bodyResult.replace(resultRegex, `**Result**: ${result}\n`);
467
+ } else {
468
+ bodyResult += `**Result**: ${result}\n`;
469
+ }
470
+ break;
471
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
472
+ // More flexible regex that handles both with and without newlines
473
+ resultRegex = /- Result: ([^\n-]+)(?=\n-|\n|$)/;
474
+ if (resultRegex.test(bodyResult)) {
475
+ bodyResult = bodyResult.replace(resultRegex, `- Result: ${result}`);
476
+ } else {
477
+ bodyResult += `- Result: ${result}\n`;
478
+ }
479
+ break;
480
+ }
481
+ return bodyResult;
482
+ }
483
+
484
+ function upsertCallRecording({ body, recordingLink, logFormat }) {
485
+ if (!recordingLink) return body;
486
+
487
+ let result = body;
488
+ let recordingLinkRegex = null;
489
+
490
+ switch (logFormat) {
491
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
492
+ // More flexible regex that handles both <li> wrapped and unwrapped content, and existing <a> anchors
493
+ recordingLinkRegex = /(?:<li>)?<b>Call recording link<\/b>:\s*(?:<a[^>]*>[^<]*<\/a>|[^<]+)(?:<\/li>|(?=<|$))/i;
494
+ if (recordingLinkRegex.test(result)) {
495
+ if (recordingLink.startsWith('http')) {
496
+ result = result.replace(recordingLinkRegex, `<li><b>Call recording link</b>: <a target="_blank" href="${recordingLink}">open</a></li>`);
497
+ } else {
498
+ result = result.replace(recordingLinkRegex, `<li><b>Call recording link</b>: (pending...)</li>`);
499
+ }
500
+ } else {
501
+ let text = '';
502
+ if (recordingLink.startsWith('http')) {
503
+ text = `<li><b>Call recording link</b>: <a target="_blank" href="${recordingLink}">open</a></li>`;
504
+ } else {
505
+ text = '<li><b>Call recording link</b>: (pending...)</li>';
506
+ }
507
+ if (result.indexOf('</ul>') === -1) {
508
+ result += text;
509
+ } else {
510
+ result = result.replace('</ul>', `${text}</ul>`);
511
+ }
512
+ }
513
+ break;
514
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
515
+ // Markdown format: **Call recording link**: value
516
+ recordingLinkRegex = /\*\*Call recording link\*\*: [^\n]*\n*/;
517
+ if (recordingLinkRegex.test(result)) {
518
+ result = result.replace(recordingLinkRegex, `**Call recording link**: ${recordingLink}\n`);
519
+ } else {
520
+ result += `**Call recording link**: ${recordingLink}\n`;
521
+ }
522
+ break;
523
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
524
+ // Match recording link field and any trailing content, replace with single newline
525
+ recordingLinkRegex = /- Call recording link: [^\n]*\n*/;
526
+ if (recordingLinkRegex.test(result)) {
527
+ result = result.replace(recordingLinkRegex, `- Call recording link: ${recordingLink}\n`);
528
+ } else {
529
+ if (result && !result.endsWith('\n')) {
530
+ result += '\n';
531
+ }
532
+ result += `- Call recording link: ${recordingLink}\n`;
533
+ }
534
+ break;
535
+ }
536
+ return result;
537
+ }
538
+
539
+ function upsertAiNote({ body, aiNote, logFormat }) {
540
+ if (!aiNote) return body;
541
+
542
+ const clearedAiNote = aiNote.replace(/\n+$/, '');
543
+ let result = body;
544
+ let aiNoteRegex = null;
545
+
546
+ switch (logFormat) {
547
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
548
+ const formattedAiNote = clearedAiNote.replace(/(?:\r\n|\r|\n)/g, '<br>');
549
+ aiNoteRegex = /<div><b>AI Note<\/b><br>(.+?)<\/div>/;
550
+ if (aiNoteRegex.test(result)) {
551
+ result = result.replace(aiNoteRegex, `<div><b>AI Note</b><br>${formattedAiNote}</div>`);
552
+ } else {
553
+ result += `<div><b>AI Note</b><br>${formattedAiNote}</div><br>`;
554
+ }
555
+ break;
556
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
557
+ // Markdown format: ### AI Note
558
+ aiNoteRegex = /### AI Note\n([\s\S]*?)(?=\n### |\n$|$)/;
559
+ if (aiNoteRegex.test(result)) {
560
+ result = result.replace(aiNoteRegex, `### AI Note\n${clearedAiNote}\n`);
561
+ } else {
562
+ result += `### AI Note\n${clearedAiNote}\n`;
563
+ }
564
+ break;
565
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
566
+ aiNoteRegex = /- AI Note:([\s\S]*?)--- END/;
567
+ if (aiNoteRegex.test(result)) {
568
+ result = result.replace(aiNoteRegex, `- AI Note:\n${clearedAiNote}\n--- END`);
569
+ } else {
570
+ result += `\n- AI Note:\n${clearedAiNote}\n--- END\n`;
571
+ }
572
+ break;
573
+ }
574
+ return result;
575
+ }
576
+
577
+ function upsertTranscript({ body, transcript, logFormat }) {
578
+ if (!transcript) return body;
579
+
580
+ let result = body;
581
+ let transcriptRegex = null;
582
+
583
+ switch (logFormat) {
584
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
585
+ const formattedTranscript = transcript.replace(/(?:\r\n|\r|\n)/g, '<br>');
586
+ transcriptRegex = /<div><b>Transcript<\/b><br>(.+?)<\/div>/;
587
+ if (transcriptRegex.test(result)) {
588
+ result = result.replace(transcriptRegex, `<div><b>Transcript</b><br>${formattedTranscript}</div>`);
589
+ } else {
590
+ result += `<div><b>Transcript</b><br>${formattedTranscript}</div><br>`;
591
+ }
592
+ break;
593
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
594
+ // Markdown format: ### Transcript
595
+ transcriptRegex = /### Transcript\n([\s\S]*?)(?=\n### |\n$|$)/;
596
+ if (transcriptRegex.test(result)) {
597
+ result = result.replace(transcriptRegex, `### Transcript\n${transcript}\n`);
598
+ } else {
599
+ result += `### Transcript\n${transcript}\n`;
600
+ }
601
+ break;
602
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
603
+ transcriptRegex = /- Transcript:([\s\S]*?)--- END/;
604
+ if (transcriptRegex.test(result)) {
605
+ result = result.replace(transcriptRegex, `- Transcript:\n${transcript}\n--- END`);
606
+ } else {
607
+ result += `\n- Transcript:\n${transcript}\n--- END\n`;
608
+ }
609
+ break;
610
+ }
611
+ return result;
612
+ }
613
+
614
+ function getLegPartyInfo(info) {
615
+ let phoneNumber = info.phoneNumber;
616
+ let extensionNumber = info.extensionNumber;
617
+ let numberInfo = phoneNumber;
618
+ if (!phoneNumber && !extensionNumber) {
619
+ return '';
620
+ }
621
+ if (extensionNumber && phoneNumber) {
622
+ numberInfo = `${phoneNumber}, ext ${extensionNumber}`;
623
+ }
624
+ if (phoneNumber && !extensionNumber) {
625
+ numberInfo = phoneNumber;
626
+ }
627
+ if (!phoneNumber && extensionNumber) {
628
+ numberInfo = `ext ${extensionNumber}`;
629
+ }
630
+ if (info.name) {
631
+ return `${info.name}, ${numberInfo}`;
632
+ }
633
+ return numberInfo;
634
+ }
635
+
636
+ function getLegsJourney(legs) {
637
+ return legs.map((leg, index) => {
638
+ if (index === 0) {
639
+ if (leg.direction === 'Outbound') {
640
+ return `Made call from ${getLegPartyInfo(leg.from)}`;
641
+ } else {
642
+ return `Received call at ${getLegPartyInfo(leg.to)}`;
643
+ }
644
+ }
645
+ if (leg.direction === 'Outbound') {
646
+ let party = leg.from;
647
+ if (leg.legType === 'PstnToSip') {
648
+ party = leg.to;
649
+ }
650
+ return `Transferred to ${getLegPartyInfo(party)}, duration: ${leg.duration} second${leg.duration > 1 ? 's' : ''}`;
651
+ } else {
652
+ return `Transferred to ${getLegPartyInfo(leg.to)}, duration: ${leg.duration} second${leg.duration > 1 ? 's' : ''}`;
653
+ }
654
+ }).join('\n');
655
+ }
656
+
657
+ function upsertLegs({ body, legs, logFormat }) {
658
+ if (!legs || legs.length === 0) return body;
659
+
660
+ let result = body;
661
+ let legsJourney = getLegsJourney(legs);
662
+ let legsRegex = null;
663
+
664
+ switch (logFormat) {
665
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
666
+ legsJourney = legsJourney.replace(/(?:\r\n|\r|\n)/g, '<br>');
667
+ legsRegex = /<div><b>Call journey<\/b><br>(.+?)<\/div>/;
668
+ if (legsRegex.test(result)) {
669
+ result = result.replace(legsRegex, `<div><b>Call journey</b><br>${legsJourney}</div>`);
670
+ } else {
671
+ result += `<div><b>Call journey</b><br>${legsJourney}</div>`;
672
+ }
673
+ break;
674
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
675
+ legsRegex = /### Call journey\n([\s\S]*?)(?=\n### |\n$|$)/;
676
+ if (legsRegex.test(result)) {
677
+ result = result.replace(legsRegex, `### Call journey\n${legsJourney}\n`);
678
+ } else {
679
+ result += `### Call journey\n${legsJourney}\n`;
680
+ }
681
+ break;
682
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
683
+ legsRegex = /- Call journey:([\s\S]*?)--- JOURNEY END/;
684
+ if (legsRegex.test(result)) {
685
+ result = result.replace(legsRegex, `- Call journey:\n${legsJourney}\n--- JOURNEY END`);
686
+ } else {
687
+ result += `- Call journey:\n${legsJourney}\n--- JOURNEY END\n`;
688
+ }
689
+ break;
690
+ }
691
+
692
+ return result;
693
+ }
694
+
695
+ function upsertRingSenseTranscript({ body, transcript, logFormat }) {
696
+ if (!transcript) return body;
697
+
698
+ let result = body;
699
+ const clearedTranscript = transcript.replace(/\n+$/, '');
700
+ let transcriptRegex = null;
701
+
702
+ switch (logFormat) {
703
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
704
+ const formattedTranscript = clearedTranscript.replace(/(?:\r\n|\r|\n)/g, '<br>');
705
+ transcriptRegex = /<div><b>ACE transcript<\/b><br>(.+?)<\/div>/;
706
+ if (transcriptRegex.test(result)) {
707
+ result = result.replace(transcriptRegex, `<div><b>ACE transcript</b><br>${formattedTranscript}</div>`);
708
+ } else {
709
+ result += `<div><b>ACE transcript</b><br>${formattedTranscript}</div>`;
710
+ }
711
+ break;
712
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
713
+ transcriptRegex = /### ACE transcript\n([\s\S]*?)(?=\n### |\n$|$)/;
714
+ if (transcriptRegex.test(result)) {
715
+ result = result.replace(transcriptRegex, `### ACE transcript\n${clearedTranscript}\n`);
716
+ } else {
717
+ result += `### ACE transcript\n${clearedTranscript}\n`;
718
+ }
719
+ break;
720
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
721
+ transcriptRegex = /- ACE transcript:([\s\S]*?)--- END/;
722
+ if (transcriptRegex.test(result)) {
723
+ result = result.replace(transcriptRegex, `- ACE transcript:\n${clearedTranscript}\n--- END`);
724
+ } else {
725
+ result += `\n- ACE transcript:\n${clearedTranscript}\n--- END\n`;
726
+ }
727
+ break;
728
+ }
729
+ return result;
730
+ }
731
+
732
+ function upsertRingSenseSummary({ body, summary, logFormat }) {
733
+ if (!summary) return body;
734
+
735
+ let result = body;
736
+ // remove new line in last line of summary
737
+ const clearedSummary = summary.replace(/\n+$/, '');
738
+ let summaryRegex = null;
739
+
740
+ switch (logFormat) {
741
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
742
+ summaryRegex = /<div><b>ACE summary<\/b><br>(.+?)<\/div>/;
743
+ const formattedSummary = clearedSummary.replace(/(?:\r\n|\r|\n)/g, '<br>');
744
+ if (summaryRegex.test(result)) {
745
+ result = result.replace(summaryRegex, `<div><b>ACE summary</b><br>${formattedSummary}</div>`);
746
+ } else {
747
+ result += `<div><b>ACE summary</b><br>${formattedSummary}</div>`;
748
+ }
749
+ break;
750
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
751
+ summaryRegex = /### ACE summary\n([\s\S]*?)(?=\n### |\n$|$)/;
752
+ if (summaryRegex.test(result)) {
753
+ result = result.replace(summaryRegex, `### ACE summary\n${summary}\n`);
754
+ } else {
755
+ result += `### ACE summary\n${summary}\n`;
756
+ }
757
+ break;
758
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
759
+ summaryRegex = /- ACE summary:([\s\S]*?)--- END/;
760
+ if (summaryRegex.test(result)) {
761
+ result = result.replace(summaryRegex, `- ACE summary:\n${summary}\n--- END`);
762
+ } else {
763
+ result += `\n- ACE summary:\n${summary}\n--- END\n`;
764
+ }
765
+ break;
766
+ }
767
+ return result;
768
+ }
769
+
770
+ function upsertRingSenseAIScore({ body, score, logFormat }) {
771
+ if (!score) return body;
772
+
773
+ let result = body;
774
+ let scoreRegex = null;
775
+
776
+ switch (logFormat) {
777
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
778
+ scoreRegex = /(?:<li>)?<b>Call score<\/b>:\s*([^<\n]+)(?:<\/li>|(?=<|$))/i;
779
+ if (scoreRegex.test(result)) {
780
+ result = result.replace(scoreRegex, `<li><b>Call score</b>: ${score}</li>`);
781
+ } else {
782
+ result += `<li><b>Call score</b>: ${score}</li>`;
783
+ }
784
+ break;
785
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
786
+ scoreRegex = /\*\*Call score\*\*: [^\n]*\n*/;
787
+ if (scoreRegex.test(result)) {
788
+ result = result.replace(scoreRegex, `**Call score**: ${score}\n`);
789
+ } else {
790
+ result += `**Call score**: ${score}\n`;
791
+ }
792
+ break;
793
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
794
+ scoreRegex = /- Call score:\s*([^<\n]+)(?=\n|$)/i;
795
+ if (scoreRegex.test(result)) {
796
+ result = result.replace(scoreRegex, `- Call score: ${score}`);
797
+ } else {
798
+ result += `- Call score: ${score}\n`;
799
+ }
800
+ break;
801
+ }
802
+ return result;
803
+ }
804
+
805
+ function upsertRingSenseBulletedSummary({ body, summary, logFormat }) {
806
+ if (!summary) return body;
807
+
808
+ let result = body;
809
+ const clearedSummary = summary.replace(/\n+$/, '');
810
+ let summaryRegex = null;
811
+
812
+ switch (logFormat) {
813
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
814
+ summaryRegex = /<div><b>ACE bulleted summary<\/b><br>(.+?)<\/div>/;
815
+ const formattedSummary = clearedSummary.replace(/(?:\r\n|\r|\n)/g, '<br>');
816
+ if (summaryRegex.test(result)) {
817
+ result = result.replace(summaryRegex, `<div><b>ACE bulleted summary</b><br>${formattedSummary}</div>`);
818
+ } else {
819
+ result += `<div><b>ACE bulleted summary</b><br>${formattedSummary}</div>`;
820
+ }
821
+ break;
822
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
823
+ summaryRegex = /### ACE bulleted summary\n([\s\S]*?)(?=\n### |\n$|$)/;
824
+ if (summaryRegex.test(result)) {
825
+ result = result.replace(summaryRegex, `### ACE bulleted summary\n${summary}\n`);
826
+ } else {
827
+ result += `### ACE bulleted summary\n${summary}\n`;
828
+ }
829
+ break;
830
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
831
+ summaryRegex = /- ACE bulleted summary:\s*([^<\n]+)(?=\n|$)/i;
832
+ if (summaryRegex.test(result)) {
833
+ result = result.replace(summaryRegex, `- ACE bulleted summary:\n${summary}\n--- END`);
834
+ } else {
835
+ result += `\n- ACE bulleted summary:\n${summary}\n--- END\n`;
836
+ }
837
+ break;
838
+ }
839
+ return result;
840
+ }
841
+
842
+ function upsertRingSenseLink({ body, link, logFormat }) {
843
+ if (!link) return body;
844
+
845
+ let result = body;
846
+ let linkRegex = null;
847
+
848
+ switch (logFormat) {
849
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
850
+ linkRegex = /(?:<li>)?<b>ACE recording link<\/b>:\s*(?:<a[^>]*>[^<]*<\/a>|[^<]+)(?:<\/li>|(?=<|$))/i;
851
+ if (linkRegex.test(result)) {
852
+ result = result.replace(linkRegex, `<li><b>ACE recording link</b>: <a target="_blank" href="${link}">open</a></li>`);
853
+ } else {
854
+ result += `<li><b>ACE recording link</b>: <a target="_blank" href="${link}">open</a></li>`;
855
+ }
856
+ break;
857
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
858
+ linkRegex = /\*\*ACE recording link\*\*:\s*([^<\n]+)(?=\n|$)/i;
859
+ if (linkRegex.test(result)) {
860
+ result = result.replace(linkRegex, `**ACE recording link**: ${link}\n`);
861
+ } else {
862
+ result += `**ACE recording link**: ${link}\n`;
863
+ }
864
+ break;
865
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
866
+ linkRegex = /- ACE recording link:\s*([^<\n]+)(?=\n|$)/i;
867
+ if (linkRegex.test(result)) {
868
+ result = result.replace(linkRegex, `- ACE recording link: ${link}`);
869
+ } else {
870
+ result += `- ACE recording link: ${link}\n`;
871
+ }
872
+ break;
873
+ }
874
+ return result;
875
+ }
876
+
877
+ module.exports = {
878
+ composeCallLog,
879
+ // Export individual upsert functions for backward compatibility
880
+ upsertCallAgentNote,
881
+ upsertCallSessionId,
882
+ upsertRingCentralUserName,
883
+ upsertRingCentralNumberAndExtension,
884
+ upsertCallSubject,
885
+ upsertContactPhoneNumber,
886
+ upsertCallDateTime,
887
+ upsertCallDuration,
888
+ upsertCallResult,
889
+ upsertCallRecording,
890
+ upsertAiNote,
891
+ upsertTranscript,
892
+ upsertLegs,
893
+ upsertRingSenseTranscript,
894
+ upsertRingSenseSummary,
895
+ upsertRingSenseAIScore,
896
+ upsertRingSenseBulletedSummary,
897
+ upsertRingSenseLink,
898
+ };