2020-08-28 19:41:08 +00:00
|
|
|
import log from '../../../../utils/log';
|
2022-04-07 14:19:54 +00:00
|
|
|
import { store } from '../../../../lib/store/auxStore';
|
2020-08-28 19:41:08 +00:00
|
|
|
import RocketChat from '../../../../lib/rocketchat';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { inquiryQueueAdd, inquiryQueueRemove, inquiryQueueUpdate, inquiryRequest } from '../../actions/inquiry';
|
2022-03-07 21:16:20 +00:00
|
|
|
import sdk from '../../../../lib/rocketchat/services/sdk';
|
|
|
|
import { IOmnichannelRoom } from '../../../../definitions';
|
2020-07-31 18:22:30 +00:00
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
interface IArgsQueueOmnichannel extends IOmnichannelRoom {
|
|
|
|
type: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IDdpMessage {
|
|
|
|
msg: string;
|
|
|
|
collection: string;
|
|
|
|
id: string;
|
|
|
|
fields: {
|
|
|
|
eventName: string;
|
|
|
|
args: IArgsQueueOmnichannel[];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeListener = (listener: any) => listener.stop();
|
2020-07-31 18:22:30 +00:00
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
let connectedListener: any;
|
|
|
|
let queueListener: any;
|
2020-07-31 18:22:30 +00:00
|
|
|
|
|
|
|
const streamTopic = 'stream-livechat-inquiry-queue-observer';
|
|
|
|
|
|
|
|
export default function subscribeInquiry() {
|
|
|
|
const handleConnection = () => {
|
|
|
|
store.dispatch(inquiryRequest());
|
|
|
|
};
|
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
const handleQueueMessageReceived = (ddpMessage: IDdpMessage) => {
|
2020-07-31 18:22:30 +00:00
|
|
|
const [{ type, ...sub }] = ddpMessage.fields.args;
|
|
|
|
|
|
|
|
// added can be ignored, since it is handled by 'changed' event
|
|
|
|
if (/added/.test(type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the sub isn't on the queue anymore
|
|
|
|
if (sub.status !== 'queued') {
|
|
|
|
// remove it from the queue
|
2022-03-07 21:16:20 +00:00
|
|
|
if (sub._id) {
|
|
|
|
store.dispatch(inquiryQueueRemove(sub._id));
|
|
|
|
}
|
2020-07-31 18:22:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { queued } = store.getState().inquiry;
|
|
|
|
// check if this sub is on the current queue
|
|
|
|
const idx = queued.findIndex(item => item._id === sub._id);
|
|
|
|
if (idx >= 0) {
|
|
|
|
// if it is on the queue let's update
|
|
|
|
store.dispatch(inquiryQueueUpdate(sub));
|
|
|
|
} else {
|
|
|
|
// if it is not on the queue let's add
|
|
|
|
store.dispatch(inquiryQueueAdd(sub));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const stop = () => {
|
|
|
|
if (connectedListener) {
|
|
|
|
connectedListener.then(removeListener);
|
|
|
|
connectedListener = false;
|
|
|
|
}
|
|
|
|
if (queueListener) {
|
|
|
|
queueListener.then(removeListener);
|
|
|
|
queueListener = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
connectedListener = sdk.onStreamData('connected', handleConnection);
|
|
|
|
queueListener = sdk.onStreamData(streamTopic, handleQueueMessageReceived);
|
2020-07-31 18:22:30 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const { user } = store.getState().login;
|
2022-03-07 21:16:20 +00:00
|
|
|
|
|
|
|
if (!user.id) {
|
|
|
|
throw new Error('inquiry: @subscribeInquiry user.id not found');
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:04:41 +00:00
|
|
|
RocketChat.getAgentDepartments(user.id).then(result => {
|
2020-07-31 18:22:30 +00:00
|
|
|
if (result.success) {
|
|
|
|
const { departments } = result;
|
|
|
|
|
|
|
|
if (!departments.length || RocketChat.hasRole('livechat-manager')) {
|
2022-03-07 21:16:20 +00:00
|
|
|
sdk.subscribe(streamTopic, 'public').catch((e: unknown) => console.log(e));
|
2020-07-31 18:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const departmentIds = departments.map(({ departmentId }) => departmentId);
|
2021-09-13 20:41:05 +00:00
|
|
|
departmentIds.forEach(departmentId => {
|
2020-07-31 18:22:30 +00:00
|
|
|
// subscribe to all departments of the agent
|
2022-03-07 21:16:20 +00:00
|
|
|
sdk.subscribe(streamTopic, `department/${departmentId}`).catch((e: unknown) => console.log(e));
|
2020-07-31 18:22:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
stop: () => stop()
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
}
|