Skip to content

Commit

Permalink
saveAnalyticsDataByRoomId -> getAnalyticsUpdateQueryByRoomId
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Aug 2, 2024
1 parent 7108f91 commit db5e20a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
8 changes: 7 additions & 1 deletion apps/meteor/app/livechat/server/hooks/saveAnalyticsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ callbacks.add(
}

const analyticsData = getAnalyticsData(room, new Date());
await LivechatRooms.saveAnalyticsDataByRoomId(room, message, analyticsData);
const updater = await LivechatRooms.getAnalyticsUpdateQueryByRoomId(room, message, analyticsData);

if (updater.hasChanges()) {
await updater.persist({
_id: room._id,
});
}

return message;
},
Expand Down
38 changes: 8 additions & 30 deletions apps/meteor/server/models/raw/LivechatRooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2046,15 +2046,14 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
room: IOmnichannelRoom,
message: IMessage,
analyticsData: Record<string, string | number | Date> | undefined,
updater: Updater<IOmnichannelRoom> = this.getUpdater(),
) {
const updater = this.getAnalyticsUpdateQuery(analyticsData);

// livechat analytics : update last message timestamps
const visitorLastQuery = room.metrics?.v ? room.metrics.v.lq : room.ts;
const agentLastReply = room.metrics?.servedBy ? room.metrics.servedBy.lr : room.ts;

if (visitorLastQuery > agentLastReply) {
updater.set('metrics.servedBy.lr', message.ts);
return this.getAnalyticsUpdateQuery(analyticsData, updater).set('metrics.servedBy.lr', message.ts);
}

return updater;
Expand All @@ -2064,52 +2063,31 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
room: IOmnichannelRoom,
message: IMessage,
analyticsData: Record<string, string | number | Date> | undefined,
updater: Updater<IOmnichannelRoom> = this.getUpdater(),
) {
const updater = this.getAnalyticsUpdateQuery(analyticsData);

// livechat analytics : update last message timestamps
const visitorLastQuery = room.metrics?.v ? room.metrics.v.lq : room.ts;
const agentLastReply = room.metrics?.servedBy ? room.metrics.servedBy.lr : room.ts;

// update visitor timestamp, only if its new inquiry and not continuing message
if (agentLastReply >= visitorLastQuery) {
updater.set('metrics.v.lq', message.ts);
return this.getAnalyticsUpdateQuery(analyticsData).set('metrics.v.lq', message.ts);
}

return updater;
}

private getAnalyticsUpdateQueryByRoomId(
async getAnalyticsUpdateQueryByRoomId(
room: IOmnichannelRoom,
message: IMessage,
analyticsData: Record<string, string | number | Date> | undefined,
updater: Updater<IOmnichannelRoom> = this.getUpdater(),
) {
return isMessageFromVisitor(message)
? this.getAnalyticsUpdateQueryBySentByVisitor(room, message, analyticsData)
: this.getAnalyticsUpdateQueryBySentByAgent(room, message, analyticsData);
}

async saveAnalyticsDataByRoomId(
room: IOmnichannelRoom,
message: IMessage,
analyticsData?: Record<string, string | number | Date>,
): Promise<void> {
const updater = this.getAnalyticsUpdateQueryByRoomId(room, message, analyticsData);
return updater.persist({ _id: room._id });
? this.getAnalyticsUpdateQueryBySentByVisitor(room, message, analyticsData, updater)
: this.getAnalyticsUpdateQueryBySentByAgent(room, message, analyticsData, updater);
}

// saveAnalyticsDataByRoomIdAndLastMessageFromVisitor(
// room: IOmnichannelRoom,
// message: IMessage,
// analyticsData: Record<string, string | number | Date>,
// ) {
// const updater = this.getAnalyticsUpdateQueryByRoomId(room, message, analyticsData);

// updater.set('v.lastMessageTs', message.ts);

// return updater.persist({ _id: room._id });
// }

getTotalConversationsBetweenDate(t: 'l', date: { gte: Date; lt: Date }, { departmentId }: { departmentId?: string } = {}) {
const query: Filter<IOmnichannelRoom> = {
t,
Expand Down
8 changes: 5 additions & 3 deletions packages/model-typings/src/models/ILivechatRoomsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import type { FindCursor, UpdateResult, AggregationCursor, Document, FindOptions, DeleteResult, Filter } from 'mongodb';

import type { FindPaginated } from '..';
import type { Updater } from '../updater';
import type { IBaseModel } from './IBaseModel';

type Period = {
Expand Down Expand Up @@ -208,11 +209,12 @@ export interface ILivechatRoomsModel extends IBaseModel<IOmnichannelRoom> {
setResponseByRoomId(roomId: string, responseBy: IOmnichannelRoom['responseBy']): Promise<UpdateResult>;
setNotResponseByRoomId(roomId: string): Promise<UpdateResult>;
setAgentLastMessageTs(roomId: string): Promise<UpdateResult>;
saveAnalyticsDataByRoomId(
getAnalyticsUpdateQueryByRoomId(
room: IOmnichannelRoom,
message: IMessage,
analyticsData?: Record<string, string | number | Date>,
): Promise<void>; // Promise<UpdateResult>;
analyticsData: Record<string, string | number | Date> | undefined,
updater?: Updater<IOmnichannelRoom>,
): Promise<Updater<IOmnichannelRoom>>;
getTotalConversationsBetweenDate(t: 'l', date: { gte: Date; lt: Date }, data?: { departmentId: string }): Promise<number>;
getAnalyticsMetricsBetweenDate(
t: 'l',
Expand Down

0 comments on commit db5e20a

Please sign in to comment.