Skip to content

Commit

Permalink
chore: improve some omnichannel types
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Oct 22, 2024
1 parent fd5e54f commit 304b890
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 73 deletions.
12 changes: 3 additions & 9 deletions apps/meteor/app/livechat/imports/server/rest/sms.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { OmnichannelIntegration } from '@rocket.chat/core-services';
import type {
ILivechatVisitor,
IOmnichannelRoom,
IUpload,
MessageAttachment,
ServiceData,
FileAttachmentProps,
IOmnichannelSource,
IOmnichannelRoomInfo,
} from '@rocket.chat/core-typings';
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
Expand Down Expand Up @@ -137,10 +137,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', {
return API.v1.success(SMSService.error(new Error('Invalid visitor')));
}

const roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
} = {
const roomInfo: IOmnichannelRoomInfo = {
sms: {
from: sms.to,
},
Expand Down Expand Up @@ -259,10 +256,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', {
const sendMessage: {
guest: ILivechatVisitor;
message: ILivechatMessage;
roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
};
roomInfo: IOmnichannelRoomInfo;
} = {
guest: visitor,
roomInfo,
Expand Down
9 changes: 7 additions & 2 deletions apps/meteor/app/livechat/server/api/v1/room.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Omnichannel } from '@rocket.chat/core-services';
import type { ILivechatAgent, IUser, SelectedAgent, TransferByData } from '@rocket.chat/core-typings';
import type { ILivechatAgent, IOmnichannelInquiryExtraData, IUser, SelectedAgent, TransferByData } from '@rocket.chat/core-typings';
import { isOmnichannelRoom, OmnichannelSourceType } from '@rocket.chat/core-typings';
import { LivechatVisitors, Users, LivechatRooms, Messages } from '@rocket.chat/models';
import {
Expand Down Expand Up @@ -80,7 +80,12 @@ API.v1.addRoute(
},
};

const newRoom = await LivechatTyped.createRoom({ visitor: guest, roomInfo, agent, extraData: extraParams });
const newRoom = await LivechatTyped.createRoom({
visitor: guest,
roomInfo,
agent,
extraData: extraParams as IOmnichannelInquiryExtraData,
});

return API.v1.success({
room: newRoom,
Expand Down
17 changes: 7 additions & 10 deletions apps/meteor/app/livechat/server/lib/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import type {
TransferByData,
ILivechatAgent,
ILivechatDepartment,
IOmnichannelRoomInfo,
IOmnichannelInquiryExtraData,
IOmnichannelRoomExtraData,
} from '@rocket.chat/core-typings';
import { LivechatInquiryStatus, OmnichannelSourceType, DEFAULT_SLA_CONFIG, UserStatus } from '@rocket.chat/core-typings';
import { LivechatPriorityWeight } from '@rocket.chat/core-typings/src/ILivechatPriority';
Expand Down Expand Up @@ -60,18 +63,12 @@ export const allowAgentSkipQueue = (agent: SelectedAgent) => {

return hasRoleAsync(agent.agentId, 'bot');
};
export const createLivechatRoom = async <
E extends Record<string, unknown> & {
sla?: string;
customFields?: Record<string, unknown>;
source?: OmnichannelSourceType;
},
>(
export const createLivechatRoom = async (
rid: string,
name: string,
guest: ILivechatVisitor,
roomInfo: Partial<IOmnichannelRoom> = {},
extraData?: E,
roomInfo: IOmnichannelRoomInfo = {},
extraData?: IOmnichannelRoomExtraData,
) => {
check(rid, String);
check(name, String);
Expand Down Expand Up @@ -163,7 +160,7 @@ export const createLivechatInquiry = async ({
guest?: Pick<ILivechatVisitor, '_id' | 'username' | 'status' | 'department' | 'name' | 'token' | 'activity'>;
message?: string;
initialStatus?: LivechatInquiryStatus;
extraData?: Pick<ILivechatInquiryRecord, 'source'>;
extraData?: IOmnichannelInquiryExtraData;
}) => {
check(rid, String);
check(name, String);
Expand Down
29 changes: 8 additions & 21 deletions apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type {
ILivechatInquiryRecord,
ILivechatContact,
ILivechatContactChannel,
IOmnichannelRoomInfo,
IOmnichannelRoomExtraData,
} from '@rocket.chat/core-typings';
import { OmnichannelSourceType, ILivechatAgentStatus, UserStatus, isOmnichannelRoom } from '@rocket.chat/core-typings';
import { Logger, type MainLogger } from '@rocket.chat/logger';
Expand Down Expand Up @@ -374,12 +376,9 @@ class LivechatClass {
visitor: ILivechatVisitor;
message?: string;
rid?: string;
roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
};
roomInfo: IOmnichannelRoomInfo;
agent?: SelectedAgent;
extraData?: Record<string, unknown>;
extraData?: IOmnichannelRoomExtraData;
}) {
if (!settings.get('Livechat_enabled')) {
throw new Meteor.Error('error-omnichannel-is-disabled');
Expand Down Expand Up @@ -465,21 +464,12 @@ class LivechatClass {
return room;
}

async getRoom<
E extends Record<string, unknown> & {
sla?: string;
customFields?: Record<string, unknown>;
source?: OmnichannelSourceType;
},
>(
async getRoom(
guest: ILivechatVisitor,
message: Pick<IMessage, 'rid' | 'msg' | 'token'>,
roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
},
roomInfo: IOmnichannelRoomInfo,
agent?: SelectedAgent,
extraData?: E,
extraData?: IOmnichannelRoomExtraData,
) {
if (!settings.get('Livechat_enabled')) {
throw new Meteor.Error('error-omnichannel-is-disabled');
Expand Down Expand Up @@ -1120,10 +1110,7 @@ class LivechatClass {
}: {
guest: ILivechatVisitor;
message: ILivechatMessage;
roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
};
roomInfo: IOmnichannelRoomInfo;
agent?: SelectedAgent;
}) {
const { room, newRoom } = await this.getRoom(guest, message, roomInfo, agent);
Expand Down
22 changes: 6 additions & 16 deletions apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Apps, AppEvents } from '@rocket.chat/apps';
import { Omnichannel } from '@rocket.chat/core-services';
import type { ILivechatDepartment } from '@rocket.chat/core-typings';
import type { ILivechatDepartment, IOmnichannelRoomInfo, IOmnichannelRoomExtraData } from '@rocket.chat/core-typings';
import {
LivechatInquiryStatus,
type ILivechatInquiryRecord,
type ILivechatVisitor,
type IOmnichannelRoom,
type SelectedAgent,
type OmnichannelSourceType,
} from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { LivechatDepartment, LivechatDepartmentAgents, LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models';
Expand Down Expand Up @@ -146,29 +145,20 @@ export class QueueManager {
await this.dispatchInquiryQueued(inquiry, room, defaultAgent);
}

static async requestRoom<
E extends Record<string, unknown> & {
sla?: string;
customFields?: Record<string, unknown>;
source?: OmnichannelSourceType;
},
>({
static async requestRoom({
guest,
rid = Random.id(),
message,
roomInfo,
agent,
extraData: { customFields, ...extraData } = {} as E,
extraData: { customFields, ...extraData } = {},
}: {
guest: ILivechatVisitor;
rid?: string;
message?: string;
roomInfo: {
source?: IOmnichannelRoom['source'];
[key: string]: unknown;
};
roomInfo: IOmnichannelRoomInfo;
agent?: SelectedAgent;
extraData?: E;
extraData?: IOmnichannelRoomExtraData;
}) {
logger.debug(`Requesting a room for guest ${guest._id}`);
check(
Expand Down Expand Up @@ -222,7 +212,7 @@ export class QueueManager {
}
}

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

const room = await createLivechatRoom(rid, name, { ...guest, ...(department && { department }) }, roomInfo, {
...extraData,
Expand Down
19 changes: 5 additions & 14 deletions apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import type {
UserStatus,
ILivechatDepartment,
MessageMention,
OmnichannelSourceType,
IOmnichannelRoomInfo,
IOmnichannelInquiryExtraData,
IOmnichannelRoomExtraData,
} from '@rocket.chat/core-typings';
import type { Updater } from '@rocket.chat/models';
import type { FilterOperators } from 'mongodb';
Expand Down Expand Up @@ -108,10 +110,7 @@ interface EventLikeCallbackSignatures {
* TODO: develop a middleware alternative and grant independence of execution order
*/
type ChainedCallbackSignatures = {
'livechat.beforeRoom': (
roomInfo: Record<string, unknown>,
extraData?: Record<string, unknown> & { sla?: string },
) => Record<string, unknown>;
'livechat.beforeRoom': (roomInfo: IOmnichannelRoomInfo, extraData?: IOmnichannelRoomExtraData) => Partial<IOmnichannelRoom>;
'livechat.newRoom': (room: IOmnichannelRoom) => IOmnichannelRoom;

'livechat.beforeForwardRoomToDepartment': <T extends { room: IOmnichannelRoom; transferData?: { department: { _id: string } } }>(
Expand Down Expand Up @@ -207,15 +206,7 @@ type ChainedCallbackSignatures = {
options: { forwardingToDepartment?: { oldDepartmentId?: string; transferData?: any }; clientAction?: boolean };
},
) => Promise<(IOmnichannelRoom & { chatQueued: boolean }) | undefined>;
'livechat.beforeInquiry': (
data: Pick<ILivechatInquiryRecord, 'source'> & { sla?: string; priority?: string; [other: string]: unknown } & {
customFields?: Record<string, unknown>;
source?: OmnichannelSourceType;
},
) => Pick<ILivechatInquiryRecord, 'source'> & { sla?: string; priority?: string; [other: string]: unknown } & {
customFields?: Record<string, unknown>;
source?: OmnichannelSourceType;
};
'livechat.beforeInquiry': (data: IOmnichannelInquiryExtraData) => Partial<ILivechatInquiryRecord>;
'roomNameChanged': (room: IRoom) => void;
'roomTopicChanged': (room: IRoom) => void;
'roomAnnouncementChanged': (room: IRoom) => void;
Expand Down
8 changes: 7 additions & 1 deletion packages/core-typings/src/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export interface IOmnichannelGenericRoom extends Omit<IRoom, 'default' | 'featur
inbox: string;
thread: string[];
replyTo: string;
subject: string;
subject?: string;
};
source: IOmnichannelSource;

Expand Down Expand Up @@ -362,6 +362,12 @@ export const isOmnichannelSourceFromApp = (source: IOmnichannelSource): source i
return source?.type === OmnichannelSourceType.APP;
};

export type IOmnichannelRoomInfo = Pick<Partial<IOmnichannelRoom>, 'source' | 'sms' | 'email'>;

export type IOmnichannelRoomExtraData = Pick<Partial<IOmnichannelRoom>, 'customFields' | 'source'> & { sla?: string };

export type IOmnichannelInquiryExtraData = IOmnichannelRoomExtraData & { priority?: string };

export type RoomAdminFieldsType =
| '_id'
| 'prid'
Expand Down

0 comments on commit 304b890

Please sign in to comment.