@app-connect/core 1.7.25 → 1.7.26

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 (137) 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 +296 -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 +984 -973
  23. package/handlers/managedAuth.js +446 -446
  24. package/handlers/plugin.js +208 -208
  25. package/handlers/user.js +142 -142
  26. package/index.js +3140 -2652
  27. package/jest.config.js +56 -56
  28. package/lib/analytics.js +54 -54
  29. package/lib/authSession.js +109 -109
  30. package/lib/cacheCleanup.js +21 -0
  31. package/lib/callLogComposer.js +898 -898
  32. package/lib/callLogLookup.js +34 -0
  33. package/lib/constants.js +8 -8
  34. package/lib/debugTracer.js +177 -177
  35. package/lib/encode.js +30 -30
  36. package/lib/errorHandler.js +218 -206
  37. package/lib/generalErrorMessage.js +41 -41
  38. package/lib/jwt.js +18 -18
  39. package/lib/logger.js +190 -190
  40. package/lib/migrateCallLogsSchema.js +116 -0
  41. package/lib/ringcentral.js +266 -266
  42. package/lib/s3ErrorLogReport.js +65 -65
  43. package/lib/sharedSMSComposer.js +471 -471
  44. package/lib/util.js +67 -67
  45. package/mcp/README.md +412 -395
  46. package/mcp/lib/validator.js +91 -91
  47. package/mcp/mcpHandler.js +425 -425
  48. package/mcp/tools/cancelAppointment.js +101 -0
  49. package/mcp/tools/checkAuthStatus.js +105 -105
  50. package/mcp/tools/confirmAppointment.js +101 -0
  51. package/mcp/tools/createAppointment.js +157 -0
  52. package/mcp/tools/createCallLog.js +327 -316
  53. package/mcp/tools/createContact.js +117 -117
  54. package/mcp/tools/createMessageLog.js +287 -287
  55. package/mcp/tools/doAuth.js +60 -60
  56. package/mcp/tools/findContactByName.js +93 -93
  57. package/mcp/tools/findContactByPhone.js +101 -101
  58. package/mcp/tools/getCallLog.js +111 -102
  59. package/mcp/tools/getGoogleFilePicker.js +99 -99
  60. package/mcp/tools/getHelp.js +43 -43
  61. package/mcp/tools/getPublicConnectors.js +94 -94
  62. package/mcp/tools/getSessionInfo.js +90 -90
  63. package/mcp/tools/index.js +51 -41
  64. package/mcp/tools/listAppointments.js +163 -0
  65. package/mcp/tools/logout.js +96 -96
  66. package/mcp/tools/rcGetCallLogs.js +65 -65
  67. package/mcp/tools/updateAppointment.js +154 -0
  68. package/mcp/tools/updateCallLog.js +130 -126
  69. package/mcp/ui/App/App.tsx +358 -358
  70. package/mcp/ui/App/components/AuthInfoForm.tsx +113 -113
  71. package/mcp/ui/App/components/AuthSuccess.tsx +22 -22
  72. package/mcp/ui/App/components/ConnectorList.tsx +82 -82
  73. package/mcp/ui/App/components/DebugPanel.tsx +43 -43
  74. package/mcp/ui/App/components/OAuthConnect.tsx +270 -270
  75. package/mcp/ui/App/lib/callTool.ts +130 -130
  76. package/mcp/ui/App/lib/debugLog.ts +41 -41
  77. package/mcp/ui/App/lib/developerPortal.ts +111 -111
  78. package/mcp/ui/App/main.css +5 -5
  79. package/mcp/ui/App/root.tsx +13 -13
  80. package/mcp/ui/index.html +13 -13
  81. package/mcp/ui/package-lock.json +6356 -6356
  82. package/mcp/ui/package.json +25 -25
  83. package/mcp/ui/tsconfig.json +26 -26
  84. package/mcp/ui/vite.config.ts +16 -16
  85. package/models/accountDataModel.js +33 -33
  86. package/models/adminConfigModel.js +35 -35
  87. package/models/cacheModel.js +30 -26
  88. package/models/callDownListModel.js +34 -34
  89. package/models/callLogModel.js +33 -27
  90. package/models/dynamo/connectorSchema.js +146 -146
  91. package/models/dynamo/lockSchema.js +24 -24
  92. package/models/dynamo/noteCacheSchema.js +29 -29
  93. package/models/llmSessionModel.js +17 -17
  94. package/models/messageLogModel.js +25 -25
  95. package/models/sequelize.js +16 -16
  96. package/models/userModel.js +45 -45
  97. package/package.json +1 -1
  98. package/releaseNotes.json +1093 -1081
  99. package/test/connector/proxy/engine.test.js +126 -126
  100. package/test/connector/proxy/index.test.js +279 -279
  101. package/test/connector/proxy/sample.json +161 -161
  102. package/test/connector/registry.test.js +415 -415
  103. package/test/handlers/admin.test.js +616 -616
  104. package/test/handlers/auth.test.js +1018 -1018
  105. package/test/handlers/contact.test.js +1014 -1014
  106. package/test/handlers/log.test.js +1298 -1160
  107. package/test/handlers/managedAuth.test.js +457 -457
  108. package/test/handlers/plugin.test.js +380 -380
  109. package/test/index.test.js +105 -105
  110. package/test/lib/cacheCleanup.test.js +42 -0
  111. package/test/lib/callLogComposer.test.js +1231 -1231
  112. package/test/lib/debugTracer.test.js +328 -328
  113. package/test/lib/jwt.test.js +176 -176
  114. package/test/lib/logger.test.js +206 -206
  115. package/test/lib/oauth.test.js +359 -359
  116. package/test/lib/ringcentral.test.js +467 -467
  117. package/test/lib/sharedSMSComposer.test.js +1084 -1084
  118. package/test/lib/util.test.js +329 -329
  119. package/test/mcp/tools/checkAuthStatus.test.js +83 -83
  120. package/test/mcp/tools/createCallLog.test.js +436 -436
  121. package/test/mcp/tools/createContact.test.js +58 -58
  122. package/test/mcp/tools/createMessageLog.test.js +595 -595
  123. package/test/mcp/tools/doAuth.test.js +113 -113
  124. package/test/mcp/tools/findContactByName.test.js +275 -275
  125. package/test/mcp/tools/findContactByPhone.test.js +296 -296
  126. package/test/mcp/tools/getCallLog.test.js +298 -298
  127. package/test/mcp/tools/getGoogleFilePicker.test.js +281 -281
  128. package/test/mcp/tools/getPublicConnectors.test.js +107 -107
  129. package/test/mcp/tools/getSessionInfo.test.js +127 -127
  130. package/test/mcp/tools/logout.test.js +233 -233
  131. package/test/mcp/tools/rcGetCallLogs.test.js +56 -56
  132. package/test/mcp/tools/updateCallLog.test.js +360 -360
  133. package/test/models/accountDataModel.test.js +98 -98
  134. package/test/models/dynamo/connectorSchema.test.js +189 -189
  135. package/test/models/models.test.js +568 -539
  136. package/test/routes/managedAuthRoutes.test.js +104 -104
  137. package/test/setup.js +178 -178
