Skip to content
Snippets Groups Projects
Unverified Commit d742b252 authored by Kevin Aleman's avatar Kevin Aleman Committed by GitHub
Browse files

refactor: Add `projection` and change promise calls to `Promise.all` on...

refactor: Add `projection` and change promise calls to `Promise.all` on LivechatAgentActivityMonitor (#32036)
parent c8b11da2
No related branches found
No related tags found
No related merge requests found
...@@ -52,21 +52,19 @@ export class LivechatAgentActivityMonitor { ...@@ -52,21 +52,19 @@ export class LivechatAgentActivityMonitor {
// TODO use service event socket.connected instead // TODO use service event socket.connected instead
Meteor.onConnection((connection: unknown) => this._handleMeteorConnection(connection as ISocketConnection)); Meteor.onConnection((connection: unknown) => this._handleMeteorConnection(connection as ISocketConnection));
callbacks.add('livechat.agentStatusChanged', this._handleAgentStatusChanged); callbacks.add('livechat.agentStatusChanged', this._handleAgentStatusChanged);
callbacks.add('livechat.setUserStatusLivechat', async (...args) => { callbacks.add('livechat.setUserStatusLivechat', (...args) => {
return this._handleUserStatusLivechatChanged(...args); return this._handleUserStatusLivechatChanged(...args);
}); });
this._started = true; this._started = true;
} }
async _startMonitoring(): Promise<void> { async _startMonitoring(): Promise<void> {
await this.scheduler.add(this._name, '0 0 * * *', async () => this._updateActiveSessions()); await this.scheduler.add(this._name, '0 0 * * *', () => this._updateActiveSessions());
} }
async _updateActiveSessions(): Promise<void> { async _updateActiveSessions(): Promise<void> {
const openLivechatAgentSessions = await LivechatAgentActivity.findOpenSessions(); const openLivechatAgentSessions = LivechatAgentActivity.findOpenSessions();
if (!(await openLivechatAgentSessions.count())) {
return;
}
const today = moment(new Date()); const today = moment(new Date());
const startedAt = new Date(today.year(), today.month(), today.date()); const startedAt = new Date(today.year(), today.month(), today.date());
for await (const session of openLivechatAgentSessions) { for await (const session of openLivechatAgentSessions) {
...@@ -74,15 +72,11 @@ export class LivechatAgentActivityMonitor { ...@@ -74,15 +72,11 @@ export class LivechatAgentActivityMonitor {
const stoppedAt = new Date(startDate.year(), startDate.month(), startDate.date(), 23, 59, 59); const stoppedAt = new Date(startDate.year(), startDate.month(), startDate.date(), 23, 59, 59);
const data = { ...formatDate(startDate.toDate()), agentId: session.agentId }; const data = { ...formatDate(startDate.toDate()), agentId: session.agentId };
const availableTime = moment(stoppedAt).diff(moment(new Date(session.lastStartedAt)), 'seconds'); const availableTime = moment(stoppedAt).diff(moment(new Date(session.lastStartedAt)), 'seconds');
await LivechatAgentActivity.updateLastStoppedAt({
...data, await Promise.all([
availableTime, LivechatAgentActivity.updateLastStoppedAt({ ...data, availableTime, lastStoppedAt: stoppedAt }),
lastStoppedAt: stoppedAt, LivechatAgentActivity.updateServiceHistory({ ...data, serviceHistory: { startedAt: session.lastStartedAt, stoppedAt } }),
}); ]);
await LivechatAgentActivity.updateServiceHistory({
...data,
serviceHistory: { startedAt: session.lastStartedAt, stoppedAt },
});
await this._createOrUpdateSession(session.agentId, startedAt); await this._createOrUpdateSession(session.agentId, startedAt);
} }
} }
...@@ -96,7 +90,9 @@ export class LivechatAgentActivityMonitor { ...@@ -96,7 +90,9 @@ export class LivechatAgentActivityMonitor {
if (!session) { if (!session) {
return; return;
} }
const user = await Users.findOneById<ILivechatAgent>(session.userId); const user = await Users.findOneById<Pick<ILivechatAgent, '_id' | 'statusLivechat' | 'status'>>(session.userId, {
projection: { _id: 1, status: 1, statusLivechat: 1 },
});
if (user && user.status !== 'offline' && user.statusLivechat === 'available') { if (user && user.status !== 'offline' && user.statusLivechat === 'available') {
await this._createOrUpdateSession(user._id); await this._createOrUpdateSession(user._id);
} }
...@@ -112,7 +108,7 @@ export class LivechatAgentActivityMonitor { ...@@ -112,7 +108,7 @@ export class LivechatAgentActivityMonitor {
return; return;
} }
const user = await Users.findOneById<ILivechatAgent>(userId); const user = await Users.findOneById<Pick<ILivechatAgent, '_id' | 'statusLivechat'>>(userId, { projection: { statusLivechat: 1 } });
if (!user || user.statusLivechat !== 'available') { if (!user || user.statusLivechat !== 'available') {
return; return;
} }
...@@ -129,7 +125,7 @@ export class LivechatAgentActivityMonitor { ...@@ -129,7 +125,7 @@ export class LivechatAgentActivityMonitor {
return; return;
} }
const user = await Users.findOneById(userId); const user = await Users.findOneById<Pick<ILivechatAgent, '_id' | 'status'>>(userId, { projection: { status: 1 } });
if (user && user.status === 'offline') { if (user && user.status === 'offline') {
return; return;
} }
...@@ -158,16 +154,13 @@ export class LivechatAgentActivityMonitor { ...@@ -158,16 +154,13 @@ export class LivechatAgentActivityMonitor {
const stoppedAt = new Date(); const stoppedAt = new Date();
const availableTime = moment(stoppedAt).diff(moment(new Date(livechatSession.lastStartedAt)), 'seconds'); const availableTime = moment(stoppedAt).diff(moment(new Date(livechatSession.lastStartedAt)), 'seconds');
await LivechatAgentActivity.updateLastStoppedAt({ await Promise.all([
agentId, LivechatAgentActivity.updateLastStoppedAt({ agentId, date, availableTime, lastStoppedAt: stoppedAt }),
date, LivechatAgentActivity.updateServiceHistory({
availableTime, agentId,
lastStoppedAt: stoppedAt, date,
}); serviceHistory: { startedAt: livechatSession.lastStartedAt, stoppedAt },
await LivechatAgentActivity.updateServiceHistory({ }),
agentId, ]);
date,
serviceHistory: { startedAt: livechatSession.lastStartedAt, stoppedAt },
});
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment