@@ -21,6 +21,7 @@ import {
21
21
} from 'ringcentral-embeddable-extension-common/src/common/db'
22
22
import { getUserId } from './activities'
23
23
import { notifySyncSuccess , getDealId } from './call-log-sync-to-deal'
24
+ import * as ls from 'ringcentral-embeddable-extension-common/src/common/ls'
24
25
25
26
let {
26
27
showCallLogSyncForm,
@@ -60,10 +61,10 @@ export async function syncCallLogToThirdParty (body) {
60
61
return createForm (
61
62
body ,
62
63
serviceName ,
63
- ( formData ) => doSync ( body , formData )
64
+ ( formData ) => doSync ( body , formData , isManuallySync )
64
65
)
65
66
} else {
66
- doSync ( body , { } )
67
+ doSync ( body , { } , isManuallySync )
67
68
}
68
69
}
69
70
@@ -103,35 +104,40 @@ async function getSyncContacts (body) {
103
104
* @param {* } body
104
105
* @param {* } formData
105
106
*/
106
- async function doSync ( body , formData ) {
107
+ async function doSync ( body , formData , isManuallySync ) {
107
108
let contacts = await getSyncContacts ( body )
108
109
// console.log(contacts, 'ccccc')
109
110
if ( ! contacts . length ) {
110
111
return notify ( 'No related contacts' )
111
112
}
112
113
for ( let contact of contacts ) {
113
- await doSyncOne ( contact , body , formData )
114
+ await doSyncOne ( contact , body , formData , isManuallySync )
114
115
}
115
116
}
116
117
117
118
function buildMsgs ( body ) {
118
119
let msgs = _ . get ( body , 'conversation.messages' )
119
- let arr = msgs . map ( m => {
120
+ const arr = [ ]
121
+ for ( const m of msgs ) {
120
122
let desc = m . direction === 'Outbound'
121
123
? 'to'
122
124
: 'from'
123
125
let n = m . direction === 'Outbound'
124
126
? m . to
125
127
: [ m . from ]
126
128
n = n . map ( m => formatPhoneLocal ( m . phoneNumber ) ) . join ( ', ' )
127
- return `<li><b>${ m . subject } </b> - ${ desc } <b>${ n } </b> - ${ moment ( m . creationTime ) . format ( 'MMM DD, YYYY HH:mm' ) } </li>`
128
- } )
129
- return `<h3>SMS logs:</h3><ul>${ arr . join ( '' ) } </ul>`
129
+ arr . push ( {
130
+ body : `<p>SMS: <b>${ m . subject } </b> - ${ desc } <b>${ n } </b> - ${ moment ( m . creationTime ) . format ( 'MMM DD, YYYY HH:mm' ) } </p>` ,
131
+ id : m . id
132
+ } )
133
+ }
134
+ return arr
130
135
}
131
136
132
137
function buildVoiceMailMsgs ( body ) {
133
138
let msgs = _ . get ( body , 'conversation.messages' )
134
- let arr = msgs . map ( m => {
139
+ const arr = [ ]
140
+ for ( const m of msgs ) {
135
141
let isOut = m . direction === 'Outbound'
136
142
let desc = isOut
137
143
? 'to'
@@ -141,9 +147,33 @@ function buildVoiceMailMsgs (body) {
141
147
: [ m . from ]
142
148
n = n . map ( m => formatPhoneLocal ( m . phoneNumber || m . extensionNumber ) ) . join ( ', ' )
143
149
let links = m . attachments . map ( t => t . link ) . join ( ', ' )
144
- return `<li>${ links } - ${ n ? desc : '' } <b>${ n } </b> ${ moment ( m . creationTime ) . format ( 'MMM DD, YYYY HH:mm' ) } </li>`
145
- } )
146
- return `<h3>Voice mail logs:</h3><ul>${ arr . join ( '' ) } </ul>`
150
+ arr . push ( {
151
+ body : `<p>Voice mail: ${ links } - ${ n ? desc : '' } <b>${ n } </b> ${ moment ( m . creationTime ) . format ( 'MMM DD, YYYY HH:mm' ) } </p>` ,
152
+ id : m . id
153
+ } )
154
+ }
155
+ return arr
156
+ }
157
+
158
+ function buildKey ( id ) {
159
+ return `rc-log-${ userId } -${ id } `
160
+ }
161
+
162
+ async function saveLog ( id , engageId ) {
163
+ const key = buildKey ( id )
164
+ await ls . set ( key , engageId )
165
+ }
166
+
167
+ async function filterLoggered ( arr ) {
168
+ const res = [ ]
169
+ for ( const m of arr ) {
170
+ const key = buildKey ( m . id )
171
+ const ig = await ls . get ( key )
172
+ if ( ! ig ) {
173
+ res . push ( m )
174
+ }
175
+ }
176
+ return res
147
177
}
148
178
149
179
/**
@@ -153,7 +183,7 @@ function buildVoiceMailMsgs (body) {
153
183
* @param {* } body
154
184
* @param {* } formData
155
185
*/
156
- async function doSyncOne ( contact , body , formData ) {
186
+ async function doSyncOne ( contact , body , formData , isManuallySync ) {
157
187
let { id, org_id : oid } = contact
158
188
let toNumber = _ . get ( body , 'call.to.phoneNumber' )
159
189
let fromNumber = _ . get ( body , 'call.from.phoneNumber' )
@@ -162,6 +192,9 @@ async function doSyncOne (contact, body, formData) {
162
192
? `<p>Recording link: ${ body . call . recording . link } </p>`
163
193
: ''
164
194
let token = getSessionToken ( )
195
+ let externalId = body . id ||
196
+ _ . get ( body , 'call.sessionId' ) ||
197
+ _ . get ( body , 'conversation.conversationLogId' )
165
198
let url = `${ host } /api/v1/activities?session_token=${ token } &strict_mode=true`
166
199
let time = _ . get ( body , 'call.startTime' ) ||
167
200
_ . get ( 'body' , 'conversation.messages[0].creationTime' )
@@ -185,36 +218,53 @@ async function doSyncOne (contact, body, formData) {
185
218
mainBody = buildVoiceMailMsgs ( body )
186
219
}
187
220
let logType = body . call ? 'Call' : ctype
188
- let bodyAll = `<p>${ formData . description || '' } </p><p>${ mainBody } </p>${ recording } `
189
- let bd = {
190
- due_date : dueDate ,
191
- due_time : dueTime ,
192
- duration,
193
- note : bodyAll ,
194
- type : 'call' ,
195
- subject : logType ,
196
- done : true ,
197
- participants : [
198
- {
199
- person_id : id ,
200
- primary_flag : true
201
- }
202
- ] ,
203
- person_id : id ,
204
- org_id : oid ,
205
- deal_id : null ,
206
- notification_language_id : 1 ,
207
- assigned_to_user_id : userId
221
+ if ( ! _ . isArray ( mainBody ) ) {
222
+ mainBody = [ {
223
+ body : mainBody ,
224
+ id : externalId
225
+ } ]
208
226
}
209
- let dealId = await getDealId ( contact )
210
- if ( dealId ) {
211
- bd . deal_id = dealId
227
+ if ( ! isManuallySync ) {
228
+ mainBody = await filterLoggered ( mainBody )
212
229
}
213
- let res = await fetch . post ( url , bd )
214
- let success = res && res . data
215
- if ( success ) {
216
- notifySyncSuccess ( { id, logType } )
217
- } else {
218
- notify ( 'call log sync to third party failed' , 'warn' )
230
+ let bodyAll = mainBody . map ( mm => {
231
+ return {
232
+ id : mm . id ,
233
+ body : `<p>${ formData . description || '' } </p><p>${ mm . body } </p>${ recording } `
234
+ }
235
+ } )
236
+ for ( const uit of bodyAll ) {
237
+ let bd = {
238
+ due_date : dueDate ,
239
+ due_time : dueTime ,
240
+ duration,
241
+ note : uit . body ,
242
+ type : 'call' ,
243
+ subject : logType ,
244
+ done : true ,
245
+ participants : [
246
+ {
247
+ person_id : id ,
248
+ primary_flag : true
249
+ }
250
+ ] ,
251
+ person_id : id ,
252
+ org_id : oid ,
253
+ deal_id : null ,
254
+ notification_language_id : 1 ,
255
+ assigned_to_user_id : userId
256
+ }
257
+ let dealId = await getDealId ( contact )
258
+ if ( dealId ) {
259
+ bd . deal_id = dealId
260
+ }
261
+ let res = await fetch . post ( url , bd )
262
+ let success = res && res . data
263
+ if ( success ) {
264
+ await saveLog ( uit . id , res . data . id )
265
+ notifySyncSuccess ( { id, logType } )
266
+ } else {
267
+ notify ( 'call log sync to third party failed' , 'warn' )
268
+ }
219
269
}
220
270
}
0 commit comments