Merge remote-tracking branch 'origin/NTP-27557-ext-deprec' into NTP-27557-ext-deprec
# Conflicts: # packages/adapter-ameyo/CHANGELOG.md # packages/common/CHANGELOG.md # packages/core/CHANGELOG.md
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import getResponseWithoutCors from "@universal-call-sdk/common/lib/utils/getResponseWithoutCors.ts";
|
||||
import apiHelper from "@universal-call-sdk/common/lib/utils/apiHelper.ts";
|
||||
import {CallTransferData, RequestKeys} from "./types";
|
||||
import RequestType from "@universal-call-sdk/common/lib/types/RequestType.ts";
|
||||
import { RequestType } from "@universal-call-sdk/common/lib/types/Request.ts";
|
||||
import {v4 as uuid} from 'uuid';
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ export const loginInAmeyo = (userId: string, password: string) => {
|
||||
if(!userId || !password) {
|
||||
return
|
||||
}
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/userLogin/login`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.AMEYO_LOGIN,
|
||||
@@ -30,7 +30,7 @@ export const loginInAmeyo = (userId: string, password: string) => {
|
||||
};
|
||||
export const maintainHeartbeat = (sessionId: string, webcoreTimestamp: string, counter: number) => {
|
||||
const HEART_BEAT_REQUEST_TEXT = `7|0|8|${window.BASE_AMEYO_URL}/app/application_ui/|93B024BCBAF3AE5EFEC49890FE64CF5F|com.drishti.ameyo.common.ui.rpc.CommonGwtRpcService|keepAliveWithPingPush|java.lang.String/2004016611|java.lang.Integer/3438268394|${sessionId}|${webcoreTimestamp}|1|2|3|4|3|5|5|6|7|8|6|${counter}|${sessionId}|`;
|
||||
setInterval(() => getResponseWithoutCors({
|
||||
setInterval(() => apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyo40/service`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.AMEYO_HEARTBEAT,
|
||||
@@ -44,7 +44,7 @@ export const maintainHeartbeat = (sessionId: string, webcoreTimestamp: string, c
|
||||
};
|
||||
|
||||
export const setAgentOnBreak = (sessionId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/cc/userBreak`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.AMEYO_ON_BREAK,
|
||||
@@ -62,7 +62,7 @@ export const setAgentOnBreak = (sessionId: string) => {
|
||||
|
||||
export const attachOmniqueService = (sessionId: string, userId: string, campaignId: string) => {
|
||||
const OMNIQUEUE_TEXT = `7|0|9|${window.BASE_AMEYO_URL}/app/application_ui/|5B1786BFF89E87DC6EAAACDE629A89E2|com.drishti.ameyo.omniqueue.shared.CommonGwtRpcService|selectCamapign|java.lang.String/2004016611|[Ljava.lang.Integer;/1574882222|${sessionId}|${userId}|java.lang.Integer/3438268394|1|2|3|4|3|5|5|6|7|8|6|1|9|${campaignId}|${sessionId}|`;
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyoomniqueue/service`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.OMNIQUEUE_SERVICE,
|
||||
@@ -76,7 +76,7 @@ export const attachOmniqueService = (sessionId: string, userId: string, campaign
|
||||
};
|
||||
|
||||
export const setAgentActive = (sessionId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/cc/userReady`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.AMEYO_AVAILABLE,
|
||||
@@ -92,7 +92,7 @@ export const setAgentActive = (sessionId: string) => {
|
||||
};
|
||||
|
||||
export const getSipAccountInfo = (sessionId: string, userId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/voice/sipAccountInfos/getSipAccountInfo?info=false`,
|
||||
method: 'GET',
|
||||
requestKey: RequestKeys.SIP_ACCOUNT_INFO,
|
||||
@@ -106,7 +106,7 @@ export const getSipAccountInfo = (sessionId: string, userId: string) => {
|
||||
};
|
||||
|
||||
export const autoSelectExtension = (sessionId: string, userId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/voice/autoSelectExtension`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.AUTO_SELECT_EXTENSION,
|
||||
@@ -123,7 +123,7 @@ export const autoSelectExtension = (sessionId: string, userId: string) => {
|
||||
|
||||
export const selectCampaign = (sessionId: string, userId: string, campaignId: string) => {
|
||||
const SELECT_CAMPAIGN_TEXT = `7|0|9|${window.BASE_AMEYO_URL}/app/application_ui/|5B1786BFF89E87DC6EAAACDE629A89E2|com.drishti.ameyo.omniqueue.shared.CommonGwtRpcService|selectCamapign|java.lang.String/2004016611|[Ljava.lang.Integer;/1574882222|${sessionId}|${userId}|java.lang.Integer/3438268394|1|2|3|4|3|5|5|6|7|8|6|1|9|${campaignId}|${sessionId}|`;
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyoomniqueue/service`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.SELECT_CAMPAIGN,
|
||||
@@ -137,7 +137,7 @@ export const selectCampaign = (sessionId: string, userId: string, campaignId: st
|
||||
};
|
||||
|
||||
export const setAutoStatus = (sessionId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/cc/setAutoStatusForUser`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.SET_AUTO_STATUS,
|
||||
@@ -150,7 +150,7 @@ export const setAutoStatus = (sessionId: string) => {
|
||||
};
|
||||
|
||||
export const ameyoHangupUser = (sessionId: string, userCRTObjectId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/voice/hangupUser`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.HANGUP_USER,
|
||||
@@ -163,7 +163,7 @@ export const ameyoHangupUser = (sessionId: string, userCRTObjectId: string) => {
|
||||
};
|
||||
|
||||
export const ameyoDisposeCall = (sessionId: string, campaignId: string, crtObjectId: string, userCRTObjectId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/voice/disposeCall`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.DISPOSE_CALL,
|
||||
@@ -185,7 +185,7 @@ export const ameyoDisposeCall = (sessionId: string, campaignId: string, crtObjec
|
||||
};
|
||||
|
||||
export const getCampaignId = (sessionId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/sessionData/getAllSessionData`,
|
||||
method: 'GET',
|
||||
requestKey: RequestKeys.GET_CAMPAIGN_ID,
|
||||
@@ -198,7 +198,7 @@ export const getCampaignId = (sessionId: string) => {
|
||||
}
|
||||
|
||||
export const getAllAgentsForTransferCall = (sessionId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/voice/getAllUserCampaignVoicePresencesByContactCenterId`,
|
||||
method: 'GET',
|
||||
requestKey: RequestKeys.GET_AGENTS_FOR_CALL_TRANSFER,
|
||||
@@ -212,7 +212,7 @@ export const getAllAgentsForTransferCall = (sessionId: string) => {
|
||||
|
||||
export const logoutFromAmeyo = (sessionId: string) => {
|
||||
const LOGOUT_PAYLOAD_TEXT = `7|0|7|${window.BASE_AMEYO_URL}/app/application_ui/|C066834E4747CF348FD8D1A68865C24D|com.drishti.ameyo.common.ui.rpc.CommonGwtRpcService|doLogout|java.lang.String/2004016611|${sessionId}|Logout from UI|1|2|3|4|2|5|5|6|7|${sessionId}|`;
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyo40/service`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.LOGOUT_FROM_AMEYO,
|
||||
@@ -227,7 +227,7 @@ export const logoutFromAmeyo = (sessionId: string) => {
|
||||
|
||||
|
||||
export const transferCallToAgent = (data: CallTransferData, sessionId: string, crtObjectId: string, userCRTObjectId: string, campaignId: string) => {
|
||||
return getResponseWithoutCors({
|
||||
return apiHelper({
|
||||
url: `${window.BASE_AMEYO_URL}/ameyorestapi/voice/transferToUserInDifferentCampaign`,
|
||||
method: 'POST',
|
||||
requestKey: RequestKeys.TRANSFER_CALL_TO_AGENT,
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
AmeyoInitializationOptions,
|
||||
CALL_STATES, CallbackFunctions,
|
||||
CallTransferData,
|
||||
RequestKeys,
|
||||
SipAccountInfo,
|
||||
StateType
|
||||
} from "./types";
|
||||
@@ -84,8 +83,6 @@ class AmeyoAdapter implements IAdapter {
|
||||
},
|
||||
onLoginFailed: () => {
|
||||
},
|
||||
onAgentsForCallTransfer: () => {
|
||||
},
|
||||
onCallTransferStatus: () => {
|
||||
}
|
||||
};
|
||||
@@ -142,8 +139,26 @@ class AmeyoAdapter implements IAdapter {
|
||||
}
|
||||
};
|
||||
|
||||
private initializeAmeyo = (): void => {
|
||||
loginInAmeyo(this.userName.toLowerCase(), this.password);
|
||||
private initializeAmeyo = async (): Promise<void> => {
|
||||
try {
|
||||
const loginResponse: GenericObject = await loginInAmeyo(this.userName.toLowerCase(), this.password);
|
||||
if (!loginResponse) {
|
||||
console.error("Invalid username or password");
|
||||
return;
|
||||
}
|
||||
this.trackApiMetrics(loginResponse);
|
||||
const sipInfoResponse: GenericObject | undefined = await this.handleLoginResponse(loginResponse);
|
||||
if (!sipInfoResponse) {
|
||||
console.error("Login failed: Invalid response");
|
||||
return;
|
||||
}
|
||||
const campaignIdResponse: GenericObject = await this.handleSipAccountInfo(sipInfoResponse);
|
||||
await this.handleCampaignId(campaignIdResponse);
|
||||
await this.handleOmniQueueService();
|
||||
await this.handleCampaignSelection();
|
||||
} catch (error) {
|
||||
console.error("Error during Ameyo initialization:", error);
|
||||
}
|
||||
};
|
||||
|
||||
private handleMessage = async ({data}: MessageEvent): Promise<void> => {
|
||||
@@ -153,9 +168,6 @@ class AmeyoAdapter implements IAdapter {
|
||||
}
|
||||
|
||||
switch (data.type) {
|
||||
case MessagingType.SET_RESPONSE_WITHOUT_CORS:
|
||||
await this.handleCorsBypassResponse(data);
|
||||
break;
|
||||
case MessagingType.ON_AMEYO_CALL_INCOMING:
|
||||
this.handleCallIncoming(data);
|
||||
break;
|
||||
@@ -207,39 +219,8 @@ class AmeyoAdapter implements IAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
private async handleCorsBypassResponse(payload: GenericObject): Promise<void> {
|
||||
this.trackApiMetrics(payload);
|
||||
|
||||
switch (payload?.data?.requestKey) {
|
||||
case RequestKeys.AMEYO_LOGIN:
|
||||
await this.handleLoginResponse(payload);
|
||||
break;
|
||||
case RequestKeys.SIP_ACCOUNT_INFO:
|
||||
await this.handleSipAccountInfo(payload);
|
||||
break;
|
||||
case RequestKeys.GET_CAMPAIGN_ID:
|
||||
await this.handleCampaignId(payload);
|
||||
break;
|
||||
case RequestKeys.AMEYO_AVAILABLE:
|
||||
this.handleAgentAvailable();
|
||||
break;
|
||||
case RequestKeys.OMNIQUEUE_SERVICE:
|
||||
await this.handleOmniQueueService();
|
||||
break;
|
||||
case RequestKeys.SELECT_CAMPAIGN:
|
||||
await this.handleCampaignSelection();
|
||||
break;
|
||||
case RequestKeys.AMEYO_ON_BREAK:
|
||||
this.handleAgentBreak();
|
||||
break;
|
||||
case RequestKeys.GET_AGENTS_FOR_CALL_TRANSFER:
|
||||
this.handleAgentsForTransfer(payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private trackApiMetrics(payload: GenericObject): void {
|
||||
if (payload?.data?.requestKey !== RequestKeys.AMEYO_HEARTBEAT && !payload?.data?.err) {
|
||||
if (!payload?.data?.err) {
|
||||
this.metricProcessor?.pushCounterMetric({
|
||||
metricName: 'ameyo-api-call-count',
|
||||
flow: 'api-call-count',
|
||||
@@ -251,9 +232,7 @@ class AmeyoAdapter implements IAdapter {
|
||||
subFlow: payload?.data?.requestKey,
|
||||
value: payload?.data?.time / 1000 || 0
|
||||
});
|
||||
}
|
||||
|
||||
if (payload?.data?.err) {
|
||||
} else {
|
||||
this.metricProcessor?.pushCounterMetric({
|
||||
metricName: 'ameyo-api-err-count',
|
||||
flow: 'api-error-count',
|
||||
@@ -266,7 +245,7 @@ class AmeyoAdapter implements IAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private async handleLoginResponse(payload: GenericObject): Promise<void> {
|
||||
private handleLoginResponse = async (payload: GenericObject): Promise<GenericObject | undefined> => {
|
||||
if (payload?.data?.err) {
|
||||
console.log('Login failed:', payload?.data?.err);
|
||||
this.callbacks.onLoginFailed(payload?.err);
|
||||
@@ -275,12 +254,14 @@ class AmeyoAdapter implements IAdapter {
|
||||
|
||||
const sessionId = payload?.data?.response?.userSessionInfo?.sessionId;
|
||||
this.sessionId = sessionId;
|
||||
await getSipAccountInfo(sessionId, this.userName?.toLowerCase());
|
||||
const res = await getSipAccountInfo(sessionId, this.userName?.toLowerCase());
|
||||
this.trackApiMetrics(res);
|
||||
registerEventProcessor(this.eventListenerUrl, sessionId);
|
||||
maintainHeartbeat(this.sessionId, window?.listenerName || '', 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
private async handleSipAccountInfo(payload: GenericObject): Promise<void> {
|
||||
private handleSipAccountInfo = async (payload: GenericObject): Promise<GenericObject> => {
|
||||
const response = payload?.data?.response;
|
||||
this.initializeSipStack({
|
||||
accountName: response?.accountName,
|
||||
@@ -290,29 +271,34 @@ class AmeyoAdapter implements IAdapter {
|
||||
});
|
||||
this.sipAccountInfo = payload?.data?.response;
|
||||
console.log('sip account info', this.sipAccountInfo)
|
||||
await getCampaignId(this.sessionId);
|
||||
const res = await getCampaignId(this.sessionId);
|
||||
this.trackApiMetrics(res);
|
||||
await setAutoStatus(this.sessionId);
|
||||
return res;
|
||||
}
|
||||
|
||||
private async handleCampaignId(payload: GenericObject): Promise<void> {
|
||||
private handleCampaignId = async (payload: GenericObject): Promise<void> => {
|
||||
this.campaignId = payload?.data?.response?.campaignInfos?.[0]?.campaignId;
|
||||
await attachOmniqueService(this.sessionId, this.userName.toLowerCase(), this.campaignId);
|
||||
const res = await attachOmniqueService(this.sessionId, this.userName.toLowerCase(), this.campaignId);
|
||||
this.trackApiMetrics(res);
|
||||
}
|
||||
|
||||
private handleAgentAvailable(): void {
|
||||
private handleAgentAvailable = (): void => {
|
||||
setAutoStatus(this.sessionId);
|
||||
}
|
||||
|
||||
private handleAgentBreak(): void {
|
||||
private handleAgentBreak = (): void => {
|
||||
setAutoStatus(this.sessionId);
|
||||
}
|
||||
|
||||
private async handleOmniQueueService(): Promise<void> {
|
||||
await selectCampaign(this.sessionId, this.userName.toLowerCase(), this.campaignId);
|
||||
private handleOmniQueueService = async (): Promise<void> => {
|
||||
const res = await selectCampaign(this.sessionId, this.userName.toLowerCase(), this.campaignId);
|
||||
this.trackApiMetrics(res);
|
||||
}
|
||||
|
||||
private async handleCampaignSelection(): Promise<void> {
|
||||
await autoSelectExtension(this.sessionId, this.userName.toLowerCase());
|
||||
private handleCampaignSelection = async (): Promise<void> => {
|
||||
const res = await autoSelectExtension(this.sessionId, this.userName.toLowerCase());
|
||||
this.trackApiMetrics(res);
|
||||
this.callbacks.onAdapterReady();
|
||||
this.currentCallState = CALL_STATES.IDLE;
|
||||
window.postMessage({type: 'onAmeyoAvailabiltyChange', data: false});
|
||||
@@ -324,10 +310,6 @@ class AmeyoAdapter implements IAdapter {
|
||||
window.postMessage({type: 'onAmeyoAvailabiltyChange', data: this.isAgentAvailable,});
|
||||
}
|
||||
|
||||
private handleAgentsForTransfer(payload: GenericObject): void {
|
||||
this.callbacks.onAgentsForCallTransfer(payload?.data?.response);
|
||||
}
|
||||
|
||||
private handleCallIncoming(data: GenericObject): void {
|
||||
this.callbacks.onCallIncoming(data?.data);
|
||||
this.currentCallState = CALL_STATES.CALL_INCOMING;
|
||||
@@ -372,12 +354,16 @@ class AmeyoAdapter implements IAdapter {
|
||||
);
|
||||
}
|
||||
|
||||
public setOnBreak(): void {
|
||||
setAgentOnBreak(this.sessionId);
|
||||
public async setOnBreak(): Promise<void> {
|
||||
const res = await setAgentOnBreak(this.sessionId);
|
||||
this.trackApiMetrics(res);
|
||||
this.handleAgentBreak();
|
||||
}
|
||||
|
||||
public setAvailable(): void {
|
||||
setAgentActive(this.sessionId);
|
||||
public async setAvailable(): Promise<void> {
|
||||
const res = await setAgentActive(this.sessionId);
|
||||
this.trackApiMetrics(res);
|
||||
this.handleAgentAvailable();
|
||||
}
|
||||
|
||||
public muteCall(): void {
|
||||
@@ -396,8 +382,8 @@ class AmeyoAdapter implements IAdapter {
|
||||
return this.currentCallState;
|
||||
}
|
||||
|
||||
public getAvailableAgentsForCallTransfer(): void {
|
||||
getAllAgentsForTransferCall(this.sessionId);
|
||||
public async getAvailableAgentsForCallTransfer(): Promise<Response> {
|
||||
return getAllAgentsForTransferCall(this.sessionId);
|
||||
}
|
||||
|
||||
public transferCallToAgent(data: CallTransferData): void {
|
||||
@@ -438,11 +424,6 @@ class AmeyoAdapter implements IAdapter {
|
||||
public registerOnLoginFailedListener(callback: () => void): void {
|
||||
this.callbacks.onLoginFailed = callback;
|
||||
}
|
||||
|
||||
public registerOnAgentsForCallTransfer(callback: (data: GenericObject) => void): void {
|
||||
this.callbacks.onAgentsForCallTransfer = callback;
|
||||
}
|
||||
|
||||
public registerOnCallTransferStatus(callback: (data: GenericObject) => void): void {
|
||||
this.callbacks.onCallTransferStatus = callback;
|
||||
}
|
||||
|
||||
@@ -73,7 +73,6 @@ export type CallbackFunctions = {
|
||||
onAgentAvailabilityChange: (isAgentAvailable: boolean, reason: string) => void;
|
||||
onForcedLogout: () => void;
|
||||
onLoginFailed: (err: GenericObject) => void;
|
||||
onAgentsForCallTransfer: (data: GenericObject) => void;
|
||||
onCallTransferStatus: (data: GenericObject) => void;
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ class IAdapter {
|
||||
registerOnAgentAvailabilityChange(callback: (isAgentAvailable: boolean, reason: string) => void) {callback(false, '')}
|
||||
registerOnForcedLogoutListener(callback:()=>void) {callback()}
|
||||
registerOnLoginFailedListener(callback:()=>void) {callback()}
|
||||
registerOnAgentsForCallTransfer(callback: (data : GenericObject) => void) {callback({})}
|
||||
registerOnCallTransferStatus(callback: (data: GenericObject) => void) {callback({})}
|
||||
acceptCall() {}
|
||||
rejectCall() {}
|
||||
@@ -30,7 +29,9 @@ class IAdapter {
|
||||
getAgentAvailability(): boolean {return false}
|
||||
|
||||
getLatestCallState(): CALL_STATES {return CALL_STATES.IDLE}
|
||||
getAvailableAgentsForCallTransfer() {}
|
||||
getAvailableAgentsForCallTransfer(): Promise<any> {
|
||||
return Promise.resolve(new Response());
|
||||
}
|
||||
transferCallToAgent(data: GenericObject) {console.log("transfer call", data)}
|
||||
}
|
||||
|
||||
|
||||
28
packages/common/lib/types/Request.ts
Normal file
28
packages/common/lib/types/Request.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { RequestKeys } from "@universal-call-sdk/adapter-ameyo/lib/types";
|
||||
|
||||
export type Request = {
|
||||
url: string;
|
||||
method: string;
|
||||
data ?:Record<string, any> | string;
|
||||
requestKey: RequestKeys;
|
||||
requestType ?: RequestType;
|
||||
headers ?: Record<string, any> | null;
|
||||
}
|
||||
|
||||
export enum RequestType {
|
||||
JSON = 'JSON',
|
||||
RAW = 'RAW'
|
||||
}
|
||||
|
||||
export enum METHODS {
|
||||
GET = "GET",
|
||||
POST = "POST",
|
||||
PUT = "PUT",
|
||||
DELETE = "DELETE",
|
||||
}
|
||||
|
||||
export enum ContentType {
|
||||
JSON = "application/json",
|
||||
TEXT = "text/plain",
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
enum RequestType {
|
||||
JSON = 'JSON',
|
||||
RAW = 'RAW'
|
||||
}
|
||||
|
||||
|
||||
export default RequestType;
|
||||
|
||||
103
packages/common/lib/utils/apiHelper.ts
Normal file
103
packages/common/lib/utils/apiHelper.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { RequestKeys } from "@universal-call-sdk/adapter-ameyo/lib/types";
|
||||
import { ContentType, METHODS, Request, RequestType } from "../types/Request";
|
||||
|
||||
const getFetchResponse = async(
|
||||
url: string,
|
||||
method: string,
|
||||
data: any,
|
||||
requestType: RequestType,
|
||||
headers: Record<string, string>
|
||||
): Promise<any> => {
|
||||
const fetchOptions: RequestInit = {
|
||||
method,
|
||||
headers: {
|
||||
"Content-Type":
|
||||
requestType === RequestType.JSON ? ContentType.JSON : ContentType.TEXT,
|
||||
...headers,
|
||||
},
|
||||
...(method !== METHODS.GET && { body: data }),
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await fetch(url, fetchOptions);
|
||||
|
||||
if (!res.ok) {
|
||||
return {
|
||||
error: res,
|
||||
};
|
||||
}
|
||||
|
||||
const contentType = res.headers.get("Content-Type") || "";
|
||||
|
||||
if (contentType.includes(ContentType.JSON)) {
|
||||
return await res.json();
|
||||
} else if (contentType.includes(ContentType.TEXT)) {
|
||||
return await res.text();
|
||||
} else {
|
||||
throw new Error(`Unsupported Content-Type: ${contentType}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Fetch error:", err);
|
||||
return {
|
||||
error: err,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const apiHelper = async({
|
||||
url,
|
||||
method,
|
||||
requestKey,
|
||||
requestType = RequestType.JSON,
|
||||
headers = {},
|
||||
data = {},
|
||||
}: Request) => {
|
||||
const requestPayload =
|
||||
requestType === RequestType.RAW ? data : JSON.stringify(data);
|
||||
|
||||
try {
|
||||
if (requestKey === RequestKeys.GET_AGENTS_FOR_CALL_TRANSFER) {
|
||||
return getFetchResponse(
|
||||
url,
|
||||
method,
|
||||
requestPayload,
|
||||
requestType,
|
||||
headers || {}
|
||||
);
|
||||
}
|
||||
|
||||
const response = await getFetchResponse(
|
||||
url,
|
||||
method,
|
||||
requestPayload,
|
||||
requestType,
|
||||
headers || {}
|
||||
);
|
||||
|
||||
if (response?.error) {
|
||||
return {
|
||||
data: {
|
||||
requestKey,
|
||||
err: response,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
requestKey,
|
||||
response,
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Response error:", error);
|
||||
return {
|
||||
data: {
|
||||
requestKey,
|
||||
err: error,
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export default apiHelper;
|
||||
@@ -1,38 +0,0 @@
|
||||
import RequestType from "../types/RequestType.ts";
|
||||
|
||||
enum MessagingType {
|
||||
GET_RESPONSE_WITHOUT_CORS = 'getResponseWithoutCors',
|
||||
SET_RESPONSE_WITHOUT_CORS = 'setResponseWithoutCors',
|
||||
}
|
||||
|
||||
|
||||
type GetResponseWithoutCors = {
|
||||
url: string;
|
||||
method: string;
|
||||
data ?:Record<string, any> | string;
|
||||
requestKey: string;
|
||||
requestType ?: RequestType;
|
||||
headers ?: Record<string, any> | null;
|
||||
}
|
||||
|
||||
export const getResponseWithoutCors = ({
|
||||
url,
|
||||
method,
|
||||
data = {},
|
||||
requestKey,
|
||||
requestType = RequestType.JSON,
|
||||
headers = null}: GetResponseWithoutCors) => {
|
||||
window.postMessage({
|
||||
type: MessagingType.GET_RESPONSE_WITHOUT_CORS,
|
||||
data: {
|
||||
url,
|
||||
method,
|
||||
data,
|
||||
requestKey,
|
||||
requestType,
|
||||
headers
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export default getResponseWithoutCors;
|
||||
@@ -8,4 +8,22 @@ export type StateType = {
|
||||
isCallConnected: boolean,
|
||||
isCallDisconnected: boolean,
|
||||
callStartTime: number,
|
||||
}
|
||||
|
||||
interface Campaign {
|
||||
campaignId: number;
|
||||
campaignName: string;
|
||||
campaignType: string;
|
||||
}
|
||||
export interface Agent {
|
||||
userId: string;
|
||||
userName: string;
|
||||
userType: string;
|
||||
sessionId: string;
|
||||
crtObjectId: string;
|
||||
isOnBreak: boolean;
|
||||
isOnAutoCall: boolean;
|
||||
isPresent: boolean;
|
||||
statusDescription: string;
|
||||
campaignList: Campaign[];
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import {useEffect, useReducer} from 'react';
|
||||
import {GenericObject, StateType} from "./types.ts";
|
||||
import {Agent, GenericObject, StateType} from "./types.ts";
|
||||
import IAdapter from "@universal-call-sdk/common/lib/Interfaces/IAdapter.ts";
|
||||
import MetricsProcessor from "@universal-call-sdk/common/lib/utils/metricsProcessor.ts";
|
||||
import ClickStreamProcessor from "@universal-call-sdk/common/lib/utils/clickStreamProcessor.ts";
|
||||
@@ -123,10 +123,6 @@ function UseCallSdk({AdapterClass, adapterOptions, metricsConfig, clickStreamCon
|
||||
adapter?.registerOnForcedLogoutListener(callback);
|
||||
}
|
||||
|
||||
function registerOnAgentsForCallTransfer(callback: (data: GenericObject) => void) {
|
||||
adapter?.registerOnAgentsForCallTransfer(callback);
|
||||
}
|
||||
|
||||
function registerOnCallTransferStatus(callback: (data: GenericObject) => void) {
|
||||
adapter?.registerOnCallTransferStatus(callback);
|
||||
}
|
||||
@@ -189,8 +185,8 @@ function UseCallSdk({AdapterClass, adapterOptions, metricsConfig, clickStreamCon
|
||||
return adapter.getLatestCallState();
|
||||
}
|
||||
|
||||
function getAvailableAgentsForCallTransfer() {
|
||||
adapter.getAvailableAgentsForCallTransfer();
|
||||
function getAvailableAgentsForCallTransfer(): Promise<Agent[]> {
|
||||
return adapter.getAvailableAgentsForCallTransfer();
|
||||
}
|
||||
|
||||
function transferCallToAgent(data: GenericObject) {
|
||||
@@ -204,7 +200,6 @@ function UseCallSdk({AdapterClass, adapterOptions, metricsConfig, clickStreamCon
|
||||
registerOnCallDisconnected,
|
||||
registerOnAgentAvailabilityChange,
|
||||
registerOnForcedLogoutListener,
|
||||
registerOnAgentsForCallTransfer,
|
||||
registerOnCallTransferStatus,
|
||||
registerOnLoginFailedListener,
|
||||
acceptCall,
|
||||
|
||||
Reference in New Issue
Block a user