diff --git a/services/chat/app/js/Features/Messages/MessageManager.js b/services/chat/app/js/Features/Messages/MessageManager.js index f020d71149..ee5d65099c 100644 --- a/services/chat/app/js/Features/Messages/MessageManager.js +++ b/services/chat/app/js/Features/Messages/MessageManager.js @@ -114,6 +114,7 @@ function _ensureIdsAreObjectIds(query) { export async function duplicateRoomToOtherRoom(sourceRoomId, targetRoomId) { const sourceMessages = await findAllMessagesInRooms([sourceRoomId]) + if (sourceMessages.length === 0) return const targetMessages = sourceMessages.map(comment => { return _ensureIdsAreObjectIds({ room_id: targetRoomId, diff --git a/services/chat/app/js/Features/Threads/ThreadManager.js b/services/chat/app/js/Features/Threads/ThreadManager.js index fc15b27b24..f327b86ff7 100644 --- a/services/chat/app/js/Features/Threads/ThreadManager.js +++ b/services/chat/app/js/Features/Threads/ThreadManager.js @@ -16,6 +16,7 @@ export async function cloneThreads(sourceProjectId, targetProjectId) { .find({ project_id: sourceProjectId, thread_id: { $exists: true } }) .toArray() const mapping = [] + if (rooms.length === 0) return mapping await db.rooms.insertMany( rooms.map(room => { const from = room._id diff --git a/services/chat/test/acceptance/js/CloningCommentThreadsTests.js b/services/chat/test/acceptance/js/CloningCommentThreadsTests.js index fa4c4e23f7..951bda2d1c 100644 --- a/services/chat/test/acceptance/js/CloningCommentThreadsTests.js +++ b/services/chat/test/acceptance/js/CloningCommentThreadsTests.js @@ -122,6 +122,32 @@ describe('Cloning comment threads', async function () { expectValidationErrorRaw(response, 404, 'projectId') }) }) + + describe('with a thread whose room has no messages', async function () { + before(async function () { + this.emptyThreadId = new ObjectId().toString() + const { response: deleteResponse } = await ChatClient.deleteMessage( + projectId, + this.emptyThreadId, + new ObjectId().toString() + ) + expect(deleteResponse.statusCode).to.equal(204) + + const { + response: { body: result, statusCode }, + } = await ChatClient.duplicateCommentThreads(projectId, [ + this.emptyThreadId, + ]) + this.result = result + expect(statusCode).to.equal(200) + }) + + it('should duplicate the thread', function () { + expect(this.result.newThreads[this.emptyThreadId]).to.have.property( + 'duplicateId' + ) + }) + }) }) describe('Cloning comment threads to another project', async function () { @@ -189,4 +215,21 @@ describe('Cloning comment threads to another project', async function () { expectValidationErrorRaw(response, 400, 'targetProjectId') }) }) + + describe('with a source project that has no comment threads', function () { + it('should clone no threads into the target project', async function () { + const emptySourceProjectId = new ObjectId().toString() + const freshTargetProjectId = new ObjectId().toString() + const { response } = await ChatClient.cloneCommentThreads( + emptySourceProjectId, + freshTargetProjectId + ) + expect(response.statusCode).to.equal(204) + + const { response: getResponse, body: threads } = + await ChatClient.getThreads(freshTargetProjectId) + expect(getResponse.statusCode).to.equal(200) + expect(threads).to.deep.equal({}) + }) + }) })