From 32c4b24a3107414749978f91fd61a22ce5bc755a Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 4 Nov 2024 17:55:46 +0530 Subject: [PATCH] fix: added saas api's to communicate with frappe cloud --- crm/api/saas_billing.py | 56 +++++++++++++++++++ crm/www/crm.py | 4 +- frappe-ui | 2 +- .../src/components/Layouts/AppSidebar.vue | 1 + frontend/src/components/Settings/Settings.vue | 9 ++- 5 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 crm/api/saas_billing.py diff --git a/crm/api/saas_billing.py b/crm/api/saas_billing.py new file mode 100644 index 000000000..2601f98f5 --- /dev/null +++ b/crm/api/saas_billing.py @@ -0,0 +1,56 @@ +import frappe +import requests + +def get_base_url(): + url = "https://frappecloud.com" + if frappe.conf.developer_mode and frappe.conf.get("saas_billing_base_url"): + url = frappe.conf.get("saas_billing_base_url") + return url + +def get_site_name(): + site_name = frappe.local.site + if frappe.conf.developer_mode and frappe.conf.get("saas_billing_site_name"): + site_name = frappe.conf.get("saas_billing_site_name") + return site_name + +def get_headers(): + # check if user is system manager + if frappe.get_roles(frappe.session.user).count("System Manager") == 0: + frappe.throw("You are not allowed to access this resource") + + # check if communication secret is set + if not frappe.conf.get("fc_communication_secret"): + frappe.throw("Communication secret not set") + + return { + "X-Site-Token": frappe.conf.get("fc_communication_secret"), + "X-Site": get_site_name() + } + +@frappe.whitelist() +def generate_access_token(): + request = requests.post(f"{get_base_url()}/api/method/press.saas.api.auth.generate_access_token", headers=get_headers()) + if request.status_code == 200: + return request.json()["message"] + else: + frappe.throw("Failed to generate access token") + +@frappe.whitelist() +def is_access_token_valid(token): + headers={ 'Content-Type': 'application/json' } + request = requests.post(f"{get_base_url()}/api/method/press.saas.api.auth.is_access_token_valid", headers, json={ token }) + return request.json()["message"] + +@frappe.whitelist() +def current_site_info(): + request = requests.post(f"{get_base_url()}/api/method/press.saas.api.site.info", headers=get_headers()) + if request.status_code == 200: + return request.json().get("message") + else: + frappe.throw("Failed to get site info") + +@frappe.whitelist() +def saas_api(method, data={}): + request = requests.post(f"{get_base_url()}/api/method/press.saas.api.{method}", headers=get_headers(), json=data) + print(request.json()) + return request.json().get("message") \ No newline at end of file diff --git a/crm/www/crm.py b/crm/www/crm.py index 38a6f13a0..be96a45f6 100644 --- a/crm/www/crm.py +++ b/crm/www/crm.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import frappe -from frappe.boot import add_subscription_conf from frappe.utils import cint from frappe.utils.telemetry import capture @@ -34,8 +33,7 @@ def get_boot(): "site_name": frappe.local.site, "read_only_mode": frappe.flags.read_only, "csrf_token": frappe.sessions.get_csrf_token(), - "telemetry_site_age": frappe.utils.telemetry.site_age(), - "subscription_conf": add_subscription_conf(), + "fc_communication_secret": frappe.conf.fc_communication_secret, "setup_complete": cint(frappe.get_system_settings("setup_complete")) } ) diff --git a/frappe-ui b/frappe-ui index b496da753..519cc536e 160000 --- a/frappe-ui +++ b/frappe-ui @@ -1 +1 @@ -Subproject commit b496da753880dba27223813979f8cee8417bcc90 +Subproject commit 519cc536e720c4e785432084c04be63f450a2d4e diff --git a/frontend/src/components/Layouts/AppSidebar.vue b/frontend/src/components/Layouts/AppSidebar.vue index 723ac60a0..a4aa76271 100644 --- a/frontend/src/components/Layouts/AppSidebar.vue +++ b/frontend/src/components/Layouts/AppSidebar.vue @@ -75,6 +75,7 @@ diff --git a/frontend/src/components/Settings/Settings.vue b/frontend/src/components/Settings/Settings.vue index 145d49b5d..da8e9a3e6 100644 --- a/frontend/src/components/Settings/Settings.vue +++ b/frontend/src/components/Settings/Settings.vue @@ -85,18 +85,21 @@ const tabs = computed(() => { }, { label: __('Subscription'), - condition: () => window.subscription_conf, + condition: () => window.fc_communication_secret && isManager(), items: [ { label: 'Plans', icon: UpgradeIcon, - component: markRaw(Plans), + component: markRaw(h(Plans, { baseAPIPath: 'crm.api.saas_billing' })), }, { label: 'Billing', icon: WalletsIcon, component: markRaw( - h(Billing, { onChangePlan: () => setActiveTab('Plans') }), + h(Billing, { + baseAPIPath: 'crm.api.saas_billing', + onChangePlan: () => setActiveTab('Plans'), + }), ), }, ],