webex-gc-wrapper
Version:
A Webex wrapper is a TypeScript module that encapsulates event listeners and interaction logic for Webex Contact Center
304 lines (303 loc) • 13.9 kB
JavaScript
var T = Object.defineProperty;
var U = (y, t, n) => t in y ? T(y, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : y[t] = n;
var E = (y, t, n) => U(y, typeof t != "symbol" ? t + "" : t, n);
class b {
constructor(t, n, s) {
E(this, "window");
E(this, "webexFrame");
E(this, "callbacks");
this.window = t, this.webexFrame = n, this.callbacks = s, this.addAgentInfoListener(this.callbacks.login), this.addAgentOfferContactListener(this.callbacks.addInteraction), this.addAgentOfferConsultListener(this.callbacks.addInteraction), this.addAgentContactListener(this.callbacks.addInteraction), this.addAgentConsultCreatedListener(this.callbacks.addInteraction), this.addAgentContactAssignedListener(this.callbacks.addInteraction), this.addAgentOfferContactRonaListener(this.callbacks.removeInteractionwithoutactivity), this.addAgentConsultEndedListener(this.callbacks.removeInteractionwithoutactivity), this.addAgentContactEndedListener(this.callbacks.removeInteractionwithoutactivity), this.addAgentConsultConferenceEndedListener(this.callbacks.endInteraction), this.addAgentContactWrappedUpListener(this.callbacks.endInteraction), this.addAttributeChangedListener(this.callbacks.interactionSwitching);
}
addAgentInfoListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
var a, r, o, h, c;
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentInfo") {
const i = (a = e == null ? void 0 : e.content) == null ? void 0 : a.find((p) => p.name === "agentName"), f = (r = e == null ? void 0 : e.content) == null ? void 0 : r.find((p) => p.name === "wrapupCodes"), u = (o = e == null ? void 0 : e.content) == null ? void 0 : o.find((p) => p.name === "isOutboundEnabledForAgent"), l = (h = e == null ? void 0 : e.content) == null ? void 0 : h.find((p) => p.name === "dn"), d = (c = e == null ? void 0 : e.content) == null ? void 0 : c.find((p) => p.name === "subStatus");
i && i.value != null && localStorage.setItem("agentName", i.value), l && l.value != null && !localStorage.getItem("dn") && localStorage.setItem("dn", l.value), f && f.value != null && localStorage.setItem("wrapUpCodes", JSON.stringify(f.value)), u && u.value != null && localStorage.setItem("isOutboundEnabledForAgent", JSON.stringify(u.value)), d && d.value !== null && localStorage.setItem("ef_agent", d.value || ""), t(e, {
agentNameItem: i,
dnItem: l,
wrapupCodesItem: f,
outboundEnabledItem: u,
current: d
});
}
};
return this.window.addEventListener("message", n), n;
}
addAgentOfferContactListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentOfferContact") {
const a = this.extractInteractionData(e);
console.log("extractedData", a), t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentOfferConsultListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentOfferConsult") {
const a = this.extractInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentContactListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentContact") {
const a = this.extractInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentConsultCreatedListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
var a, r;
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentConsultCreated") {
let o = (r = (a = e == null ? void 0 : e.content) == null ? void 0 : a.data) == null ? void 0 : r.interactionId;
if (o) {
let c = JSON.parse(localStorage.getItem("conversation_ids") || "[]");
c.push(o), localStorage.setItem("consult_array", JSON.stringify(c)), console.log("Updated conversation IDs in localStorage:", c);
}
t(e, "");
}
};
return this.window.addEventListener("message", n), n;
}
addAgentContactAssignedListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
(e == null ? void 0 : e.type) === "agentContactAssigned" && t(e, "");
};
return this.window.addEventListener("message", n), n;
}
addAgentOfferContactRonaListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentOfferContactRona") {
const a = this.removeInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentConsultEndedListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentConsultEnded") {
const a = this.removeInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentContactEndedListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentContactEnded") {
const a = this.removeInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentConsultConferenceEndedListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentConsultConferenceEnded") {
const a = this.endInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAgentContactWrappedUpListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
const e = this.parseMessage(s);
if ((e == null ? void 0 : e.type) === "agentContactWrappedUp") {
const a = this.endInteractionData(e);
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
addAttributeChangedListener(t) {
if (typeof t != "function") return;
this._isFnCheck(t);
const n = (s) => {
var r, o, h, c, i, f, u, l;
const e = this.parseMessage(s);
let a;
if ((e == null ? void 0 : e.type) === "attributeChanged") {
if (((o = (r = e == null ? void 0 : e.content) == null ? void 0 : r.data) == null ? void 0 : o.name) === "outdialep") {
const d = (c = (h = e == null ? void 0 : e.content) == null ? void 0 : h.data) == null ? void 0 : c.value;
localStorage.setItem("entryId", d);
} else if (((f = (i = e == null ? void 0 : e.content) == null ? void 0 : i.data) == null ? void 0 : f.name) === "selectedtaskid") {
const d = (l = (u = e == null ? void 0 : e.content) == null ? void 0 : u.data) == null ? void 0 : l.value;
if (!d || d.trim() === "")
console.log("Invalid interactionId: It is null, undefined, or empty.");
else {
const g = JSON.parse(localStorage.getItem(d) || "");
g && d && (a = g);
}
}
t(e, a);
}
};
return this.window.addEventListener("message", n), n;
}
removeListener(t) {
this._isFnCheck(t), this.window.removeEventListener("message", t);
}
dialNumber(t) {
var e, a;
const n = t.calledNumber, s = localStorage.getItem("entryId");
console.log("Phone Number: ", n), (a = (e = this.webexFrame) == null ? void 0 : e.contentWindow) == null || a.postMessage({ type: "userOutDial", number: n, id: s }, "*");
}
parseMessage(t) {
try {
return typeof t.data == "string" ? JSON.parse(t.data) : t.data;
} catch (n) {
return console.error("Failed to parse message data:", t.data, n), null;
}
}
endInteractionData(t) {
var O, A, N, k, x, _;
console.log("Incoming Data: ", t);
let n;
const s = ((O = t == null ? void 0 : t.content) == null ? void 0 : O.data) || {}, e = s.interactionId || "", a = s.eventTime || "", r = ((N = (A = s.interaction) == null ? void 0 : A.contactDirection) == null ? void 0 : N.type) || "";
let o = ((k = s.interaction) == null ? void 0 : k.mediaType) || "";
const h = (x = s == null ? void 0 : s.interaction) == null ? void 0 : x.state, c = localStorage.getItem(e);
if (!c) return;
const i = JSON.parse(c), f = i.startTime, u = i.selectedRecordId;
o = this.getTaskType(o);
const l = i.agentEmailId, d = i.agentName, g = i.ani, I = o.toLowerCase() === "email" || o.toLowerCase() === "chat" ? l : localStorage.getItem("dn"), v = a - f, w = this.format(v), L = this.convertDurationToSeconds(w);
if (t.type === "agentConsultConferenceEnded") {
let C = JSON.parse(localStorage.getItem("consult_array") || "[]");
C.includes(e) ? (C = C.filter((m) => m !== e), localStorage.setItem("consult_array", JSON.stringify(C))) : n = {
mediaType: o,
direction: r,
fromNumber: r === "OUTBOUND" ? I : g,
toNumber: r === "OUTBOUND" ? g : I,
agentName: d,
agentEmailId: l,
wrapup: "Conference Ended",
duration: L,
id: e,
selectedRecordId: u
};
}
if (t.type === "agentContactWrappedUp") {
const C = s == null ? void 0 : s.wrapUpAuxCodeId;
console.log("Wrap Up code: ", C);
const S = JSON.parse(localStorage.getItem("wrapUpCodes") || "[]");
console.log("All wrap up codes: ", S);
const m = ((_ = S.find(
(F) => F.id === C
)) == null ? void 0 : _.name) || "No data found";
console.log("Selected Data: ", m), o !== "Unknown" && h !== "new" && (n = {
mediaType: o,
direction: r,
fromNumber: r === "OUTBOUND" ? I : g,
toNumber: r === "OUTBOUND" ? g : I,
agentName: d,
agentEmailId: l,
wrapup: m,
duration: L,
id: e,
selectedRecordId: u
});
}
return console.log("Returning the Data for Activity: ", n), n;
}
convertDurationToSeconds(t) {
const n = t.split(":").map(Number), s = n.length === 3 ? n[0] : 0, e = n.length >= 2 ? n[n.length - 2] : 0, a = n[n.length - 1] || 0;
return s * 3600 + e * 60 + a;
}
format(t) {
const n = Math.floor(t / 1e3), s = Math.floor(n / 3600), e = Math.floor(n % 3600 / 60);
return `${this.pad(s)}:${this.pad(e)}:${this.pad(n % 60)}`;
}
pad(t) {
return t.toString().padStart(2, "0");
}
getTaskType(t) {
const n = {
chat: "Chat",
voice: "Call",
telephony: "Call",
email: "Email"
};
return typeof t == "string" && t in n ? n[t] : "Unknown";
}
extractInteractionData(t) {
var f, u, l, d, g, p, I, v, w;
const n = ((f = t == null ? void 0 : t.content) == null ? void 0 : f.data) || {}, s = n.interactionId || "", e = ((l = (u = n.interaction) == null ? void 0 : u.callAssociatedDetails) == null ? void 0 : l.ani) || "", a = ((p = (g = (d = n.interaction) == null ? void 0 : d.participants) == null ? void 0 : g[n.agentId]) == null ? void 0 : p.name) || "", r = n.eventTime || "", o = ((v = (I = n.interaction) == null ? void 0 : I.contactDirection) == null ? void 0 : v.type) || "", h = ((w = n.interaction) == null ? void 0 : w.mediaType) || "", c = n.agentEmailId || "", i = {
id: s,
ani: e,
agentName: a,
agentExtension: "",
startTime: r,
selectedRecordId: "",
crmResults: "",
direction: o,
mediaType: h,
agentEmailId: c
};
return console.log("callPayload", i), (t == null ? void 0 : t.type) !== "agentConsultCreated" && localStorage.setItem(s, JSON.stringify(i)), i;
}
removeInteractionData(t) {
var o, h, c, i, f, u, l;
const n = (h = (o = t == null ? void 0 : t.content) == null ? void 0 : o.data) == null ? void 0 : h.interactionId, s = (f = (i = (c = t == null ? void 0 : t.content) == null ? void 0 : c.data) == null ? void 0 : i.interaction) == null ? void 0 : f.state;
let e = {};
const a = JSON.parse(localStorage.getItem(n) || "{}");
let r = JSON.parse(localStorage.getItem("consult_array") || "[]");
if ((t == null ? void 0 : t.type) === "agentContactEnded")
if (s && s === "new")
e = a, console.log("Data in remove Interaction ist if: ", e);
else {
const d = (l = (u = t == null ? void 0 : t.content) == null ? void 0 : u.data) == null ? void 0 : l.agentEmailId;
a && (!a.agentEmailId || a.agentEmailId.trim() === "") && (a.agentEmailId = d || "", localStorage.setItem(n, JSON.stringify(a)), console.log("agentEmailId inserted into retrievedData and saved."), e = "");
}
else (t.type === "agentOfferContactRona" || t.type === "agentConsultEnded") && (r.includes(n) ? (r = r.filter((d) => d !== n), localStorage.setItem("consult_array", JSON.stringify(r)), console.log("Removed interactionId from conversation_ids:", n)) : (e = a, console.log("Data in remove Interaction 2nd else: ", e)));
return console.log("Overall returned Data: ", e), e;
}
_isFnCheck(t) {
if (t !== void 0) {
if (typeof t != "function")
throw new Error("Callback must be a function");
return !0;
}
}
}
export {
b as default
};