Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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 src/definition/accessors/ILivechatRead.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDepartment } from '../livechat';
import { ILivechatRoom } from '../livechat/ILivechatRoom';
import { IVisitor } from '../livechat/IVisitor';
import { IMessage } from '../messages';

export interface ILivechatRead {
/**
Expand All @@ -27,4 +28,9 @@ export interface ILivechatRead {
getLivechatVisitorByToken(token: string): Promise<IVisitor | undefined>;
getLivechatVisitorByPhoneNumber(phoneNumber: string): Promise<IVisitor | undefined>;
getLivechatDepartmentByIdOrName(value: string): Promise<IDepartment | undefined>;
/**
* @experimental we do not encourage the wider usage of this method,
* as we're evaluating its performance and fit for the API.
*/
_fetchLivechatRoomMessages(roomId: string): Promise<Array<IMessage>>;
}
5 changes: 5 additions & 0 deletions src/server/accessors/LivechatRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ILivechatRead } from '../../definition/accessors/ILivechatRead';
import { IDepartment } from '../../definition/livechat';
import { ILivechatRoom } from '../../definition/livechat/ILivechatRoom';
import { IVisitor } from '../../definition/livechat/IVisitor';
import { IMessage } from '../../definition/messages';
import { LivechatBridge } from '../bridges/LivechatBridge';
export class LivechatRead implements ILivechatRead {
constructor(private readonly livechatBridge: LivechatBridge, private readonly appId: string) { }
Expand Down Expand Up @@ -56,4 +57,8 @@ export class LivechatRead implements ILivechatRead {
public getLivechatDepartmentByIdOrName(value: string): Promise<IDepartment | undefined> {
return this.livechatBridge.doFindDepartmentByIdOrName(value, this.appId);
}

public _fetchLivechatRoomMessages(roomId: string): Promise<Array<IMessage>> {
return this.livechatBridge.do_fetchLivechatRoomMessages(this.appId, roomId);
}
}
24 changes: 19 additions & 5 deletions src/server/bridges/LivechatBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ILivechatTransferData,
IVisitor,
} from '../../definition/livechat';
import { IMessage } from '../../definition/messages';
import { IUser } from '../../definition/users';
import { PermissionDeniedError } from '../errors/PermissionDeniedError';
import { AppPermissionManager } from '../managers/AppPermissionManager';
Expand All @@ -30,6 +31,12 @@ type LivechatWritePermissions = keyof Pick<
'livechat-visitor'
>;

type LivechatMultiplePermissions = keyof Pick<
typeof AppPermissions,
'livechat-department' |
'livechat-message'
>;

export abstract class LivechatBridge extends BaseBridge {
public doIsOnline(departmentId?: string, appId?: string): boolean {
if (this.hasReadPermission(appId, 'livechat-status')) {
Expand Down Expand Up @@ -122,17 +129,23 @@ export abstract class LivechatBridge extends BaseBridge {
}

public async doFindDepartmentByIdOrName(value: string, appId: string): Promise<IDepartment | undefined> {
if (this.hasReadPermission(appId, 'livechat-department') || this.hasMultiplePermission(appId)) {
if (this.hasReadPermission(appId, 'livechat-department') || this.hasMultiplePermission(appId, 'livechat-department')) {
return this.findDepartmentByIdOrName(value, appId);
}
}

public async doFindDepartmentsEnabledWithAgents(appId: string): Promise<Array<IDepartment>> {
if (this.hasMultiplePermission(appId)) {
if (this.hasMultiplePermission(appId, 'livechat-department')) {
return this.findDepartmentsEnabledWithAgents(appId);
}
}

public async do_fetchLivechatRoomMessages(appId: string, roomId: string): Promise<Array<IMessage>> {
if (this.hasMultiplePermission(appId, 'livechat-message')) {
return this._fetchLivechatRoomMessages(appId, roomId);
}
}

public async doSetCustomFields(data: { token: IVisitor['token']; key: string; value: string; overwrite: boolean }, appId: string): Promise<number> {
if (this.hasWritePermission(appId, 'livechat-custom-fields')) {
return this.setCustomFields(data, appId);
Expand Down Expand Up @@ -165,6 +178,7 @@ export abstract class LivechatBridge extends BaseBridge {
protected abstract findRooms(visitor: IVisitor, departmentId: string | null, appId: string): Promise<Array<ILivechatRoom>>;
protected abstract findDepartmentByIdOrName(value: string, appId: string): Promise<IDepartment | undefined>;
protected abstract findDepartmentsEnabledWithAgents(appId: string): Promise<Array<IDepartment>>;
protected abstract _fetchLivechatRoomMessages(appId: string, roomId: string): Promise<Array<IMessage>>;

protected abstract setCustomFields(data: { token: IVisitor['token']; key: string; value: string; overwrite: boolean }, appId: string): Promise<number>;

Expand Down Expand Up @@ -194,14 +208,14 @@ export abstract class LivechatBridge extends BaseBridge {
return false;
}

private hasMultiplePermission(appId: string): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions['livechat-department'].multiple)) {
private hasMultiplePermission(appId: string, scope: LivechatMultiplePermissions): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions[scope].multiple)) {
return true;
}

AppPermissionManager.notifyAboutError(new PermissionDeniedError({
appId,
missingPermissions: [AppPermissions['livechat-department'].multiple],
missingPermissions: [AppPermissions[scope].multiple],
}));

return false;
Expand Down
1 change: 1 addition & 0 deletions src/server/permissions/AppPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const AppPermissions = {
'livechat-message': {
read: { name: 'livechat-message.read' },
write: { name: 'livechat-message.write' },
multiple: { name: 'livechat-message.multiple' },
},
'livechat-room': {
read: { name: 'livechat-room.read' },
Expand Down
4 changes: 4 additions & 0 deletions tests/test-data/bridges/livechatBridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IDepartment, ILivechatMessage, ILivechatRoom, ILivechatTransferData, IVisitor } from '../../../src/definition/livechat';
import { IMessage } from '../../../src/definition/messages';
import { IUser } from '../../../src/definition/users';
import { LivechatBridge } from '../../../src/server/bridges/LivechatBridge';

Expand Down Expand Up @@ -55,6 +56,9 @@ export class TestLivechatBridge extends LivechatBridge {
public findDepartmentByIdOrName(value: string, appId: string): Promise<IDepartment | undefined> {
throw new Error('Method not implemented');
}
public _fetchLivechatRoomMessages(appId: string, roomId: string): Promise<Array<IMessage>> {
throw new Error('Method not implemented');
}
public setCustomFields(data: { token: IVisitor['token']; key: string; value: string; overwrite: boolean }, appId: string): Promise<number> {
throw new Error('Method not implemented');
}
Expand Down