src/chat/SupportChat.ts
import {User} from '../session/User';
import {Chat} from './Chat';
type SupportChatParticipant = Pick<User, 'domain' | 'id' | 'username'>;
export interface SupportChatService {
leaveSupportChat(id: string): Promise<void>;
transferSupportChat(id: string, destination: SupportChatParticipant): Promise<void>;
}
// eslint-disable-next-line
export interface SupportChat extends Chat {}
export class SupportChat {
private supportChatId: string;
private chatService: SupportChatService;
private constructor(supportChatId: string, chatService: SupportChatService) {
this.supportChatId = supportChatId;
this.chatService = chatService;
}
public static create(
supportChatId: string,
chatService: SupportChatService,
chat: Chat,
): SupportChat {
const supportChat = new SupportChat(supportChatId, chatService);
return Object.assign(Object.create(chat), {
leave: supportChat.leave.bind(supportChat),
transfer: supportChat.transfer.bind(supportChat),
});
}
/**
* Leaves the current Support Chat
*
* If leaving participant is the chat's agent, the support chat is finished.
*
* @return {Promise<void>}
*/
public async leave(): Promise<void> {
await this.chatService.leaveSupportChat(this.supportChatId);
}
/**
* Transfers the current Support Chat to another agent
*
* Changes the agent who is assigned to this support chat. The backend
* will generate an XMPP invitation to that destination agent.
*
* @param {object} destination The agent we want to transfer this chat to
* @param {string} destination.id The ID of the agent
* @param {string} destination.username The username of the agent
* @param {string} destination domain The domain of the agent
*
* @return {Promise<void>}
*/
public async transfer(destination: SupportChatParticipant): Promise<void> {
await this.chatService.transferSupportChat(this.supportChatId, destination);
}
}