feat: realtime whatsapp message using socket and publish_realtime

This commit is contained in:
Shariq Ansari 2024-04-23 19:58:16 +05:30
parent a9e59ff431
commit 8be27ac2a8
4 changed files with 35 additions and 3 deletions

View File

@ -7,6 +7,12 @@ def validate(doc, method):
doc.reference_doctype = doctype doc.reference_doctype = doctype
doc.reference_name = name doc.reference_name = name
def on_update(doc, method):
frappe.publish_realtime("whatsapp_message", {
'reference_doctype': doc.reference_doctype,
'reference_name': doc.reference_name,
})
def get_lead_or_deal_from_number(number): def get_lead_or_deal_from_number(number):
"""Get lead/deal from the given number. """Get lead/deal from the given number.
""" """

View File

@ -138,6 +138,7 @@ doc_events = {
}, },
"WhatsApp Message": { "WhatsApp Message": {
"validate": ["crm.api.whatsapp.validate"], "validate": ["crm.api.whatsapp.validate"],
"on_update": ["crm.api.whatsapp.on_update"],
}, },
} }

View File

@ -864,10 +864,19 @@ import {
call, call,
} from 'frappe-ui' } from 'frappe-ui'
import { useElementVisibility } from '@vueuse/core' import { useElementVisibility } from '@vueuse/core'
import { ref, computed, h, markRaw, watch, nextTick } from 'vue' import {
ref,
computed,
h,
markRaw,
watch,
nextTick,
onMounted,
onBeforeUnmount,
} from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
const { makeCall } = globalStore() const { makeCall, $socket } = globalStore()
const { getUser } = usersStore() const { getUser } = usersStore()
const { getContact, getLeadContact } = contactsStore() const { getContact, getLeadContact } = contactsStore()
@ -946,6 +955,21 @@ const whatsappMessages = createResource({
onSuccess: () => nextTick(() => scroll()), onSuccess: () => nextTick(() => scroll()),
}) })
onBeforeUnmount(() => {
$socket.off('whatsapp_message')
})
onMounted(() => {
$socket.on('whatsapp_message', (data) => {
if (
data.reference_doctype === props.doctype &&
data.reference_name === doc.value.data.name
) {
whatsappMessages.reload()
}
})
})
function sendTemplate(template) { function sendTemplate(template) {
showWhatsappTemplates.value = false showWhatsappTemplates.value = false
createResource({ createResource({

View File

@ -3,7 +3,7 @@ import { getCurrentInstance, ref } from 'vue'
export const globalStore = defineStore('crm-global', () => { export const globalStore = defineStore('crm-global', () => {
const app = getCurrentInstance() const app = getCurrentInstance()
const { $dialog } = app.appContext.config.globalProperties const { $dialog, $socket } = app.appContext.config.globalProperties
let twilioEnabled = ref(false) let twilioEnabled = ref(false)
let callMethod = () => {} let callMethod = () => {}
@ -22,6 +22,7 @@ export const globalStore = defineStore('crm-global', () => {
return { return {
$dialog, $dialog,
$socket,
twilioEnabled, twilioEnabled,
makeCall, makeCall,
setTwilioEnabled, setTwilioEnabled,