Skip to content

Commit

Permalink
refactor(omnichannel): replace create and find by findAndUpdate (#32773)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Jul 12, 2024
1 parent 753b08e commit 0a46a11
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
41 changes: 34 additions & 7 deletions apps/meteor/app/livechat/server/lib/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '@rocket.chat/models';
import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { ObjectId } from 'mongodb';

import { callbacks } from '../../../../lib/callbacks';
import { validateEmail as validatorFunc } from '../../../../lib/emailValidator';
Expand Down Expand Up @@ -124,13 +125,26 @@ export const createLivechatRoom = async <
...extraRoomInfo,
} as InsertionModel<IOmnichannelRoom>;

const roomId = (await Rooms.insertOne(room)).insertedId;
const result = await Rooms.findOneAndUpdate(
room,
{
$set: {},
},
{
upsert: true,
returnDocument: 'after',
},
);

if (!result.value) {
throw new Error('Room not created');
}

await callbacks.run('livechat.newRoom', room);

await sendMessage(guest, { t: 'livechat-started', msg: '', groupable: false }, room);

return roomId;
return result.value as IOmnichannelRoom;
};

export const createLivechatInquiry = async ({
Expand Down Expand Up @@ -172,8 +186,8 @@ export const createLivechatInquiry = async ({
visitor: { _id, username, department, status, activity },
});

const result = (
await LivechatInquiry.insertOne({
const result = await LivechatInquiry.findOneAndUpdate(
{
rid,
name,
ts,
Expand All @@ -192,11 +206,24 @@ export const createLivechatInquiry = async ({
estimatedWaitingTimeQueue: DEFAULT_SLA_CONFIG.ESTIMATED_WAITING_TIME_QUEUE,

...extraInquiryInfo,
})
).insertedId;
},
{
$set: {
_id: new ObjectId().toHexString(),
},
},
{
upsert: true,
returnDocument: 'after',
},
);
logger.debug(`Inquiry ${result} created for visitor ${_id}`);

return result;
if (!result.value) {
throw new Error('Inquiry not created');
}

return result.value as ILivechatInquiryRecord;
};

export const createLivechatSubscription = async (
Expand Down
46 changes: 21 additions & 25 deletions apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,28 +221,26 @@ export class QueueManager {

const name = (roomInfo?.fname as string) || guest.name || guest.username;

const room = await LivechatRooms.findOneById(
await createLivechatRoom(rid, name, guest, roomInfo, {
...extraData,
...(Boolean(customFields) && { customFields }),
}),
);
const room = await createLivechatRoom(rid, name, guest, roomInfo, {
...extraData,
...(Boolean(customFields) && { customFields }),
});

if (!room) {
logger.error(`Room for visitor ${guest._id} not found`);
throw new Error('room-not-found');
}
logger.debug(`Room for visitor ${guest._id} created with id ${room._id}`);

const inquiry = await LivechatInquiry.findOneById(
await createLivechatInquiry({
rid,
name,
initialStatus: await this.getInquiryStatus({ room, agent: defaultAgent }),
guest,
message,
extraData: { ...extraData, source: roomInfo.source },
}),
);
const inquiry = await createLivechatInquiry({
rid,
name,
initialStatus: await this.getInquiryStatus({ room, agent: defaultAgent }),
guest,
message,
extraData: { ...extraData, source: roomInfo.source },
});

if (!inquiry) {
logger.error(`Inquiry for visitor ${guest._id} not found`);
throw new Error('inquiry-not-found');
Expand Down Expand Up @@ -299,15 +297,13 @@ export class QueueManager {
if (!room) {
throw new Error('room-not-found');
}
const inquiry = await LivechatInquiry.findOneById(
await createLivechatInquiry({
rid,
name,
guest,
message: message?.msg,
extraData: { source },
}),
);
const inquiry = await createLivechatInquiry({
rid,
name,
guest,
message: message?.msg,
extraData: { source },
});
if (!inquiry) {
throw new Error('inquiry-not-found');
}
Expand Down

0 comments on commit 0a46a11

Please sign in to comment.