NTP-27557 | Handling api calls together
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import IAdapter from "@universal-call-sdk/common/lib/Interfaces/IAdapter";
|
||||
import GenericFunction from "@universal-call-sdk/common/lib/types/GenericFunction";
|
||||
import GenericObject from "@universal-call-sdk/common/lib/types/GenericObject";
|
||||
import {
|
||||
AmeyoInitializationOptions,
|
||||
CALL_STATES, CallbackFunctions,
|
||||
CallTransferData,
|
||||
RequestKeys,
|
||||
SipAccountInfo,
|
||||
StateType
|
||||
} from "./types";
|
||||
@@ -143,8 +141,25 @@ class AmeyoAdapter implements IAdapter {
|
||||
};
|
||||
|
||||
private initializeAmeyo = async (): Promise<void> => {
|
||||
const res = await loginInAmeyo(this.userName.toLowerCase(), this.password);
|
||||
if (res) this.handleApiResponse(res, this.handleLoginResponse);
|
||||
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> => {
|
||||
@@ -204,16 +219,8 @@ class AmeyoAdapter implements IAdapter {
|
||||
});
|
||||
}
|
||||
|
||||
private handleApiResponse = async (
|
||||
payload: GenericObject,
|
||||
apiHandler: GenericFunction
|
||||
): Promise<void> => {
|
||||
apiHandler(payload);
|
||||
this.trackApiMetrics(payload);
|
||||
};
|
||||
|
||||
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',
|
||||
@@ -225,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',
|
||||
@@ -240,7 +245,7 @@ class AmeyoAdapter implements IAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private handleLoginResponse = async (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);
|
||||
@@ -250,12 +255,13 @@ class AmeyoAdapter implements IAdapter {
|
||||
const sessionId = payload?.data?.response?.userSessionInfo?.sessionId;
|
||||
this.sessionId = sessionId;
|
||||
const res = await getSipAccountInfo(sessionId, this.userName?.toLowerCase());
|
||||
this.handleApiResponse(res, this.handleSipAccountInfo);
|
||||
this.trackApiMetrics(res);
|
||||
registerEventProcessor(this.eventListenerUrl, sessionId);
|
||||
maintainHeartbeat(this.sessionId, window?.listenerName || '', 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
private handleSipAccountInfo = async (payload: GenericObject): Promise<void> => {
|
||||
private handleSipAccountInfo = async (payload: GenericObject): Promise<GenericObject> => {
|
||||
const response = payload?.data?.response;
|
||||
this.initializeSipStack({
|
||||
accountName: response?.accountName,
|
||||
@@ -266,31 +272,33 @@ class AmeyoAdapter implements IAdapter {
|
||||
this.sipAccountInfo = payload?.data?.response;
|
||||
console.log('sip account info', this.sipAccountInfo)
|
||||
const res = await getCampaignId(this.sessionId);
|
||||
this.handleApiResponse(res, this.handleCampaignId);
|
||||
this.trackApiMetrics(res);
|
||||
await setAutoStatus(this.sessionId);
|
||||
return res;
|
||||
}
|
||||
|
||||
private handleCampaignId = async (payload: GenericObject): Promise<void> => {
|
||||
this.campaignId = payload?.data?.response?.campaignInfos?.[0]?.campaignId;
|
||||
const res = await attachOmniqueService(this.sessionId, this.userName.toLowerCase(), this.campaignId);
|
||||
this.handleApiResponse(res, this.handleOmniQueueService);
|
||||
this.trackApiMetrics(res);
|
||||
}
|
||||
|
||||
private handleAgentAvailable = (_: GenericObject): void => {
|
||||
private handleAgentAvailable = (): void => {
|
||||
setAutoStatus(this.sessionId);
|
||||
}
|
||||
|
||||
private handleAgentBreak = (_: GenericObject): void => {
|
||||
private handleAgentBreak = (): void => {
|
||||
setAutoStatus(this.sessionId);
|
||||
}
|
||||
|
||||
private handleOmniQueueService = async (_: GenericObject): Promise<void> => {
|
||||
private handleOmniQueueService = async (): Promise<void> => {
|
||||
const res = await selectCampaign(this.sessionId, this.userName.toLowerCase(), this.campaignId);
|
||||
this.handleApiResponse(res, this.handleCampaignSelection);
|
||||
this.trackApiMetrics(res);
|
||||
}
|
||||
|
||||
private handleCampaignSelection = async (_: GenericObject): 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});
|
||||
@@ -348,12 +356,14 @@ class AmeyoAdapter implements IAdapter {
|
||||
|
||||
public async setOnBreak(): Promise<void> {
|
||||
const res = await setAgentOnBreak(this.sessionId);
|
||||
this.handleApiResponse(res, this.handleAgentBreak);
|
||||
this.trackApiMetrics(res);
|
||||
this.handleAgentBreak();
|
||||
}
|
||||
|
||||
public async setAvailable(): Promise<void> {
|
||||
const res = await setAgentActive(this.sessionId);
|
||||
this.handleApiResponse(res, this.handleAgentAvailable);
|
||||
this.trackApiMetrics(res);
|
||||
this.handleAgentAvailable();
|
||||
}
|
||||
|
||||
public muteCall(): void {
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import GenericObject from "./GenericObject";
|
||||
|
||||
type GenericFunction =
|
||||
| ((payload: GenericObject) => void)
|
||||
| ((payload: GenericObject) => Promise<void>);
|
||||
|
||||
export default GenericFunction;
|
||||
@@ -21,3 +21,8 @@ export enum METHODS {
|
||||
DELETE = "DELETE",
|
||||
}
|
||||
|
||||
export enum ContentType {
|
||||
JSON = "application/json",
|
||||
TEXT = "text/plain",
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import { RequestKeys } from "@universal-call-sdk/adapter-ameyo/lib/types";
|
||||
import { METHODS, Request, RequestType } from "../types/Request";
|
||||
|
||||
const handleError = (context: string, error: unknown): void => {
|
||||
console.error(`[${context}]`, error);
|
||||
};
|
||||
import { ContentType, METHODS, Request, RequestType } from "../types/Request";
|
||||
|
||||
const getFetchResponse = async(
|
||||
url: string,
|
||||
@@ -16,7 +12,7 @@ const getFetchResponse = async(
|
||||
method,
|
||||
headers: {
|
||||
"Content-Type":
|
||||
requestType === RequestType.JSON ? "application/json" : "text/plain",
|
||||
requestType === RequestType.JSON ? ContentType.JSON : ContentType.TEXT,
|
||||
...headers,
|
||||
},
|
||||
...(method !== METHODS.GET && { body: data }),
|
||||
@@ -26,21 +22,25 @@ const getFetchResponse = async(
|
||||
const res = await fetch(url, fetchOptions);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP error! Status: ${res.status} ${res.statusText}`);
|
||||
return {
|
||||
error: res,
|
||||
};
|
||||
}
|
||||
|
||||
const contentType = res.headers.get("Content-Type") || "";
|
||||
|
||||
if (contentType.includes("application/json")) {
|
||||
if (contentType.includes(ContentType.JSON)) {
|
||||
return await res.json();
|
||||
} else if (contentType.includes("text/plain")) {
|
||||
} else if (contentType.includes(ContentType.TEXT)) {
|
||||
return await res.text();
|
||||
} else {
|
||||
throw new Error(`Unsupported Content-Type: ${contentType}`);
|
||||
}
|
||||
} catch (err) {
|
||||
handleError("Fetch error:", err);
|
||||
return null;
|
||||
console.error("Fetch error:", err);
|
||||
return {
|
||||
error: err,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -74,15 +74,29 @@ export const apiHelper = async({
|
||||
headers || {}
|
||||
);
|
||||
|
||||
if (response?.error) {
|
||||
return {
|
||||
data: {
|
||||
requestKey,
|
||||
err: response,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
requestKey,
|
||||
response,
|
||||
},
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
handleError("Response error:", error);
|
||||
return null;
|
||||
console.error("Response error:", error);
|
||||
return {
|
||||
data: {
|
||||
requestKey,
|
||||
err: error,
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user