@@ -1,471 +1,471 @@
1
- const moment = require('moment-timezone');
2
- const { LOG_DETAILS_FORMAT_TYPE } = require('./constants');
3
-
4
- function composeSharedSMSLog({ logFormat = LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT, conversation, contactName, timezoneOffset }) {
5
- const conversationCreatedDate = moment(conversation?.creationTime);
6
- const conversationUpdatedDate = moment(findLatestModifiedTime(conversation.messages));
7
- if (timezoneOffset) {
8
- conversationCreatedDate.utcOffset(timezoneOffset);
9
- conversationUpdatedDate.utcOffset(timezoneOffset);
10
- }
11
-
12
- const subject = composeSubject({
13
- logFormat,
14
- contactName
15
- });
16
-
17
- const body = composeBody({
18
- logFormat,
19
- conversation,
20
- contactName,
21
- conversationCreatedDate,
22
- conversationUpdatedDate,
23
- timezoneOffset,
24
- });
25
-
26
- return { subject, body };
27
- }
28
-
29
- function findLatestModifiedTime(messages) {
30
- let result = 0;
31
- for (const message of messages) {
32
- if (message.lastModifiedTime > result) {
33
- result = message.lastModifiedTime;
34
- }
35
- }
36
- return result;
37
- }
38
-
39
- function composeSubject({ logFormat, contactName }) {
40
- const title = `SMS conversation with ${contactName}`;
41
-
42
- switch (logFormat) {
43
- case LOG_DETAILS_FORMAT_TYPE.HTML:
44
- return title;
45
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
46
- return `**${title}**`;
47
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
48
- default:
49
- return title;
50
- }
51
- }
52
-
53
- function composeBody({
54
- logFormat,
55
- conversation,
56
- contactName,
57
- conversationCreatedDate,
58
- conversationUpdatedDate,
59
- timezoneOffset
60
- }) {
61
- // Gather participants from entities
62
- const agents = gatherAgents(conversation.entities || []);
63
-
64
- // Get owner/call queue info
65
- const ownerInfo = getOwnerInfo(conversation);
66
-
67
- // Count messages and notes
68
- const { messageCount, noteCount } = countEntities(conversation.entities || []);
69
-
70
- // Process entities into formatted entries
71
- const formattedEntries = processEntities({
72
- entities: conversation.entities || [],
73
- timezoneOffset,
74
- logFormat,
75
- contactName
76
- });
77
-
78
- // Build the body based on format
79
- switch (logFormat) {
80
- case LOG_DETAILS_FORMAT_TYPE.HTML:
81
- return composeHTMLBody({
82
- conversationCreatedDate,
83
- conversationUpdatedDate,
84
- contactName,
85
- agents,
86
- ownerInfo,
87
- messageCount,
88
- noteCount,
89
- formattedEntries
90
- });
91
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
92
- return composeMarkdownBody({
93
- conversationCreatedDate,
94
- conversationUpdatedDate,
95
- contactName,
96
- agents,
97
- ownerInfo,
98
- messageCount,
99
- noteCount,
100
- formattedEntries
101
- });
102
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
103
- default:
104
- return composePlainTextBody({
105
- conversationCreatedDate,
106
- conversationUpdatedDate,
107
- contactName,
108
- agents,
109
- ownerInfo,
110
- messageCount,
111
- noteCount,
112
- formattedEntries
113
- });
114
- }
115
- }
116
-
117
- function gatherAgents(entities) {
118
- const participantSet = new Set();
119
-
120
- // Add from entities
121
- if (entities) {
122
- for (const entity of entities) {
123
- if (entity.author?.name) {
124
- participantSet.add(entity.author.name);
125
- }
126
- if (entity.from?.name) {
127
- participantSet.add(entity.from.name);
128
- }
129
- if (entity.initiator?.name) {
130
- participantSet.add(entity.initiator.name);
131
- }
132
- if (entity.assignee?.name) {
133
- participantSet.add(entity.assignee.name);
134
- }
135
- }
136
- }
137
- return Array.from(participantSet);
138
- }
139
-
140
- function getOwnerInfo(conversation) {
141
- if (!conversation.owner) return null;
142
-
143
- const ownerName = conversation.owner.name || '';
144
- const extensionType = conversation.owner.extensionType;
145
-
146
- // Check if it's a call queue (Department type)
147
- if (extensionType === 'Department' || ownerName.toLowerCase().includes('queue')) {
148
- return {
149
- type: 'callQueue',
150
- name: ownerName,
151
- extensionId: conversation.owner.extensionId
152
- };
153
- }
154
-
155
- return {
156
- type: 'user',
157
- name: ownerName,
158
- extensionId: conversation.owner.extensionId
159
- };
160
- }
161
-
162
- function countEntities(entities) {
163
- let messageCount = 0;
164
- let noteCount = 0;
165
-
166
- for (const entity of entities) {
167
- if (entity.recordType === 'AliveMessage') {
168
- messageCount++;
169
- } else if (entity.recordType === 'AliveNote') {
170
- noteCount++;
171
- }
172
- }
173
-
174
- return { messageCount, noteCount };
175
- }
176
-
177
- function processEntities({ entities, timezoneOffset, logFormat, contactName }) {
178
- const processedEntries = [];
179
-
180
- for (const entity of entities) {
181
- const entry = processEntity({
182
- entity,
183
- timezoneOffset,
184
- logFormat,
185
- contactName
186
- });
187
- if (entry) {
188
- processedEntries.push(entry);
189
- }
190
- }
191
-
192
- // Sort by creation time (newest first for display)
193
- processedEntries.sort((a, b) => b.creationTime - a.creationTime);
194
-
195
- return processedEntries;
196
- }
197
-
198
- function processEntity({ entity, timezoneOffset, logFormat, contactName }) {
199
- const creationTime = entity.creationTime;
200
- let momentTime = moment(creationTime);
201
- if (timezoneOffset) {
202
- momentTime = momentTime.utcOffset(timezoneOffset);
203
- }
204
- const formattedTime = momentTime.format('YYYY-MM-DD hh:mm A');
205
-
206
- switch (entity.recordType) {
207
- case 'AliveMessage':
208
- return formatMessage({ entity, contactName, formattedTime, creationTime, logFormat });
209
-
210
- case 'ThreadAssignedHint':
211
- return formatAssignment({ entity, formattedTime, creationTime, logFormat });
212
-
213
- case 'AliveNote':
214
- return formatNote({ entity, formattedTime, creationTime, logFormat });
215
-
216
- case 'ThreadCreatedHint':
217
- // Skip thread created hints - not typically shown in log body
218
- return null;
219
-
220
- default:
221
- return null;
222
- }
223
- }
224
-
225
- function formatMessage({ entity, contactName, formattedTime, creationTime, logFormat }) {
226
- const authorName = entity.author?.name || entity.from?.name;
227
- const isInbound = entity.direction === 'Inbound';
228
- const senderName = isInbound ? contactName : authorName;
229
- const messageText = entity.text || entity.subject || '';
230
-
231
- switch (logFormat) {
232
- case LOG_DETAILS_FORMAT_TYPE.HTML:
233
- return {
234
- type: 'message',
235
- creationTime,
236
- content: `<p><b>${escapeHtml(senderName)}</b> said on ${formattedTime}:<br>${escapeHtml(messageText)}</p>`
237
- };
238
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
239
- return {
240
- type: 'message',
241
- creationTime,
242
- content: `**${senderName}** said on ${formattedTime}:\n${messageText}\n`
243
- };
244
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
245
- default:
246
- return {
247
- type: 'message',
248
- creationTime,
249
- content: `${senderName} said on ${formattedTime}:\n${messageText}\n`
250
- };
251
- }
252
- }
253
-
254
- function formatAssignment({ entity, formattedTime, creationTime, logFormat }) {
255
- const assigneeName = entity.assignee?.name || 'Unknown';
256
-
257
- switch (logFormat) {
258
- case LOG_DETAILS_FORMAT_TYPE.HTML:
259
- return {
260
- type: 'assignment',
261
- creationTime,
262
- content: `<p><i>Conversation assigned to <b>${escapeHtml(assigneeName)}</b></i></p>`
263
- };
264
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
265
- return {
266
- type: 'assignment',
267
- creationTime,
268
- content: `*Conversation assigned to **${assigneeName}***\n`
269
- };
270
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
271
- default:
272
- return {
273
- type: 'assignment',
274
- creationTime,
275
- content: `Conversation assigned to ${assigneeName}\n`
276
- };
277
- }
278
- }
279
-
280
- function formatNote({ entity, formattedTime, creationTime, logFormat }) {
281
- const authorName = entity.author?.name || entity.initiator?.name || 'Unknown';
282
- const noteText = entity.text || entity.body || '';
283
-
284
- switch (logFormat) {
285
- case LOG_DETAILS_FORMAT_TYPE.HTML:
286
- return {
287
- type: 'note',
288
- creationTime,
289
- content: `<p><b>${escapeHtml(authorName)}</b> left a note on ${formattedTime}:<br>${escapeHtml(noteText)}</p>`
290
- };
291
- case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
292
- return {
293
- type: 'note',
294
- creationTime,
295
- content: `**${authorName}** left a note on ${formattedTime}:\n${noteText}\n`
296
- };
297
- case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
298
- default:
299
- return {
300
- type: 'note',
301
- creationTime,
302
- content: `${authorName} left a note on ${formattedTime}:\n${noteText}\n`
303
- };
304
- }
305
- }
306
-
307
- function composePlainTextBody({ conversationCreatedDate, conversationUpdatedDate, contactName, agents, ownerInfo, messageCount, noteCount, formattedEntries }) {
308
- let body = '';
309
-
310
- // Conversation summary header
311
- body += 'Conversation summary\n';
312
- body += `Started: ${conversationCreatedDate.format('dddd, MMMM DD, YYYY')} at ${conversationCreatedDate.format('hh:mm A')}\n`;
313
- body += `Ended: ${conversationUpdatedDate ? conversationUpdatedDate.format('dddd, MMMM DD, YYYY') : 'On-going'} at ${conversationUpdatedDate ? conversationUpdatedDate.format('hh:mm A') : 'On-going'}\n`;
314
- body += `Duration: ${conversationUpdatedDate ? `${conversationUpdatedDate.diff(conversationCreatedDate, 'days')} d ${conversationUpdatedDate.diff(conversationCreatedDate, 'hours')} h` : 'On-going'} \n`;
315
- body += '\n';
316
- // Participants
317
- body += 'Participants\n';
318
- body += `* ${contactName} (customer)\n`;
319
- for (const agent of agents) {
320
- body += `* ${agent}\n`;
321
- }
322
- body += '\n';
323
-
324
- // Owner/Call queue info
325
- if (ownerInfo) {
326
- if (ownerInfo.type === 'callQueue') {
327
- body += `Receiving call queue: ${ownerInfo.name}\n\n`;
328
- } else {
329
- body += `Owner: ${ownerInfo.name}\n\n`;
330
- }
331
- }
332
-
333
- // Conversation count
334
- const countParts = [];
335
- if (messageCount > 0) {
336
- countParts.push(`${messageCount} message${messageCount !== 1 ? 's' : ''}`);
337
- }
338
- if (noteCount > 0) {
339
- countParts.push(`${noteCount} note${noteCount !== 1 ? 's' : ''}`);
340
- }
341
- body += `Conversation (${countParts.join(', ') || '0 messages'})\n`;
342
- body += 'BEGIN\n';
343
- body += '------------\n';
344
-
345
- // Formatted entries
346
- for (const entry of formattedEntries) {
347
- body += entry.content + '\n';
348
- }
349
-
350
- body += '------------\n';
351
- body += 'END';
352
-
353
- return body;
354
- }
355
-
356
- function composeHTMLBody({ conversationCreatedDate, conversationUpdatedDate, contactName, agents, ownerInfo, messageCount, noteCount, formattedEntries }) {
357
- let body = '';
358
-
359
- // Conversation summary header
360
- body += '<div><b>Conversation summary</b><br>';
361
- body += `Started: ${conversationCreatedDate.format('dddd, MMMM DD, YYYY')} at ${conversationCreatedDate.format('hh:mm A')}<br>`;
362
- body += `Ended: ${conversationUpdatedDate ? conversationUpdatedDate.format('dddd, MMMM DD, YYYY') : 'On-going'} at ${conversationUpdatedDate ? conversationUpdatedDate.format('hh:mm A') : 'On-going'}<br>`;
363
- body += `Duration: ${conversationUpdatedDate ? `${conversationUpdatedDate.diff(conversationCreatedDate, 'days')} d ${conversationUpdatedDate.diff(conversationCreatedDate, 'hours')} h` : 'On-going'}<br>`;
364
- body += '</div><br>';
365
- // Participants
366
- body += '<div><b>Participants</b><ul>';
367
- body += `<li>${escapeHtml(contactName)} (customer)</li>`;
368
- for (const agent of agents) {
369
- body += `<li>${escapeHtml(agent)}</li>`;
370
- }
371
- body += '</ul></div>';
372
-
373
- // Owner/Call queue info
374
- if (ownerInfo) {
375
- if (ownerInfo.type === 'callQueue') {
376
- body += `<div>Receiving call queue: <b>${escapeHtml(ownerInfo.name)}</b></div><br>`;
377
- } else {
378
- body += `<div>Owner: <b>${escapeHtml(ownerInfo.name)}</b></div><br>`;
379
- }
380
- }
381
-
382
- // Conversation count
383
- const countParts = [];
384
- if (messageCount > 0) {
385
- countParts.push(`${messageCount} message${messageCount !== 1 ? 's' : ''}`);
386
- }
387
- if (noteCount > 0) {
388
- countParts.push(`${noteCount} note${noteCount !== 1 ? 's' : ''}`);
389
- }
390
- body += `<div><b>Conversation (${countParts.join(', ') || '0 messages'})</b></div>`;
391
- body += '<div>BEGIN</div>';
392
- body += '<hr>';
393
-
394
- // Formatted entries
395
- for (const entry of formattedEntries) {
396
- body += entry.content;
397
- }
398
-
399
- body += '<hr>';
400
- body += '<div>END</div>';
401
-
402
- return body;
403
- }
404
-
405
- function composeMarkdownBody({ conversationCreatedDate, conversationUpdatedDate, contactName, agents, ownerInfo, messageCount, noteCount, formattedEntries }) {
406
- let body = '';
407
-
408
- // Conversation summary header
409
- body += '## Conversation summary\n';
410
- body += `Started: ${conversationCreatedDate.format('dddd, MMMM DD, YYYY')} at ${conversationCreatedDate.format('hh:mm A')}\n`;
411
- body += `Ended: ${conversationUpdatedDate ? conversationUpdatedDate.format('dddd, MMMM DD, YYYY') : 'On-going'} at ${conversationUpdatedDate ? conversationUpdatedDate.format('hh:mm A') : 'On-going'}\n`;
412
- body += `Duration: ${conversationUpdatedDate ? `${conversationUpdatedDate.diff(conversationCreatedDate, 'days')} d ${conversationUpdatedDate.diff(conversationCreatedDate, 'hours')} h` : 'On-going'} \n`;
413
- body += '\n';
414
- // Participants
415
- body += '### Participants\n';
416
- body += `* ${contactName} (customer)\n`;
417
- for (const agent of agents) {
418
- body += `* ${agent}\n`;
419
- }
420
- body += '\n';
421
-
422
- // Owner/Call queue info
423
- if (ownerInfo) {
424
- if (ownerInfo.type === 'callQueue') {
425
- body += `Receiving call queue: **${ownerInfo.name}**\n\n`;
426
- } else {
427
- body += `Owner: **${ownerInfo.name}**\n\n`;
428
- }
429
- }
430
-
431
- // Conversation count
432
- const countParts = [];
433
- if (messageCount > 0) {
434
- countParts.push(`${messageCount} message${messageCount !== 1 ? 's' : ''}`);
435
- }
436
- if (noteCount > 0) {
437
- countParts.push(`${noteCount} note${noteCount !== 1 ? 's' : ''}`);
438
- }
439
- body += `### Conversation (${countParts.join(', ') || '0 messages'})\n`;
440
- body += 'BEGIN\n';
441
- body += '---\n';
442
-
443
- // Formatted entries
444
- for (const entry of formattedEntries) {
445
- body += entry.content + '\n';
446
- }
447
-
448
- body += '---\n';
449
- body += 'END';
450
-
451
- return body;
452
- }
453
-
454
- function escapeHtml(text) {
455
- if (!text) return '';
456
- return text
457
- .replace(/&/g, '&amp;')
458
- .replace(/</g, '&lt;')
459
- .replace(/>/g, '&gt;')
460
- .replace(/"/g, '&quot;')
461
- .replace(/'/g, '&#039;');
462
- }
463
-
464
- module.exports = {
465
- composeSharedSMSLog,
466
- gatherParticipants: gatherAgents,
467
- countEntities,
468
- processEntities,
469
- escapeHtml
470
- };
471
-
1
+ const moment = require('moment-timezone');
2
+ const { LOG_DETAILS_FORMAT_TYPE } = require('./constants');
3
+
4
+ function composeSharedSMSLog({ logFormat = LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT, conversation, contactName, timezoneOffset }) {
5
+ const conversationCreatedDate = moment(conversation?.creationTime);
6
+ const conversationUpdatedDate = moment(findLatestModifiedTime(conversation.messages));
7
+ if (timezoneOffset) {
8
+ conversationCreatedDate.utcOffset(timezoneOffset);
9
+ conversationUpdatedDate.utcOffset(timezoneOffset);
10
+ }
11
+
12
+ const subject = composeSubject({
13
+ logFormat,
14
+ contactName
15
+ });
16
+
17
+ const body = composeBody({
18
+ logFormat,
19
+ conversation,
20
+ contactName,
21
+ conversationCreatedDate,
22
+ conversationUpdatedDate,
23
+ timezoneOffset,
24
+ });
25
+
26
+ return { subject, body };
27
+ }
28
+
29
+ function findLatestModifiedTime(messages) {
30
+ let result = 0;
31
+ for (const message of messages) {
32
+ if (message.lastModifiedTime > result) {
33
+ result = message.lastModifiedTime;
34
+ }
35
+ }
36
+ return result;
37
+ }
38
+
39
+ function composeSubject({ logFormat, contactName }) {
40
+ const title = `SMS conversation with ${contactName}`;
41
+
42
+ switch (logFormat) {
43
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
44
+ return title;
45
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
46
+ return `**${title}**`;
47
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
48
+ default:
49
+ return title;
50
+ }
51
+ }
52
+
53
+ function composeBody({
54
+ logFormat,
55
+ conversation,
56
+ contactName,
57
+ conversationCreatedDate,
58
+ conversationUpdatedDate,
59
+ timezoneOffset
60
+ }) {
61
+ // Gather participants from entities
62
+ const agents = gatherAgents(conversation.entities || []);
63
+
64
+ // Get owner/call queue info
65
+ const ownerInfo = getOwnerInfo(conversation);
66
+
67
+ // Count messages and notes
68
+ const { messageCount, noteCount } = countEntities(conversation.entities || []);
69
+
70
+ // Process entities into formatted entries
71
+ const formattedEntries = processEntities({
72
+ entities: conversation.entities || [],
73
+ timezoneOffset,
74
+ logFormat,
75
+ contactName
76
+ });
77
+
78
+ // Build the body based on format
79
+ switch (logFormat) {
80
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
81
+ return composeHTMLBody({
82
+ conversationCreatedDate,
83
+ conversationUpdatedDate,
84
+ contactName,
85
+ agents,
86
+ ownerInfo,
87
+ messageCount,
88
+ noteCount,
89
+ formattedEntries
90
+ });
91
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
92
+ return composeMarkdownBody({
93
+ conversationCreatedDate,
94
+ conversationUpdatedDate,
95
+ contactName,
96
+ agents,
97
+ ownerInfo,
98
+ messageCount,
99
+ noteCount,
100
+ formattedEntries
101
+ });
102
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
103
+ default:
104
+ return composePlainTextBody({
105
+ conversationCreatedDate,
106
+ conversationUpdatedDate,
107
+ contactName,
108
+ agents,
109
+ ownerInfo,
110
+ messageCount,
111
+ noteCount,
112
+ formattedEntries
113
+ });
114
+ }
115
+ }
116
+
117
+ function gatherAgents(entities) {
118
+ const participantSet = new Set();
119
+
120
+ // Add from entities
121
+ if (entities) {
122
+ for (const entity of entities) {
123
+ if (entity.author?.name) {
124
+ participantSet.add(entity.author.name);
125
+ }
126
+ if (entity.from?.name) {
127
+ participantSet.add(entity.from.name);
128
+ }
129
+ if (entity.initiator?.name) {
130
+ participantSet.add(entity.initiator.name);
131
+ }
132
+ if (entity.assignee?.name) {
133
+ participantSet.add(entity.assignee.name);
134
+ }
135
+ }
136
+ }
137
+ return Array.from(participantSet);
138
+ }
139
+
140
+ function getOwnerInfo(conversation) {
141
+ if (!conversation.owner) return null;
142
+
143
+ const ownerName = conversation.owner.name || '';
144
+ const extensionType = conversation.owner.extensionType;
145
+
146
+ // Check if it's a call queue (Department type)
147
+ if (extensionType === 'Department' || ownerName.toLowerCase().includes('queue')) {
148
+ return {
149
+ type: 'callQueue',
150
+ name: ownerName,
151
+ extensionId: conversation.owner.extensionId
152
+ };
153
+ }
154
+
155
+ return {
156
+ type: 'user',
157
+ name: ownerName,
158
+ extensionId: conversation.owner.extensionId
159
+ };
160
+ }
161
+
162
+ function countEntities(entities) {
163
+ let messageCount = 0;
164
+ let noteCount = 0;
165
+
166
+ for (const entity of entities) {
167
+ if (entity.recordType === 'AliveMessage') {
168
+ messageCount++;
169
+ } else if (entity.recordType === 'AliveNote') {
170
+ noteCount++;
171
+ }
172
+ }
173
+
174
+ return { messageCount, noteCount };
175
+ }
176
+
177
+ function processEntities({ entities, timezoneOffset, logFormat, contactName }) {
178
+ const processedEntries = [];
179
+
180
+ for (const entity of entities) {
181
+ const entry = processEntity({
182
+ entity,
183
+ timezoneOffset,
184
+ logFormat,
185
+ contactName
186
+ });
187
+ if (entry) {
188
+ processedEntries.push(entry);
189
+ }
190
+ }
191
+
192
+ // Sort by creation time (newest first for display)
193
+ processedEntries.sort((a, b) => b.creationTime - a.creationTime);
194
+
195
+ return processedEntries;
196
+ }
197
+
198
+ function processEntity({ entity, timezoneOffset, logFormat, contactName }) {
199
+ const creationTime = entity.creationTime;
200
+ let momentTime = moment(creationTime);
201
+ if (timezoneOffset) {
202
+ momentTime = momentTime.utcOffset(timezoneOffset);
203
+ }
204
+ const formattedTime = momentTime.format('YYYY-MM-DD hh:mm A');
205
+
206
+ switch (entity.recordType) {
207
+ case 'AliveMessage':
208
+ return formatMessage({ entity, contactName, formattedTime, creationTime, logFormat });
209
+
210
+ case 'ThreadAssignedHint':
211
+ return formatAssignment({ entity, formattedTime, creationTime, logFormat });
212
+
213
+ case 'AliveNote':
214
+ return formatNote({ entity, formattedTime, creationTime, logFormat });
215
+
216
+ case 'ThreadCreatedHint':
217
+ // Skip thread created hints - not typically shown in log body
218
+ return null;
219
+
220
+ default:
221
+ return null;
222
+ }
223
+ }
224
+
225
+ function formatMessage({ entity, contactName, formattedTime, creationTime, logFormat }) {
226
+ const authorName = entity.author?.name || entity.from?.name;
227
+ const isInbound = entity.direction === 'Inbound';
228
+ const senderName = isInbound ? contactName : authorName;
229
+ const messageText = entity.text || entity.subject || '';
230
+
231
+ switch (logFormat) {
232
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
233
+ return {
234
+ type: 'message',
235
+ creationTime,
236
+ content: `<p><b>${escapeHtml(senderName)}</b> said on ${formattedTime}:<br>${escapeHtml(messageText)}</p>`
237
+ };
238
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
239
+ return {
240
+ type: 'message',
241
+ creationTime,
242
+ content: `**${senderName}** said on ${formattedTime}:\n${messageText}\n`
243
+ };
244
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
245
+ default:
246
+ return {
247
+ type: 'message',
248
+ creationTime,
249
+ content: `${senderName} said on ${formattedTime}:\n${messageText}\n`
250
+ };
251
+ }
252
+ }
253
+
254
+ function formatAssignment({ entity, formattedTime, creationTime, logFormat }) {
255
+ const assigneeName = entity.assignee?.name || 'Unknown';
256
+
257
+ switch (logFormat) {
258
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
259
+ return {
260
+ type: 'assignment',
261
+ creationTime,
262
+ content: `<p><i>Conversation assigned to <b>${escapeHtml(assigneeName)}</b></i></p>`
263
+ };
264
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
265
+ return {
266
+ type: 'assignment',
267
+ creationTime,
268
+ content: `*Conversation assigned to **${assigneeName}***\n`
269
+ };
270
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
271
+ default:
272
+ return {
273
+ type: 'assignment',
274
+ creationTime,
275
+ content: `Conversation assigned to ${assigneeName}\n`
276
+ };
277
+ }
278
+ }
279
+
280
+ function formatNote({ entity, formattedTime, creationTime, logFormat }) {
281
+ const authorName = entity.author?.name || entity.initiator?.name || 'Unknown';
282
+ const noteText = entity.text || entity.body || '';
283
+
284
+ switch (logFormat) {
285
+ case LOG_DETAILS_FORMAT_TYPE.HTML:
286
+ return {
287
+ type: 'note',
288
+ creationTime,
289
+ content: `<p><b>${escapeHtml(authorName)}</b> left a note on ${formattedTime}:<br>${escapeHtml(noteText)}</p>`
290
+ };
291
+ case LOG_DETAILS_FORMAT_TYPE.MARKDOWN:
292
+ return {
293
+ type: 'note',
294
+ creationTime,
295
+ content: `**${authorName}** left a note on ${formattedTime}:\n${noteText}\n`
296
+ };
297
+ case LOG_DETAILS_FORMAT_TYPE.PLAIN_TEXT:
298
+ default:
299
+ return {
300
+ type: 'note',
301
+ creationTime,
302
+ content: `${authorName} left a note on ${formattedTime}:\n${noteText}\n`
303
+ };
304
+ }
305
+ }
306
+
307
+ function composePlainTextBody({ conversationCreatedDate, conversationUpdatedDate, contactName, agents, ownerInfo, messageCount, noteCount, formattedEntries }) {
308
+ let body = '';
309
+
310
+ // Conversation summary header
311
+ body += 'Conversation summary\n';
312
+ body += `Started: ${conversationCreatedDate.format('dddd, MMMM DD, YYYY')} at ${conversationCreatedDate.format('hh:mm A')}\n`;
313
+ body += `Ended: ${conversationUpdatedDate ? conversationUpdatedDate.format('dddd, MMMM DD, YYYY') : 'On-going'} at ${conversationUpdatedDate ? conversationUpdatedDate.format('hh:mm A') : 'On-going'}\n`;
314
+ body += `Duration: ${conversationUpdatedDate ? `${conversationUpdatedDate.diff(conversationCreatedDate, 'days')} d ${conversationUpdatedDate.diff(conversationCreatedDate, 'hours')} h` : 'On-going'} \n`;
315
+ body += '\n';
316
+ // Participants
317
+ body += 'Participants\n';
318
+ body += `* ${contactName} (customer)\n`;
319
+ for (const agent of agents) {
320
+ body += `* ${agent}\n`;
321
+ }
322
+ body += '\n';
323
+
324
+ // Owner/Call queue info
325
+ if (ownerInfo) {
326
+ if (ownerInfo.type === 'callQueue') {
327
+ body += `Receiving call queue: ${ownerInfo.name}\n\n`;
328
+ } else {
329
+ body += `Owner: ${ownerInfo.name}\n\n`;
330
+ }
331
+ }
332
+
333
+ // Conversation count
334
+ const countParts = [];
335
+ if (messageCount > 0) {
336
+ countParts.push(`${messageCount} message${messageCount !== 1 ? 's' : ''}`);
337
+ }
338
+ if (noteCount > 0) {
339
+ countParts.push(`${noteCount} note${noteCount !== 1 ? 's' : ''}`);
340
+ }
341
+ body += `Conversation (${countParts.join(', ') || '0 messages'})\n`;
342
+ body += 'BEGIN\n';
343
+ body += '------------\n';
344
+
345
+ // Formatted entries
346
+ for (const entry of formattedEntries) {
347
+ body += entry.content + '\n';
348
+ }
349
+
350
+ body += '------------\n';
351
+ body += 'END';
352
+
353
+ return body;
354
+ }
355
+
356
+ function composeHTMLBody({ conversationCreatedDate, conversationUpdatedDate, contactName, agents, ownerInfo, messageCount, noteCount, formattedEntries }) {
357
+ let body = '';
358
+
359
+ // Conversation summary header
360
+ body += '<div><b>Conversation summary</b><br>';
361
+ body += `Started: ${conversationCreatedDate.format('dddd, MMMM DD, YYYY')} at ${conversationCreatedDate.format('hh:mm A')}<br>`;
362
+ body += `Ended: ${conversationUpdatedDate ? conversationUpdatedDate.format('dddd, MMMM DD, YYYY') : 'On-going'} at ${conversationUpdatedDate ? conversationUpdatedDate.format('hh:mm A') : 'On-going'}<br>`;
363
+ body += `Duration: ${conversationUpdatedDate ? `${conversationUpdatedDate.diff(conversationCreatedDate, 'days')} d ${conversationUpdatedDate.diff(conversationCreatedDate, 'hours')} h` : 'On-going'}<br>`;
364
+ body += '</div><br>';
365
+ // Participants
366
+ body += '<div><b>Participants</b><ul>';
367
+ body += `<li>${escapeHtml(contactName)} (customer)</li>`;
368
+ for (const agent of agents) {
369
+ body += `<li>${escapeHtml(agent)}</li>`;
370
+ }
371
+ body += '</ul></div>';
372
+
373
+ // Owner/Call queue info
374
+ if (ownerInfo) {
375
+ if (ownerInfo.type === 'callQueue') {
376
+ body += `<div>Receiving call queue: <b>${escapeHtml(ownerInfo.name)}</b></div><br>`;
377
+ } else {
378
+ body += `<div>Owner: <b>${escapeHtml(ownerInfo.name)}</b></div><br>`;
379
+ }
380
+ }
381
+
382
+ // Conversation count
383
+ const countParts = [];
384
+ if (messageCount > 0) {
385
+ countParts.push(`${messageCount} message${messageCount !== 1 ? 's' : ''}`);
386
+ }
387
+ if (noteCount > 0) {
388
+ countParts.push(`${noteCount} note${noteCount !== 1 ? 's' : ''}`);
389
+ }
390
+ body += `<div><b>Conversation (${countParts.join(', ') || '0 messages'})</b></div>`;
391
+ body += '<div>BEGIN</div>';
392
+ body += '<hr>';
393
+
394
+ // Formatted entries
395
+ for (const entry of formattedEntries) {
396
+ body += entry.content;
397
+ }
398
+
399
+ body += '<hr>';
400
+ body += '<div>END</div>';
401
+
402
+ return body;
403
+ }
404
+
405
+ function composeMarkdownBody({ conversationCreatedDate, conversationUpdatedDate, contactName, agents, ownerInfo, messageCount, noteCount, formattedEntries }) {
406
+ let body = '';
407
+
408
+ // Conversation summary header
409
+ body += '## Conversation summary\n';
410
+ body += `Started: ${conversationCreatedDate.format('dddd, MMMM DD, YYYY')} at ${conversationCreatedDate.format('hh:mm A')}\n`;
411
+ body += `Ended: ${conversationUpdatedDate ? conversationUpdatedDate.format('dddd, MMMM DD, YYYY') : 'On-going'} at ${conversationUpdatedDate ? conversationUpdatedDate.format('hh:mm A') : 'On-going'}\n`;
412
+ body += `Duration: ${conversationUpdatedDate ? `${conversationUpdatedDate.diff(conversationCreatedDate, 'days')} d ${conversationUpdatedDate.diff(conversationCreatedDate, 'hours')} h` : 'On-going'} \n`;
413
+ body += '\n';
414
+ // Participants
415
+ body += '### Participants\n';
416
+ body += `* ${contactName} (customer)\n`;
417
+ for (const agent of agents) {
418
+ body += `* ${agent}\n`;
419
+ }
420
+ body += '\n';
421
+
422
+ // Owner/Call queue info
423
+ if (ownerInfo) {
424
+ if (ownerInfo.type === 'callQueue') {
425
+ body += `Receiving call queue: **${ownerInfo.name}**\n\n`;
426
+ } else {
427
+ body += `Owner: **${ownerInfo.name}**\n\n`;
428
+ }
429
+ }
430
+
431
+ // Conversation count
432
+ const countParts = [];
433
+ if (messageCount > 0) {
434
+ countParts.push(`${messageCount} message${messageCount !== 1 ? 's' : ''}`);
435
+ }
436
+ if (noteCount > 0) {
437
+ countParts.push(`${noteCount} note${noteCount !== 1 ? 's' : ''}`);
438
+ }
439
+ body += `### Conversation (${countParts.join(', ') || '0 messages'})\n`;
440
+ body += 'BEGIN\n';
441
+ body += '---\n';
442
+
443
+ // Formatted entries
444
+ for (const entry of formattedEntries) {
445
+ body += entry.content + '\n';
446
+ }
447
+
448
+ body += '---\n';
449
+ body += 'END';
450
+
451
+ return body;
452
+ }
453
+
454
+ function escapeHtml(text) {
455
+ if (!text) return '';
456
+ return text
457
+ .replace(/&/g, '&amp;')
458
+ .replace(/</g, '&lt;')
459
+ .replace(/>/g, '&gt;')
460
+ .replace(/"/g, '&quot;')
461
+ .replace(/'/g, '&#039;');
462
+ }
463
+
464
+ module.exports = {
465
+ composeSharedSMSLog,
466
+ gatherParticipants: gatherAgents,
467
+ countEntities,
468
+ processEntities,
469
+ escapeHtml
470
+ };
471
+