fix: load calls from get_activities

This commit is contained in:
Shariq Ansari 2024-02-18 16:55:45 +05:30
parent 4920027e85
commit 3ba549422e
2 changed files with 78 additions and 84 deletions

View File

@ -32,10 +32,11 @@ def get_deal_activities(name):
lead = doc[2] lead = doc[2]
activities = [] activities = []
calls = []
creation_text = "created this deal" creation_text = "created this deal"
if lead: if lead:
activities = get_lead_activities(lead) activities, calls = get_lead_activities(lead)
creation_text = "converted the lead to this deal" creation_text = "converted the lead to this deal"
activities.append({ activities.append({
@ -125,10 +126,12 @@ def get_deal_activities(name):
} }
activities.append(activity) activities.append(activity)
calls = calls + get_linked_calls(name)
activities.sort(key=lambda x: x["creation"], reverse=True) activities.sort(key=lambda x: x["creation"], reverse=True)
activities = handle_multiple_versions(activities) activities = handle_multiple_versions(activities)
return activities return activities, calls
def get_lead_activities(name): def get_lead_activities(name):
get_docinfo('', "CRM Lead", name) get_docinfo('', "CRM Lead", name)
@ -232,10 +235,12 @@ def get_lead_activities(name):
} }
activities.append(activity) activities.append(activity)
calls = get_linked_calls(name)
activities.sort(key=lambda x: x["creation"], reverse=True) activities.sort(key=lambda x: x["creation"], reverse=True)
activities = handle_multiple_versions(activities) activities = handle_multiple_versions(activities)
return activities return activities, calls
@redis_cache() @redis_cache()
def get_attachments(name): def get_attachments(name):
@ -276,4 +281,26 @@ def parse_grouped_versions(versions):
return version return version
other_versions = versions[1:] other_versions = versions[1:]
version["other_versions"] = other_versions version["other_versions"] = other_versions
return version return version
def get_linked_calls(name):
calls = frappe.db.get_all(
"CRM Call Log",
filters={"reference_docname": name},
fields=[
"name",
"caller",
"receiver",
"from",
"to",
"duration",
"start_time",
"end_time",
"status",
"type",
"recording_url",
"creation",
"note",
],
)
return calls or []

View File

@ -799,82 +799,47 @@ const reload = defineModel('reload')
const reload_email = ref(false) const reload_email = ref(false)
const versions = createResource({ const all_activities = createResource({
url: 'crm.api.activities.get_activities', url: 'crm.api.activities.get_activities',
params: { name: doc.value.data.name }, params: { name: doc.value.data.name },
cache: ['activity', doc.value.data.name], cache: ['activity', doc.value.data.name],
}) auto: true,
transform: ([versions, calls]) => {
onMounted(() => { if (calls?.length) {
if (versions.data == null) { calls.forEach((doc) => {
versions.fetch() doc.show_recording = false
} doc.activity_type =
if (calls.data == null) { doc.type === 'Incoming' ? 'incoming_call' : 'outgoing_call'
calls.fetch() doc.duration = secondsToDuration(doc.duration)
} if (doc.type === 'Incoming') {
if (notes.data == null) { doc.caller = {
notes.fetch() label:
} getContact(doc.from)?.full_name ||
if (tasks.data == null) { getLeadContact(doc.from)?.full_name ||
tasks.fetch() 'Unknown',
} image:
}) getContact(doc.from)?.image || getLeadContact(doc.from)?.image,
}
const calls = createListResource({ doc.receiver = {
type: 'list', label: getUser(doc.receiver).full_name,
doctype: 'CRM Call Log', image: getUser(doc.receiver).user_image,
cache: ['Call Logs', doc.value.data.name], }
fields: [ } else {
'name', doc.caller = {
'caller', label: getUser(doc.caller).full_name,
'receiver', image: getUser(doc.caller).user_image,
'from', }
'to', doc.receiver = {
'duration', label:
'start_time', getContact(doc.to)?.full_name ||
'end_time', getLeadContact(doc.to)?.full_name ||
'status', 'Unknown',
'type', image: getContact(doc.to)?.image || getLeadContact(doc.to)?.image,
'recording_url', }
'creation',
'note',
],
filters: { reference_docname: doc.value.data.name },
orderBy: 'creation desc',
pageLength: 999,
transform: (docs) => {
docs.forEach((doc) => {
doc.show_recording = false
doc.activity_type =
doc.type === 'Incoming' ? 'incoming_call' : 'outgoing_call'
doc.duration = secondsToDuration(doc.duration)
if (doc.type === 'Incoming') {
doc.caller = {
label:
getContact(doc.from)?.full_name ||
getLeadContact(doc.from)?.full_name ||
'Unknown',
image: getContact(doc.from)?.image || getLeadContact(doc.from)?.image,
} }
doc.receiver = { })
label: getUser(doc.receiver).full_name, }
image: getUser(doc.receiver).user_image, return { versions, calls }
}
} else {
doc.caller = {
label: getUser(doc.caller).full_name,
image: getUser(doc.caller).user_image,
}
doc.receiver = {
label:
getContact(doc.to)?.full_name ||
getLeadContact(doc.to)?.full_name ||
'Unknown',
image: getContact(doc.to)?.image || getLeadContact(doc.to)?.image,
}
}
})
return docs
}, },
}) })
@ -908,10 +873,11 @@ const tasks = createListResource({
pageLength: 999, pageLength: 999,
}) })
function all_activities() { function get_activities() {
if (!versions.data) return [] if (!all_activities.data?.versions) return []
if (!calls.data) return versions.data if (!all_activities.data?.calls.length)
return [...versions.data, ...calls.data].sort( return all_activities.data.versions || []
return [...all_activities.data.versions, ...all_activities.data.calls].sort(
(a, b) => new Date(a.creation) - new Date(b.creation) (a, b) => new Date(a.creation) - new Date(b.creation)
) )
} }
@ -919,14 +885,15 @@ function all_activities() {
const activities = computed(() => { const activities = computed(() => {
let activities = [] let activities = []
if (props.title == 'Activity') { if (props.title == 'Activity') {
activities = all_activities() activities = get_activities()
} else if (props.title == 'Emails') { } else if (props.title == 'Emails') {
if (!versions.data) return [] if (!all_activities.data?.versions) return []
activities = versions.data activities = all_activities.data.versions
.filter((activity) => activity.activity_type === 'communication') .filter((activity) => activity.activity_type === 'communication')
.sort((a, b) => new Date(a.creation) - new Date(b.creation)) .sort((a, b) => new Date(a.creation) - new Date(b.creation))
} else if (props.title == 'Calls') { } else if (props.title == 'Calls') {
return calls.data?.sort( if (!all_activities.data?.calls) return []
return all_activities.data.calls.sort(
(a, b) => new Date(a.creation) - new Date(b.creation) (a, b) => new Date(a.creation) - new Date(b.creation)
) )
} else if (props.title == 'Tasks') { } else if (props.title == 'Tasks') {