Skip to content

Commit

Permalink
Add activity to room
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Sep 11, 2023
1 parent 1f867ea commit 4eed56d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
16 changes: 10 additions & 6 deletions apps/meteor/app/livechat/server/hooks/markRoomResponded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ callbacks.add(
if (message.token) {
return message;
}
if (room.responseBy) {
await LivechatRooms.setAgentLastMessageTs(room._id);
}

// Return MMMM-YY from moment
// Return YYYY-MM from moment
const monthYear = moment().format('YYYY-MM');
if (!(await LivechatVisitors.isVisitorActiveOnPeriod(room.v._id, monthYear))) {
await LivechatVisitors.markVisitorActiveForPeriod(room.v._id, monthYear);
await Promise.all([
LivechatVisitors.markVisitorActiveForPeriod(room.v._id, monthYear),
LivechatRooms.markVisitorActiveForPeriod(room._id, monthYear),
]);
}

if (room.responseBy) {
await LivechatRooms.setAgentLastMessageTs(room._id);
}

// check if room is yet awaiting for response
if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.waitingResponse)) {
if (!room.waitingResponse) {
return message;
}

Expand Down
17 changes: 17 additions & 0 deletions apps/meteor/server/models/raw/LivechatRooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,23 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
return this.updateOne(query, update);
}

markVisitorActiveForPeriod(rid: string, period: string): Promise<UpdateResult> {
const query = {
_id: rid,
};

const update = {
$push: {
'v.activity': {
$each: [period],
$slice: -12,
},
},
};

return this.updateOne(query, update);
}

async unsetAllPredictedVisitorAbandonment(): Promise<void> {
throw new Error('Method not implemented.');
}
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/models/raw/LivechatVisitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class LivechatVisitorsRaw extends BaseRaw<ILivechatVisitor> implements IL
{ key: { username: 1 } },
{ key: { 'contactMananger.username': 1 }, sparse: true },
{ key: { 'livechatData.$**': 1 } },
{ key: { activity: 1 } },
{ key: { activity: 1 }, partialFilterExpression: { activity: { $exists: true } } },
];
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core-typings/src/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ export enum OmnichannelSourceType {

export interface IOmnichannelGenericRoom extends Omit<IRoom, 'default' | 'featured' | 'broadcast' | ''> {
t: 'l' | 'v';
v: Pick<ILivechatVisitor, '_id' | 'username' | 'status' | 'name' | 'token'> & { lastMessageTs?: Date; phone?: string };
v: Pick<ILivechatVisitor, '_id' | 'username' | 'status' | 'name' | 'token'> & {
lastMessageTs?: Date;
phone?: string;
activity?: string[];
};
email?: {
// Data used when the room is created from an email, via email Integration.
inbox: string;
Expand Down
1 change: 1 addition & 0 deletions packages/model-typings/src/models/ILivechatRoomsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,5 @@ export interface ILivechatRoomsModel extends IBaseModel<IOmnichannelRoom> {
setVisitorInactivityInSecondsById(roomId: string, visitorInactivity: any): Promise<UpdateResult>;
changeVisitorByRoomId(roomId: string, visitor: { _id: string; username: string; token: string }): Promise<UpdateResult>;
unarchiveOneById(roomId: string): Promise<UpdateResult>;
markVisitorActiveForPeriod(rid: string, period: string): Promise<UpdateResult>;
}

0 comments on commit 4eed56d

Please sign in to comment.