From 0904ba3c624754f42f9c7c18da83e6d5799ceffd Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Fri, 3 Nov 2023 16:02:23 +0530 Subject: [PATCH] fix: initialize call if twilio is enabled --- .../twilio_settings/twilio_settings.json | 22 +++++-- crm/twilio/api.py | 4 ++ crm/twilio/twilio_handler.py | 6 +- frontend/src/components/CallUI.vue | 59 +++++++++++-------- frontend/src/composables/twilio.js | 6 ++ 5 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 frontend/src/composables/twilio.js diff --git a/crm/fcrm/doctype/twilio_settings/twilio_settings.json b/crm/fcrm/doctype/twilio_settings/twilio_settings.json index ed3aa43f..8aef9749 100644 --- a/crm/fcrm/doctype/twilio_settings/twilio_settings.json +++ b/crm/fcrm/doctype/twilio_settings/twilio_settings.json @@ -7,6 +7,7 @@ "editable_grid": 1, "engine": "InnoDB", "field_order": [ + "section_break_malx", "account_sid", "api_key", "api_secret", @@ -14,8 +15,9 @@ "auth_token", "twiml_sid", "section_break_ssqj", - "record_calls", - "column_break_avmt" + "enabled", + "column_break_avmt", + "record_calls" ], "fields": [ { @@ -23,7 +25,7 @@ "fieldtype": "Data", "in_list_view": 1, "label": "Account SID", - "reqd": 1 + "mandatory_depends_on": "eval: doc.enabled" }, { "fieldname": "api_key", @@ -46,7 +48,7 @@ "fieldtype": "Password", "in_list_view": 1, "label": "Auth Token", - "reqd": 1 + "mandatory_depends_on": "eval: doc.enabled" }, { "fieldname": "twiml_sid", @@ -67,12 +69,22 @@ { "fieldname": "column_break_avmt", "fieldtype": "Column Break" + }, + { + "fieldname": "section_break_malx", + "fieldtype": "Section Break" + }, + { + "default": "0", + "fieldname": "enabled", + "fieldtype": "Check", + "label": "Enabled" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2023-09-20 16:42:17.025651", + "modified": "2023-11-03 15:13:09.155818", "modified_by": "Administrator", "module": "FCRM", "name": "Twilio Settings", diff --git a/crm/twilio/api.py b/crm/twilio/api.py index 536b62ae..4297e5a7 100644 --- a/crm/twilio/api.py +++ b/crm/twilio/api.py @@ -5,6 +5,10 @@ import frappe from frappe import _ from .twilio_handler import Twilio, IncomingCall, TwilioCallDetails +@frappe.whitelist() +def is_enabled(): + return frappe.db.get_single_value("Twilio Settings", "enabled") + @frappe.whitelist() def generate_access_token(): """Returns access token that is required to authenticate Twilio Client SDK. diff --git a/crm/twilio/twilio_handler.py b/crm/twilio/twilio_handler.py index 61e527dd..71a64461 100644 --- a/crm/twilio/twilio_handler.py +++ b/crm/twilio/twilio_handler.py @@ -27,8 +27,8 @@ class Twilio: """Make a twilio connection. """ settings = frappe.get_doc("Twilio Settings") - # if not (settings and settings.enabled): - # return + if not (settings and settings.enabled): + return return Twilio(settings=settings) def get_phone_numbers(self): @@ -115,6 +115,8 @@ class Twilio: @classmethod def get_twilio_client(self): twilio_settings = frappe.get_doc("Twilio Settings") + if not twilio_settings.enabled: + frappe.throw(_("Please enable twilio settings before making a call.")) auth_token = get_decrypted_password("Twilio Settings", "Twilio Settings", 'auth_token') client = TwilioClient(twilio_settings.account_sid, auth_token) diff --git a/frontend/src/components/CallUI.vue b/frontend/src/components/CallUI.vue index 92f4ae87..55cf9596 100644 --- a/frontend/src/components/CallUI.vue +++ b/frontend/src/components/CallUI.vue @@ -2,17 +2,17 @@
-
- +
+
-
+
@@ -22,11 +22,11 @@
{{ contact.mobile_no }}
-
+
{{ counterUp?.updatedTime }}
-
+
{{ callStatus == 'ringing' ? 'Ringing...' @@ -43,13 +43,13 @@ />
@@ -87,7 +87,7 @@ @click="acceptIncomingCall" >
@@ -107,16 +107,16 @@
-
+
{{ contact.full_name }}
@@ -124,10 +124,10 @@
{{ counterUp?.updatedTime }}
-
@@ -152,21 +152,21 @@
@@ -197,6 +197,7 @@ const contact = ref({ mobile_no: '', }) +let enabled = ref(false) let showCallPopup = ref(false) let showSmallCallWindow = ref(false) let onCall = ref(false) @@ -244,6 +245,10 @@ let { style } = useDraggable(callPopup, { preventDefault: true, }) +async function is_twilio_enabled() { + return await call('crm.twilio.api.is_enabled') +} + async function startupClient() { log.value = 'Requesting Access Token...' @@ -469,7 +474,10 @@ function toggleCallWindow() { } } -onMounted(() => startupClient()) +onMounted(async () => { + enabled.value = await is_twilio_enabled() + enabled.value && startupClient() +}) watch( () => log.value, @@ -481,6 +489,7 @@ watch( const app = getCurrentInstance() app.appContext.config.globalProperties.makeCall = makeOutgoingCall +app.appContext.config.globalProperties.is_twilio_enabled = enabled.value