File: //home/arjun/projects/buyercall/buyercall/assets/vue/widgets/CommWidget/store/call/actions.js
import {
CALL_ACTION_START_CALL,
CALL_ACTION_GET_CALL_STATUS,
CALL_ACTION_RESET_STATE,
CALL_MUTATION_SET_START_CALL_API_LOADING_STATUS,
CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE,
CALL_MUTATION_SET_CALL_STATUS_API_LOADING_STATUS,
CALL_MUTATION_SET_CALL_STATUS_API_ERROR_MESSAGE,
CALL_MUTATION_SET_CALL_STATUS_TIMERID,
CALL_MUTATION_SET_LEAD_ID,
CALL_MUTATION_SET_CALL_CONNECT_STATUS,
CALL_MUTATION_RESET,
ROOT_ACTION_RESET_STATE,
ROOT_SELECT_WIDGET_ID,
} from '../../constants';
import ExternalWidgetService from '../../../../service/externalWidgetService';
import _ from 'lodash';
export default {
// Start Call
async [CALL_ACTION_START_CALL](
{ commit, dispatch, state, rootGetters },
payload,
) {
const CALL_POLL_TIMEOUT = 360; //360
let pollCounter = 0;
const ERR_NO_AGENTS_MSG =
'Sadly, no agents are available at the moment. Please try again later.';
const ERR_TOOLONG_MSG =
'The call seems to be taking longer than usual. An agent might not be available right now. We will call you back as soon as possible.';
const ERR_GENERIC_MSG =
"We're sorry, your request cannot be fulfilled at the moment. Please try again later.";
commit(CALL_MUTATION_SET_START_CALL_API_LOADING_STATUS, true);
const {
data: {
data: { success: SuccessResponse = false, callId = null, code = null },
message,
success,
},
} = await ExternalWidgetService.connectLeadToAgent(
payload,
rootGetters[`root/${ROOT_SELECT_WIDGET_ID}`],
);
if (success) {
if (SuccessResponse) {
commit(CALL_MUTATION_SET_LEAD_ID, callId);
const timerId = setInterval(() => {
if (pollCounter > CALL_POLL_TIMEOUT) {
commit(
CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE,
ERR_TOOLONG_MSG,
);
clearInterval(state.callStatusTimerID);
commit(CALL_MUTATION_SET_CALL_STATUS_TIMERID, null);
return;
}
pollCounter += 2; //2
dispatch(CALL_ACTION_GET_CALL_STATUS, state.leadID);
}, 2000);
commit(CALL_MUTATION_SET_CALL_STATUS_TIMERID, timerId);
} else {
if (code == 'ERR_NO_AGENTS') {
commit(
CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE,
ERR_NO_AGENTS_MSG,
);
} else {
commit(
CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE,
ERR_GENERIC_MSG,
);
}
}
} else {
commit(CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE, message);
}
commit(CALL_MUTATION_SET_START_CALL_API_LOADING_STATUS, false);
},
// Get call status
async [CALL_ACTION_GET_CALL_STATUS]({ commit, state, dispatch }, payload) {
const ERR_GENERIC_MSG =
"We're sorry, your request cannot be fulfilled at the moment. Please try again later.";
commit(CALL_MUTATION_SET_CALL_STATUS_API_LOADING_STATUS, true);
const {
data: {
data: { callConnect = false, error = false },
message,
success,
},
} = await ExternalWidgetService.getLeadToAgentCallStatus(payload);
if (success) {
if (callConnect) {
commit(CALL_MUTATION_SET_CALL_CONNECT_STATUS, true);
clearInterval(state.callStatusTimerID);
commit(CALL_MUTATION_SET_CALL_STATUS_TIMERID, null);
setTimeout(() => {
dispatch(`root/${ROOT_ACTION_RESET_STATE}`, null, {
root: true,
});
}, 3000);
} else if (error) {
commit(CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE, ERR_GENERIC_MSG);
clearInterval(state.callStatusTimerID);
commit(CALL_MUTATION_SET_CALL_STATUS_TIMERID, null);
}
} else {
clearInterval(state.callStatusTimerID);
commit(CALL_MUTATION_SET_CALL_STATUS_TIMERID, null);
commit(CALL_MUTATION_SET_START_CALL_API_ERROR_MESSAGE, ERR_GENERIC_MSG);
}
commit(CALL_MUTATION_SET_CALL_STATUS_API_LOADING_STATUS, false);
},
// Reset call state
[CALL_ACTION_RESET_STATE]({ commit }) {
commit(CALL_MUTATION_RESET);
},
};