Files
call-sdk/packages/adapter-ameyo/lib/assets/js/ajaxClient.ts
2024-10-29 18:07:27 +05:30

362 lines
10 KiB
TypeScript

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-nocheck
export enum MessagingType {
ON_AMEYO_CALL_INCOMING= 'onAmeyoCallIncoming',
ON_AMEYO_CALL_ACCEPTED = 'onAmeyoCallAccepted',
ON_AMEYO_CALL_DISCONNECTED = 'onAmeyoCallDisconnected',
}
let pushConnectionIndex = 0;
const pushHistory = [];
let sizeOfHistory = 5;
let isConnectionEstablished = false;
let shouldStopConnection = false;
let isAttempting = false;
let connStart;
let connEnd;
let conAttemptFailed = 0;
let reConnectionCount = 0;
let baseUrl;
let sessionId;
let pushConnectionHealthTimer = null;
let lastPushReceivedTime = '';
const monitorPushTime = 60 * 1000;
//call the above function to create the XMLHttpRequest object
let http = createRequestObject();
enum pushResponseTypes {
UserCallModelUpdatedPush = 'UserCallModelUpdatedPush',
CRMCreateNotifyPush = 'CRMCreateNotifyPush'
}
export function parseQuerystring(url: string): Record<string, any> {
const foo = url?.split('?')?.[1]?.split('&') || [];
const dict = {};
let elem = [];
for (let i = foo.length - 1; i >= 0; i--) {
elem = foo[i].split('=');
dict[elem[0]] = elem[1];
}
return dict || {};
}
function extractUserCallModelUpdatedPush(rawResponse) {
const parts = rawResponse
.replaceAll(`Content-type: application/json ; charset: utf-8\r\n\r\n`, '')
.split("--DON'T_PANIC!_more_will_follow\r\n");
let res = null;
for (const part of parts) {
try {
const jsonData = JSON.parse(part.trim());
if (jsonData?.pushType === pushResponseTypes.CRMCreateNotifyPush) {
const crtObjectId = jsonData?.data?.crtObjectId;
const parsedObject = parseQuerystring(jsonData?.data?.crmURL);
const phoneNumber = parsedObject?.phone;
const lan = parsedObject?.loanaccountnumber;
localStorage.setItem(
'revEngCustomerInfo',
JSON.stringify({ phoneNumber, lan, crtObjectId })
);
}
if (jsonData?.pushType === pushResponseTypes.UserCallModelUpdatedPush) {
res = jsonData;
}
} catch (e) {
/* empty */
}
}
return res;
}
const sendCallStatusMessage = res => {
let message = null;
const { phoneNumber, lan, crtObjectId } = JSON.parse(
localStorage.getItem('revEngCustomerInfo') || '{}'
);
if (res?.data?.status === 'ringing') {
message = {
type: MessagingType.ON_AMEYO_CALL_INCOMING,
data: {
phoneNumber,
lan,
crtObjectId
}
};
} else if (res?.data?.status === 'connected') {
message = {
type: MessagingType.ON_AMEYO_CALL_ACCEPTED,
data: {
phoneNumber,
lan,
crtObjectId
}
};
} else if (res?.data?.status === 'hungup') {
message = {
type: MessagingType.ON_AMEYO_CALL_DISCONNECTED,
data: {
phoneNumber,
lan,
crtObjectId
}
};
}
if (message) window.postMessage(message);
};
function createRequestObject() {
let tmpXmlHttpObject;
// depending on what the browser supports, use the right way to create the
// XMLHttpRequest object
if (window.XMLHttpRequest) {
// Mozilla, Safari would use this method ...
tmpXmlHttpObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// IE would use this method ...
tmpXmlHttpObject = new ActiveXObject('Microsoft.XMLHTTP');
}
return tmpXmlHttpObject;
}
//This is called after login to start attempting push connection through post request
export function establishPushConnection(url, session) {
baseUrl = url;
sessionId = session;
makePostRequest(baseUrl, sessionId);
}
//This should be after logout to stop re-establishing push connection
function stopPushConnection() {
shouldStopConnection = true;
if (http) {
http.abort();
}
}
function reset() {
pushConnectionIndex = 0;
sizeOfHistory = 5;
isConnectionEstablished = false;
shouldStopConnection = false;
isAttempting = false;
conAttemptFailed = 0;
reConnectionCount = 0;
}
function processResponse() {
// check if the response has been received from the server
if (http.readyState == 3 || http.readyState == 4) {
reConnectionCount = 0;
if (http.status == 200) {
changeConnectionEstablishState(true);
}
// read and assign the response from the server
const pushResponse = http.responseText.substring(pushConnectionIndex, http.responseText.length);
pushConnectionIndex = http.responseText.length;
// process push received one at a time
processPush(pushResponse);
// in this case simply assign the response to the contents of the <div>
// on the page.
// document.getElementById('description').innerHTML = http.responseText;
const currentTime = new Date();
lastPushReceivedTime = currentTime.getTime();
}
// Note :This sample does not consider content boundary in the multipart
// response
}
function processPush(pushResponse) {
console.log('push response', pushResponse);
sendCallStatusMessage(extractUserCallModelUpdatedPush(pushResponse));
//ajaxRequest.handleIntermediateResponse(pushResponse);
}
function makePostRequest(url, session) {
if (shouldStopConnection == true) {
console.log('should stop connection');
return;
}
reConnectionCount++;
if (reConnectionCount > 10) {
console.log('reconnection count reached not reconnecting');
return;
}
if (isConnectionEstablished) {
console.log('connection is already established , so not establishing a connection');
return;
}
if (conAttemptFailed > 5) {
console.log('continous attempt failed limit reached');
alert('Unable to connect to the server. Please login after some time.');
return;
}
if (isAttempting) {
return;
} else {
isAttempting = true;
}
connStart = new Date();
http = createRequestObject();
const listenerName = `webcore_${Date.now()}`; //window.listenerName ;
window.listenerName = listenerName;
let lastProcessedSeq = -1; //window.lastProcessedPush;
let finalUrl = url;
const tempSession = session;
if (typeof lastProcessedSeq == 'undefined' || lastProcessedSeq == null) {
lastProcessedSeq = -1;
}
if (typeof pushConnectionHealthTimer != 'undefined' && pushConnectionHealthTimer != null) {
clearInterval(pushConnectionHealthTimer);
pushConnectionHealthTimer = null;
}
finalUrl = finalUrl + '&listener-name=' + listenerName + '&lastProcessedPush=' + lastProcessedSeq;
console.log(
'going to create push connection with seq:- ' +
lastProcessedSeq +
' listener name:- ' +
listenerName
);
console.log('final url ', finalUrl);
// make a connection to the server ... specifying that you intend to make a
// GET request
// to the server. Specifiy the page name and the URL parameters to send
http.open('POST', finalUrl, true);
http.setRequestHeader('sessionId', tempSession);
pushConnectionIndex = 0;
// assign a handler for the response Origin null is not allowed by
// Access-Control-Allow-Origin.
http.onreadystatechange = processResponse;
http.addEventListener('loadend', function checkPushEventHealth(e) {
connEnd = new Date();
const seconds = (connEnd.getTime() - connStart.getTime()) / 1000;
if (seconds < 5) {
conAttemptFailed++;
} else {
conAttemptFailed = 0;
}
isAttempting = false;
changeConnectionEstablishState(false);
makePostRequest(baseUrl, sessionId);
});
http.addEventListener('loadstart', function createCheckPushHealthTimer() {
if (typeof pushConnectionHealthTimer == 'undefined' || pushConnectionHealthTimer == null) {
pushConnectionHealthTimer = window.setInterval(
function () {
checkAjaxPushConnectionHealth();
},
3 * 1000 // 3 seconds
);
}
});
// actually send the request to the server
http.send();
}
function checkAjaxPushConnectionHealth() {
if (typeof http == 'undefined' || http == null) {
return;
}
const time = new Date();
const timeDiff = time.getTime() - lastPushReceivedTime;
if (timeDiff >= monitorPushTime) {
resetConn();
}
}
function resetConn() {
console.log('going to reset connection');
if (http) {
http.abort();
}
}
function makeConnectionEstablishHistory(isEstablished) {
const connectionRecord = new Object();
connectionRecord.state = isEstablished;
connectionRecord.dateAdded = new Date();
console.log(
'push connection state changed to ' +
connectionRecord.state +
' at ' +
connectionRecord.dateAdded
);
addRecordInHistory(connectionRecord);
}
function addRecordInHistory(record) {
if (pushHistory != null && record != null) {
if (pushHistory.length > sizeOfHistory) {
pushHistory.pop(0);
}
pushHistory.push(record);
}
}
function getPushConnectionHistory() {
let pushConnectionString = '';
if (pushHistory != null) {
for (index = 0; index < pushHistory.length; index++) {
const connectionRecord = pushHistory[index];
pushConnectionString =
pushConnectionString +
'push connection state changed to ' +
connectionRecord.state +
' at ' +
connectionRecord.dateAdded +
' \n ';
}
}
return pushConnectionString;
}
/**
* change state of the connection establish. ignore if same state event came.
*
* @param isEstablished
*/
function changeConnectionEstablishState(isEstablished) {
try {
if (isConnectionEstablished == isEstablished) {
} else {
isConnectionEstablished = isEstablished;
makeConnectionEstablishHistory(isEstablished);
}
} catch (err) {
console.log('unable to log connection establish history');
}
}