Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/popular-cars-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rocket.chat/model-typings': patch
'@rocket.chat/models': patch
'@rocket.chat/meteor': patch
---

Fixes an issue with Omnichannel inquiries where multiple instances could take the same inquiry from the queue resulting in the same room being assined to multiple agents.
6 changes: 5 additions & 1 deletion apps/meteor/app/livechat/server/lib/RoutingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ export const RoutingManager: Routing = {
return cbRoom;
}

await LivechatInquiry.takeInquiry(_id);
const result = await LivechatInquiry.takeInquiry(_id, inquiry.lockedAt);
if (result.modifiedCount === 0) {
logger.error('Failed to take inquiry, could not match lockedAt', { inquiryId: _id, lockedAt: inquiry.lockedAt });
throw new Error('error-taking-inquiry-lockedAt-mismatch');
}

logger.info(`Inquiry ${inquiry._id} taken by agent ${agent.agentId}`);

Expand Down
2 changes: 1 addition & 1 deletion packages/model-typings/src/models/ILivechatInquiryModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ILivechatInquiryModel extends IBaseModel<ILivechatInquiryRecord
}): Promise<(Pick<ILivechatInquiryRecord, '_id' | 'rid' | 'name' | 'ts' | 'status' | 'department'> & { position: number })[]>;
removeByRoomId(rid: string, options?: DeleteOptions): Promise<DeleteResult>;
getQueuedInquiries(options?: FindOptions<ILivechatInquiryRecord>): FindCursor<ILivechatInquiryRecord>;
takeInquiry(inquiryId: string): Promise<void>;
takeInquiry(inquiryId: string, lockedAt?: Date): Promise<UpdateResult>;
openInquiry(inquiryId: string): Promise<UpdateResult>;
queueInquiry(inquiryId: string, lastMessage?: IMessage, defaultAgent?: SelectedAgent | null): Promise<ILivechatInquiryRecord | null>;
queueInquiryAndRemoveDefaultAgent(inquiryId: string): Promise<UpdateResult>;
Expand Down
11 changes: 7 additions & 4 deletions packages/models/src/models/LivechatInquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import type {
import { BaseRaw } from './BaseRaw';
import { readSecondaryPreferred } from '../readSecondaryPreferred';

const { INQUIRY_LOCK_TIMEOUT = '10000' } = process.env;

export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implements ILivechatInquiryModel {
constructor(db: Db, trash?: Collection<RocketChatRecordDeleted<ILivechatInquiryRecord>>) {
super(db, 'livechat_inquiry', trash);
Expand Down Expand Up @@ -191,7 +193,7 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
{
locked: true,
lockedAt: {
$lte: new Date(date.getTime() - 5000),
$lte: new Date(date.getTime() - parseInt(INQUIRY_LOCK_TIMEOUT)),
},
},
{
Expand All @@ -202,7 +204,7 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
{
$set: {
locked: true,
// apply 5 secs lock lifetime
// apply INQUIRY_LOCK_TIMEOUT secs lock lifetime
lockedAt: new Date(),
},
},
Expand Down Expand Up @@ -314,10 +316,11 @@ export class LivechatInquiryRaw extends BaseRaw<ILivechatInquiryRecord> implemen
return this.find({ status: LivechatInquiryStatus.QUEUED }, options);
}

async takeInquiry(inquiryId: string): Promise<void> {
await this.updateOne(
takeInquiry(inquiryId: string, lockedAt?: Date): Promise<UpdateResult> {
return this.updateOne(
{
_id: inquiryId,
...(lockedAt && { lockedAt }),
},
{
$set: { status: LivechatInquiryStatus.TAKEN, takenAt: new Date() },
Expand Down
Loading