From dd2967ce04220f67258f39cc33806bceb927bb78 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 12 Jan 2024 14:17:11 -0600 Subject: [PATCH] chore: Make room closing process more sequential (#31435) --- apps/meteor/app/livechat/server/api/v1/room.ts | 4 ++++ .../app/livechat/server/lib/LivechatTyped.ts | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/apps/meteor/app/livechat/server/api/v1/room.ts b/apps/meteor/app/livechat/server/api/v1/room.ts index 23d7fe2c507a..f610b9a9d3de 100644 --- a/apps/meteor/app/livechat/server/api/v1/room.ts +++ b/apps/meteor/app/livechat/server/api/v1/room.ts @@ -416,6 +416,10 @@ API.v1.addRoute( throw new Error('error-invalid-room'); } + if (!room.open) { + throw new Error('room-closed'); + } + if (!(await Omnichannel.isWithinMACLimit(room))) { throw new Error('error-mac-limit-reached'); } diff --git a/apps/meteor/app/livechat/server/lib/LivechatTyped.ts b/apps/meteor/app/livechat/server/lib/LivechatTyped.ts index ea508b047882..5e9c08fcc1ef 100644 --- a/apps/meteor/app/livechat/server/lib/LivechatTyped.ts +++ b/apps/meteor/app/livechat/server/lib/LivechatTyped.ts @@ -290,11 +290,17 @@ class LivechatClass { this.logger.debug(`Updating DB for room ${room._id} with close data`); - await Promise.all([ - LivechatRooms.closeRoomById(rid, closeData), - LivechatInquiry.removeByRoomId(rid), - Subscriptions.removeByRoomId(rid), - ]); + const removedInquiry = await LivechatInquiry.removeByRoomId(rid); + if (removedInquiry && removedInquiry.deletedCount !== 1) { + throw new Error('Error removing inquiry'); + } + + const updatedRoom = await LivechatRooms.closeRoomById(rid, closeData); + if (!updatedRoom || updatedRoom.modifiedCount !== 1) { + throw new Error('Error closing room'); + } + + await Subscriptions.removeByRoomId(rid); this.logger.debug(`DB updated for room ${room._id}`);