Initial commit
This commit is contained in:
86
fusion_ringcentral/static/src/js/rc_phone_widget.js
Normal file
86
fusion_ringcentral/static/src/js/rc_phone_widget.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import { registry } from "@web/core/registry";
|
||||
import { session } from "@web/session";
|
||||
import { rpc } from "@web/core/network/rpc";
|
||||
|
||||
const RC_ADAPTER_URL = "https://apps.ringcentral.com/integration/ringcentral-embeddable/latest/adapter.js";
|
||||
|
||||
let rcWidgetLoaded = false;
|
||||
|
||||
async function loadRcWidget() {
|
||||
if (rcWidgetLoaded) return;
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = await rpc("/ringcentral/widget-config", {});
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config || !config.enabled || !config.client_id) return;
|
||||
|
||||
rcWidgetLoaded = true;
|
||||
|
||||
const script = document.createElement("script");
|
||||
const params = new URLSearchParams({
|
||||
clientId: config.client_id,
|
||||
appServer: config.app_server || "https://platform.ringcentral.com",
|
||||
disableInactiveTabCallEvent: "1",
|
||||
});
|
||||
script.src = `${RC_ADAPTER_URL}?${params.toString()}`;
|
||||
document.head.appendChild(script);
|
||||
|
||||
window.addEventListener("message", (e) => {
|
||||
if (!e.data) return;
|
||||
|
||||
switch (e.data.type) {
|
||||
case "rc-login-status-notify":
|
||||
window.__rcLoggedIn = e.data.loggedIn;
|
||||
break;
|
||||
case "rc-active-call-notify":
|
||||
window.__rcActiveCall = e.data.call;
|
||||
break;
|
||||
case "rc-call-end-notify":
|
||||
_logCallToOdoo(e.data.call);
|
||||
break;
|
||||
case "rc-adapter-syncPresence":
|
||||
window.__rcPresence = {
|
||||
dndStatus: e.data.dndStatus,
|
||||
userStatus: e.data.userStatus,
|
||||
telephonyStatus: e.data.telephonyStatus,
|
||||
};
|
||||
window.dispatchEvent(new CustomEvent("rc-presence-changed", {
|
||||
detail: window.__rcPresence,
|
||||
}));
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function _logCallToOdoo(call) {
|
||||
if (!call) return;
|
||||
try {
|
||||
await rpc("/web/dataset/call_kw", {
|
||||
model: "rc.call.history",
|
||||
method: "create",
|
||||
args: [{
|
||||
rc_session_id: String(call.sessionId || ""),
|
||||
direction: (call.direction || "").toLowerCase() === "inbound" ? "inbound" : "outbound",
|
||||
from_number: call.from || "",
|
||||
to_number: call.to || "",
|
||||
duration: call.duration || 0,
|
||||
status: call.duration > 0 ? "answered" : "missed",
|
||||
}],
|
||||
kwargs: {},
|
||||
});
|
||||
} catch {
|
||||
// Silently fail -- cron will catch it
|
||||
}
|
||||
}
|
||||
|
||||
registry.category("services").add("rc_phone_widget", {
|
||||
start() {
|
||||
loadRcWidget();
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user