Skip to content

Commit d0b1f3d

Browse files
committed
Fix duplicate log issue
1 parent f07f8df commit d0b1f3d

File tree

4 files changed

+98
-48
lines changed

4 files changed

+98
-48
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pipedrive-embeddable-ringcentral-phone-spa",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Add RingCentral Embeddable Voice widgets to Pipedrive",
55
"keywords": [
66
"RingCentral",

src/features/call-log-sync.js

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from 'ringcentral-embeddable-extension-common/src/common/db'
2222
import { getUserId } from './activities'
2323
import { notifySyncSuccess, getDealId } from './call-log-sync-to-deal'
24+
import * as ls from 'ringcentral-embeddable-extension-common/src/common/ls'
2425

2526
let {
2627
showCallLogSyncForm,
@@ -60,10 +61,10 @@ export async function syncCallLogToThirdParty (body) {
6061
return createForm(
6162
body,
6263
serviceName,
63-
(formData) => doSync(body, formData)
64+
(formData) => doSync(body, formData, isManuallySync)
6465
)
6566
} else {
66-
doSync(body, {})
67+
doSync(body, {}, isManuallySync)
6768
}
6869
}
6970

@@ -103,35 +104,40 @@ async function getSyncContacts (body) {
103104
* @param {*} body
104105
* @param {*} formData
105106
*/
106-
async function doSync (body, formData) {
107+
async function doSync (body, formData, isManuallySync) {
107108
let contacts = await getSyncContacts(body)
108109
// console.log(contacts, 'ccccc')
109110
if (!contacts.length) {
110111
return notify('No related contacts')
111112
}
112113
for (let contact of contacts) {
113-
await doSyncOne(contact, body, formData)
114+
await doSyncOne(contact, body, formData, isManuallySync)
114115
}
115116
}
116117

117118
function buildMsgs (body) {
118119
let msgs = _.get(body, 'conversation.messages')
119-
let arr = msgs.map(m => {
120+
const arr = []
121+
for (const m of msgs) {
120122
let desc = m.direction === 'Outbound'
121123
? 'to'
122124
: 'from'
123125
let n = m.direction === 'Outbound'
124126
? m.to
125127
: [m.from]
126128
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
130135
}
131136

132137
function buildVoiceMailMsgs (body) {
133138
let msgs = _.get(body, 'conversation.messages')
134-
let arr = msgs.map(m => {
139+
const arr = []
140+
for (const m of msgs) {
135141
let isOut = m.direction === 'Outbound'
136142
let desc = isOut
137143
? 'to'
@@ -141,9 +147,33 @@ function buildVoiceMailMsgs (body) {
141147
: [m.from]
142148
n = n.map(m => formatPhoneLocal(m.phoneNumber || m.extensionNumber)).join(', ')
143149
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
147177
}
148178

149179
/**
@@ -153,7 +183,7 @@ function buildVoiceMailMsgs (body) {
153183
* @param {*} body
154184
* @param {*} formData
155185
*/
156-
async function doSyncOne (contact, body, formData) {
186+
async function doSyncOne (contact, body, formData, isManuallySync) {
157187
let { id, org_id: oid } = contact
158188
let toNumber = _.get(body, 'call.to.phoneNumber')
159189
let fromNumber = _.get(body, 'call.from.phoneNumber')
@@ -162,6 +192,9 @@ async function doSyncOne (contact, body, formData) {
162192
? `<p>Recording link: ${body.call.recording.link}</p>`
163193
: ''
164194
let token = getSessionToken()
195+
let externalId = body.id ||
196+
_.get(body, 'call.sessionId') ||
197+
_.get(body, 'conversation.conversationLogId')
165198
let url = `${host}/api/v1/activities?session_token=${token}&strict_mode=true`
166199
let time = _.get(body, 'call.startTime') ||
167200
_.get('body', 'conversation.messages[0].creationTime')
@@ -185,36 +218,53 @@ async function doSyncOne (contact, body, formData) {
185218
mainBody = buildVoiceMailMsgs(body)
186219
}
187220
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+
}]
208226
}
209-
let dealId = await getDealId(contact)
210-
if (dealId) {
211-
bd.deal_id = dealId
227+
if (!isManuallySync) {
228+
mainBody = await filterLoggered(mainBody)
212229
}
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+
}
219269
}
220270
}

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Ringcentral Embeddable Widgets for Pipedrive",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Add RingCentral Embeddable Voice widgets to Pipedrive",
55
"permissions": [
66
"http://*/",

0 commit comments

Comments
 (0)