Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(omnichannel): replace create and find by findAndUpdate #32773

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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;
};

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
Loading