-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Updating path
responseBy
would create a conflict on `responseB…
…y` (#33412)
- Loading branch information
Showing
3 changed files
with
155 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rocket.chat/meteor": patch | ||
--- | ||
|
||
Fixes a logical error when updating the `responseBy` property of a room while `waitingResponse` property was still defined. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
apps/meteor/tests/unit/server/livechat/hooks/markRoomResponded.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it, beforeEach } from 'mocha'; | ||
import proxyquire from 'proxyquire'; | ||
import Sinon from 'sinon'; | ||
|
||
const models = { | ||
LivechatVisitors: { isVisitorActiveOnPeriod: Sinon.stub(), markVisitorActiveForPeriod: Sinon.stub() }, | ||
LivechatInquiry: { markInquiryActiveForPeriod: Sinon.stub() }, | ||
LivechatRooms: { | ||
getVisitorActiveForPeriodUpdateQuery: Sinon.stub(), | ||
getAgentLastMessageTsUpdateQuery: Sinon.stub(), | ||
getResponseByRoomIdUpdateQuery: Sinon.stub(), | ||
}, | ||
}; | ||
|
||
const { markRoomResponded } = proxyquire.load('../../../../../app/livechat/server/hooks/markRoomResponded.ts', { | ||
'../../../../lib/callbacks': { callbacks: { add: Sinon.stub(), priority: { HIGH: 'high' } } }, | ||
'../../../lib/server/lib/notifyListener': { notifyOnLivechatInquiryChanged: Sinon.stub() }, | ||
'@rocket.chat/models': models, | ||
}); | ||
|
||
describe('markRoomResponded', () => { | ||
beforeEach(() => { | ||
models.LivechatVisitors.isVisitorActiveOnPeriod.reset(); | ||
models.LivechatVisitors.markVisitorActiveForPeriod.reset(); | ||
models.LivechatInquiry.markInquiryActiveForPeriod.reset(); | ||
models.LivechatRooms.getVisitorActiveForPeriodUpdateQuery.reset(); | ||
models.LivechatRooms.getAgentLastMessageTsUpdateQuery.reset(); | ||
models.LivechatRooms.getResponseByRoomIdUpdateQuery.reset(); | ||
}); | ||
|
||
it('should return void if message is system message', async () => { | ||
const message = { | ||
t: 'livechat-started', | ||
}; | ||
|
||
const room = {}; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.undefined; | ||
}); | ||
|
||
it('should return void if message is edited message', async () => { | ||
const message = { | ||
editedAt: new Date(), | ||
editedBy: { _id: '123' }, | ||
}; | ||
|
||
const room = {}; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.undefined; | ||
}); | ||
|
||
it('should return void if message is from visitor', async () => { | ||
const message = { | ||
token: 'token', | ||
}; | ||
|
||
const room = {}; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.undefined; | ||
}); | ||
|
||
it('should try to mark visitor as active for current period', async () => { | ||
const message = {}; | ||
const room = { v: { _id: '1234' } }; | ||
|
||
await markRoomResponded(message, room, {}); | ||
|
||
expect(models.LivechatVisitors.markVisitorActiveForPeriod.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should try to mark inquiry as active for current period when room.v.activity doesnt include current period', async () => { | ||
const message = {}; | ||
const room = { v: { activity: [] } }; | ||
|
||
models.LivechatInquiry.markInquiryActiveForPeriod.resolves({}); | ||
|
||
await markRoomResponded(message, room, {}); | ||
|
||
expect(models.LivechatInquiry.markInquiryActiveForPeriod.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should return room.responseBy when room is not waiting for response', async () => { | ||
const message = {}; | ||
const room = { v: { _id: '1234' }, waitingResponse: false, responseBy: { _id: '1234' } }; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.equal(room.responseBy); | ||
expect(models.LivechatRooms.getAgentLastMessageTsUpdateQuery.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should try to update the lastMessageTs property when a room was already answered by an agent', async () => { | ||
const message = { u: { _id: '1234', username: 'username' }, ts: new Date() }; | ||
const room = { responseBy: { _id: '1234' }, v: { _id: '1234' } }; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.deep.equal(room.responseBy); | ||
expect(models.LivechatRooms.getAgentLastMessageTsUpdateQuery.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should add a new responseBy object when room is waiting for response', async () => { | ||
const message = { u: { _id: '1234', username: 'username' }, ts: new Date() }; | ||
const room = { waitingResponse: true, v: { _id: '1234' } }; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.deep.equal({ _id: '1234', username: 'username', firstResponseTs: message.ts, lastMessageTs: message.ts }); | ||
expect(models.LivechatRooms.getResponseByRoomIdUpdateQuery.calledOnce).to.be.true; | ||
expect(models.LivechatRooms.getResponseByRoomIdUpdateQuery.getCall(0).args[0]).to.be.deep.equal({ | ||
_id: '1234', | ||
lastMessageTs: message.ts, | ||
firstResponseTs: message.ts, | ||
username: 'username', | ||
}); | ||
}); | ||
|
||
// This should never happpen on the wild, checking because of a data inconsistency bug found | ||
it('should update only the lastMessageTs property when a room has both waitingResponse and responseBy properties', async () => { | ||
const message = { u: { _id: '1234', username: 'username' }, ts: new Date() }; | ||
const room = { | ||
waitingResponse: true, | ||
responseBy: { _id: '1234', username: 'username', firstResponseTs: new Date() }, | ||
v: { _id: '1234' }, | ||
}; | ||
|
||
const res = await markRoomResponded(message, room, {}); | ||
|
||
expect(res).to.be.deep.equal({ | ||
_id: '1234', | ||
username: 'username', | ||
firstResponseTs: room.responseBy.firstResponseTs, | ||
lastMessageTs: message.ts, | ||
}); | ||
}); | ||
}); |