From 304b89006430f83eacc56ed978ae12001a62d450 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 22 Oct 2024 17:24:53 -0300 Subject: [PATCH] chore: improve some omnichannel types --- .../app/livechat/imports/server/rest/sms.ts | 12 ++------ .../meteor/app/livechat/server/api/v1/room.ts | 9 ++++-- apps/meteor/app/livechat/server/lib/Helper.ts | 17 +++++------ .../app/livechat/server/lib/LivechatTyped.ts | 29 +++++-------------- .../app/livechat/server/lib/QueueManager.ts | 22 ++++---------- apps/meteor/lib/callbacks.ts | 19 ++++-------- packages/core-typings/src/IRoom.ts | 8 ++++- 7 files changed, 43 insertions(+), 73 deletions(-) diff --git a/apps/meteor/app/livechat/imports/server/rest/sms.ts b/apps/meteor/app/livechat/imports/server/rest/sms.ts index 8c6701ac67eb..12bfcd6f7a33 100644 --- a/apps/meteor/app/livechat/imports/server/rest/sms.ts +++ b/apps/meteor/app/livechat/imports/server/rest/sms.ts @@ -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'; @@ -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, }, @@ -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, diff --git a/apps/meteor/app/livechat/server/api/v1/room.ts b/apps/meteor/app/livechat/server/api/v1/room.ts index 5d327e3d4ad7..b46a8c3e0663 100644 --- a/apps/meteor/app/livechat/server/api/v1/room.ts +++ b/apps/meteor/app/livechat/server/api/v1/room.ts @@ -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 { @@ -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, diff --git a/apps/meteor/app/livechat/server/lib/Helper.ts b/apps/meteor/app/livechat/server/lib/Helper.ts index 76a192dc1078..a6af9cee2f24 100644 --- a/apps/meteor/app/livechat/server/lib/Helper.ts +++ b/apps/meteor/app/livechat/server/lib/Helper.ts @@ -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'; @@ -60,18 +63,12 @@ export const allowAgentSkipQueue = (agent: SelectedAgent) => { return hasRoleAsync(agent.agentId, 'bot'); }; -export const createLivechatRoom = async < - E extends Record & { - sla?: string; - customFields?: Record; - source?: OmnichannelSourceType; - }, ->( +export const createLivechatRoom = async ( rid: string, name: string, guest: ILivechatVisitor, - roomInfo: Partial = {}, - extraData?: E, + roomInfo: IOmnichannelRoomInfo = {}, + extraData?: IOmnichannelRoomExtraData, ) => { check(rid, String); check(name, String); @@ -163,7 +160,7 @@ export const createLivechatInquiry = async ({ guest?: Pick; message?: string; initialStatus?: LivechatInquiryStatus; - extraData?: Pick; + extraData?: IOmnichannelInquiryExtraData; }) => { check(rid, String); check(name, String); diff --git a/apps/meteor/app/livechat/server/lib/LivechatTyped.ts b/apps/meteor/app/livechat/server/lib/LivechatTyped.ts index b76dae2cd9c4..9037e75e4537 100644 --- a/apps/meteor/app/livechat/server/lib/LivechatTyped.ts +++ b/apps/meteor/app/livechat/server/lib/LivechatTyped.ts @@ -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'; @@ -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; + extraData?: IOmnichannelRoomExtraData; }) { if (!settings.get('Livechat_enabled')) { throw new Meteor.Error('error-omnichannel-is-disabled'); @@ -465,21 +464,12 @@ class LivechatClass { return room; } - async getRoom< - E extends Record & { - sla?: string; - customFields?: Record; - source?: OmnichannelSourceType; - }, - >( + async getRoom( guest: ILivechatVisitor, message: Pick, - 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'); @@ -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); diff --git a/apps/meteor/app/livechat/server/lib/QueueManager.ts b/apps/meteor/app/livechat/server/lib/QueueManager.ts index 1644316e9920..4506e38b99d4 100644 --- a/apps/meteor/app/livechat/server/lib/QueueManager.ts +++ b/apps/meteor/app/livechat/server/lib/QueueManager.ts @@ -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'; @@ -146,29 +145,20 @@ export class QueueManager { await this.dispatchInquiryQueued(inquiry, room, defaultAgent); } - static async requestRoom< - E extends Record & { - sla?: string; - customFields?: Record; - 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( @@ -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, diff --git a/apps/meteor/lib/callbacks.ts b/apps/meteor/lib/callbacks.ts index ae2ea140bde1..28a926dba997 100644 --- a/apps/meteor/lib/callbacks.ts +++ b/apps/meteor/lib/callbacks.ts @@ -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'; @@ -108,10 +110,7 @@ interface EventLikeCallbackSignatures { * TODO: develop a middleware alternative and grant independence of execution order */ type ChainedCallbackSignatures = { - 'livechat.beforeRoom': ( - roomInfo: Record, - extraData?: Record & { sla?: string }, - ) => Record; + 'livechat.beforeRoom': (roomInfo: IOmnichannelRoomInfo, extraData?: IOmnichannelRoomExtraData) => Partial; 'livechat.newRoom': (room: IOmnichannelRoom) => IOmnichannelRoom; 'livechat.beforeForwardRoomToDepartment': ( @@ -207,15 +206,7 @@ type ChainedCallbackSignatures = { options: { forwardingToDepartment?: { oldDepartmentId?: string; transferData?: any }; clientAction?: boolean }; }, ) => Promise<(IOmnichannelRoom & { chatQueued: boolean }) | undefined>; - 'livechat.beforeInquiry': ( - data: Pick & { sla?: string; priority?: string; [other: string]: unknown } & { - customFields?: Record; - source?: OmnichannelSourceType; - }, - ) => Pick & { sla?: string; priority?: string; [other: string]: unknown } & { - customFields?: Record; - source?: OmnichannelSourceType; - }; + 'livechat.beforeInquiry': (data: IOmnichannelInquiryExtraData) => Partial; 'roomNameChanged': (room: IRoom) => void; 'roomTopicChanged': (room: IRoom) => void; 'roomAnnouncementChanged': (room: IRoom) => void; diff --git a/packages/core-typings/src/IRoom.ts b/packages/core-typings/src/IRoom.ts index cba7fbede924..e35456b7977f 100644 --- a/packages/core-typings/src/IRoom.ts +++ b/packages/core-typings/src/IRoom.ts @@ -205,7 +205,7 @@ export interface IOmnichannelGenericRoom extends Omit, 'source' | 'sms' | 'email'>; + +export type IOmnichannelRoomExtraData = Pick, 'customFields' | 'source'> & { sla?: string }; + +export type IOmnichannelInquiryExtraData = IOmnichannelRoomExtraData & { priority?: string }; + export type RoomAdminFieldsType = | '_id' | 'prid'