diff --git a/crm/api/whatsapp.py b/crm/api/whatsapp.py index 3b1b5bae..111d702a 100644 --- a/crm/api/whatsapp.py +++ b/crm/api/whatsapp.py @@ -40,6 +40,86 @@ def parse_mobile_no(mobile_no: str): """ return ''.join([c for c in mobile_no if c.isdigit() or c == '+']) +@frappe.whitelist() +def get_whatsapp_messages(reference_doctype, reference_name): + messages = frappe.get_all( + "WhatsApp Message", + filters={ + "reference_doctype": reference_doctype, + "reference_name": reference_name + }, + fields=[ + 'name', + 'type', + 'to', + 'from', + 'content_type', + 'message_type', + 'attach', + 'template', + 'use_template', + 'message_id', + 'is_reply', + 'reply_to_message_id', + 'creation', + 'message', + 'status', + 'reference_doctype', + 'reference_name', + ], + ) + + # Filter messages to get only Template messages + template_messages = [message for message in messages if message['message_type'] == 'Template'] + + # Iterate through template messages + for template_message in template_messages: + # Find the template that this message is using + template = frappe.get_doc("WhatsApp Templates", template_message['template']) + + # If the template is found, add the template details to the template message + if template: + template_message['template_name'] = template.template_name + template_message['template'] = template.template + template_message['header'] = template.header + template_message['footer'] = template.footer + + # Filter messages to get only reaction messages + reaction_messages = [message for message in messages if message['content_type'] == 'reaction'] + + # Iterate through reaction messages + for reaction_message in reaction_messages: + # Find the message that this reaction is reacting to + reacted_message = next((m for m in messages if m['message_id'] == reaction_message['reply_to_message_id']), None) + + # If the reacted message is found, add the reaction to it + if reacted_message: + reacted_message['reaction'] = reaction_message['message'] + + # Filter messages to get only replies + reply_messages = [message for message in messages if message['is_reply']] + + # Iterate through reply messages + for reply_message in reply_messages: + # Find the message that this message is replying to + replied_message = next((m for m in messages if m['message_id'] == reply_message['reply_to_message_id']), None) + + # If the replied message is found, add the reply details to the reply message + doc = frappe.get_doc(reply_message['reference_doctype'], reply_message['reference_name']) + from_name = replied_message['from'] + for c in doc.contacts: + if c.is_primary: + from_name = c.full_name or c.mobile_no + if replied_message: + reply_message['reply_message'] = replied_message['template'] if replied_message['message_type'] == 'Template' else replied_message['message'] + reply_message['header'] = replied_message.get('header') or '' + reply_message['footer'] = replied_message.get('footer') or '' + reply_message['reply_to'] = replied_message['name'] + reply_message['reply_to_type'] = replied_message['type'] + reply_message['reply_to_from'] = from_name + + return [message for message in messages if message['content_type'] != 'reaction'] + @frappe.whitelist() def create_whatsapp_message(reference_doctype, reference_name, message, to, attach, reply_to, content_type="text"): doc = frappe.new_doc("WhatsApp Message") diff --git a/frontend/src/components/Activities.vue b/frontend/src/components/Activities.vue index 09b060e5..77a990dd 100644 --- a/frontend/src/components/Activities.vue +++ b/frontend/src/components/Activities.vue @@ -934,73 +934,15 @@ const all_activities = createResource({ const showWhatsappTemplates = ref(false) -const whatsappMessages = createListResource({ - type: 'list', - doctype: 'WhatsApp Message', - cache: ['whatsapp_message', doc.value.data.name], - fields: [ - 'name', - 'type', - 'to', - 'from', - 'content_type', - 'attach', - 'template', - 'use_template', - 'message_id', - 'is_reply', - 'reply_to_message_id', - 'creation', - 'message', - 'status', - ], - filters: { +const whatsappMessages = createResource({ + url: 'crm.api.whatsapp.get_whatsapp_messages', + cache: ['whatsapp_messages', doc.value.data.name], + params: { reference_doctype: props.doctype, reference_name: doc.value.data.name, - status: ['!=', 'failed'], }, - orderBy: 'modified desc', - pageLength: 99999, auto: true, - transform: (data) => { - data = sortByCreation(data) - // loop on filtered data where message.content_type == 'reaction' - data - .filter((message) => message.content_type == 'reaction') - .forEach((message) => { - // find the message that this reaction is reacting to - const reactedMessage = data.find( - (m) => m.message_id == message.reply_to_message_id - ) - // if the reacted message is found, add the reaction to it - if (reactedMessage) { - reactedMessage.reaction = message.message - } - }) - - // loop on filtered data where message.is_reply == 1 - data - .filter((message) => message.is_reply) - .forEach((message) => { - // find the message that this message is replying to - const repliedMessage = data.find( - (m) => m.message_id == message.reply_to_message_id - ) - // if the replied message is found, add the reply to it - if (repliedMessage) { - message.reply_message = repliedMessage.message - message.reply_to = repliedMessage.name - message.reply_to_type = repliedMessage.type - message.reply_to_from = - (repliedMessage.from && - getContact(doc.value.data.mobile_no)?.full_name) || - getLeadContact(doc.value.data.mobile_no)?.full_name || - repliedMessage.from - } - }) - - return data.filter((message) => message.content_type != 'reaction') - }, + transform: (data) => sortByCreation(data), onSuccess: () => nextTick(() => scroll()), })