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

feat(Livechat): transferChat Livechat api endpoint #32498

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/afraid-guests-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/livechat": patch
MartinSchoeler marked this conversation as resolved.
Show resolved Hide resolved
---

Created a transferChat Livechat API endpoint for transferring chats programmatically, the endpoint has all the limitations & permissions required that transferring via UI has
MartinSchoeler marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare const window: Window & {
setParentUrl: (url: string) => void;
setTheme: (theme: { color?: string; fontColor?: string; iconColor?: string; title?: string; offlineTitle?: string }) => void;
setLanguage: (language: string) => void;
transferChat: (department: string) => void;
onChatMaximized: (callback: () => void) => void;
onChatMinimized: (callback: () => void) => void;
onChatStarted: (callback: () => void) => void;
Expand Down Expand Up @@ -406,6 +407,60 @@ test.describe('OC - Livechat API', () => {
});
});

test.describe('OC - Livechat API - transferChat', () => {
let poAuxContext2: { page: Page; poHomeOmnichannel: HomeOmnichannel };

test.beforeEach(async ({ browser }) => {
const { page: pageCtx2 } = await createAuxContext(browser, Users.user2);
poAuxContext2 = { page: pageCtx2, poHomeOmnichannel: new HomeOmnichannel(pageCtx2) };
});

test.afterEach(async () => {
await poAuxContext2.page.close();
});

test('transferChat - Called during ongoing conversation', async () => {
const [departmentA, departmentB] = departments.map(({ data }) => data);
const registerGuestVisitor = {
name: faker.person.firstName(),
email: faker.internet.email(),
token: faker.string.uuid(),
department: departmentA._id,
};

// Start Chat
await poLiveChat.page.evaluate(() => window.RocketChat.livechat.maximizeWidget());
await expect(page.frameLocator('#rocketchat-iframe').getByText('Start Chat')).toBeVisible();

await poLiveChat.page.evaluate(
(registerGuestVisitor) => window.RocketChat.livechat.registerGuest(registerGuestVisitor),
registerGuestVisitor,
);

await expect(page.frameLocator('#rocketchat-iframe').getByText('Start Chat')).not.toBeVisible();

await poLiveChat.onlineAgentMessage.type('this_a_test_message_from_visitor');
await poLiveChat.btnSendMessageToOnlineAgent.click();

await test.step('Expect registered guest to be in dep1', async () => {
await poAuxContext.poHomeOmnichannel.sidenav.openChat(registerGuestVisitor.name);
await expect(poAuxContext.poHomeOmnichannel.content.channelHeader).toContainText(registerGuestVisitor.name);
});

const depId = departmentB._id;

await test.step('Expect chat to be transferred', async () => {
await poLiveChat.page.evaluate((depId) => window.RocketChat.livechat.transferChat(depId), depId);

await poAuxContext2.page.locator('role=navigation >> role=button[name=Search]').click();
await poAuxContext2.page.locator('role=search >> role=searchbox').fill(registerGuestVisitor.name);
await expect(
poAuxContext2.page.locator(`role=search >> role=listbox >> role=link >> text="${registerGuestVisitor.name}"`),
).toBeVisible();
});
});
});

test('OC - Livechat API - registerGuest', async ({ browser }) => {
const registerGuestVisitor = {
name: faker.person.firstName(),
Expand Down
21 changes: 21 additions & 0 deletions packages/livechat/src/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,27 @@ const api = {
});
},

transferChat: async (department: string) => {
const {
config: { departments = [] },
room,
} = store.state;

const dep = departments.find((dep) => dep._id === department || dep.name === department)?._id || '';

if (!dep) {
throw new Error(
'The selected department is invalid. Check departments configuration to ensure the department exists, is enabled and has at least 1 agent',
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
);
}
if (!room) {
throw new Error("Conversation has not been started yet, can't transfer");
MartinSchoeler marked this conversation as resolved.
Show resolved Hide resolved
}

const { _id: rid } = room;
await Livechat.transferChat({ rid, department: dep });
},

setLanguage: async (language: StoreState['iframe']['language']) => {
const { iframe } = store.state;
await store.setState({ iframe: { ...iframe, language } });
Expand Down
5 changes: 5 additions & 0 deletions packages/livechat/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ function setParentUrl(url: string) {
callHook('setParentUrl', url);
}

function transferChat(department: string) {
callHook('transferChat', department);
}

function setGuestMetadata(metadata: StoreState['iframe']['guestMetadata']) {
if (typeof metadata !== 'object') {
throw new Error('Invalid metadata');
Expand Down Expand Up @@ -550,6 +554,7 @@ const livechatWidgetAPI = {
setGuestMetadata,
clearAllCallbacks,
setHiddenSystemMessages,
transferChat,

// callbacks
onChatMaximized(fn: () => void) {
Expand Down
Loading