Skip to content
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
94 changes: 43 additions & 51 deletions apps/meteor/app/livechat/server/api/v1/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,64 @@ import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/agent.info/:rid/:token', {
async get() {
try {
check(this.urlParams, {
rid: String,
token: String,
});
check(this.urlParams, {
rid: String,
token: String,
});

const visitor = await findGuest(this.urlParams.token);
if (!visitor) {
throw new Meteor.Error('invalid-token');
}

const room = findRoom(this.urlParams.token, this.urlParams.rid);
if (!room) {
throw new Meteor.Error('invalid-room');
}
const visitor = await findGuest(this.urlParams.token);
if (!visitor) {
throw new Meteor.Error('invalid-token');
}

const agent = room && room.servedBy && findAgent(room.servedBy._id);
if (!agent) {
throw new Meteor.Error('invalid-agent');
}
const room = findRoom(this.urlParams.token, this.urlParams.rid);
if (!room) {
throw new Meteor.Error('invalid-room');
}

return API.v1.success({ agent });
} catch (e) {
return API.v1.failure(e);
const agent = room && room.servedBy && findAgent(room.servedBy._id);
if (!agent) {
throw new Meteor.Error('invalid-agent');
}

return API.v1.success({ agent });
},
});

API.v1.addRoute('livechat/agent.next/:token', {
get() {
try {
check(this.urlParams, {
token: String,
});
check(this.urlParams, {
token: String,
});

check(this.queryParams, {
department: Match.Maybe(String),
});
check(this.queryParams, {
department: Match.Maybe(String),
});

const { token } = this.urlParams;
const room = findOpenRoom(token);
if (room) {
return API.v1.success();
}

let { department } = this.queryParams;
if (!department) {
const requireDeparment = Livechat.getRequiredDepartment();
if (requireDeparment) {
department = requireDeparment._id;
}
}
const { token } = this.urlParams;
const room = findOpenRoom(token);
if (room) {
return API.v1.success();
}

const agentData = Promise.await(Livechat.getNextAgent(department));
if (!agentData) {
throw new Meteor.Error('agent-not-found');
let { department } = this.queryParams;
if (!department) {
const requireDeparment = Livechat.getRequiredDepartment();
if (requireDeparment) {
department = requireDeparment._id;
}
}

const agent = findAgent(agentData.agentId);
if (!agent) {
throw new Meteor.Error('invalid-agent');
}
const agentData = Promise.await(Livechat.getNextAgent(department));
if (!agentData) {
throw new Meteor.Error('agent-not-found');
}

return API.v1.success({ agent });
} catch (e) {
return API.v1.failure(e);
const agent = findAgent(agentData.agentId);
if (!agent) {
throw new Meteor.Error('invalid-agent');
}

return API.v1.success({ agent });
},
});
34 changes: 15 additions & 19 deletions apps/meteor/app/livechat/server/api/v1/customField.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ import { findLivechatCustomFields, findCustomFieldById } from '../lib/customFiel

API.v1.addRoute('livechat/custom.field', {
async post() {
try {
check(this.bodyParams, {
token: String,
key: String,
value: String,
overwrite: Boolean,
});

const { token, key, value, overwrite } = this.bodyParams;
check(this.bodyParams, {
token: String,
key: String,
value: String,
overwrite: Boolean,
});

const guest = await findGuest(token);
if (!guest) {
throw new Meteor.Error('invalid-token');
}
const { token, key, value, overwrite } = this.bodyParams;

if (!(await Livechat.setCustomFields({ token, key, value, overwrite }))) {
return API.v1.failure();
}
const guest = await findGuest(token);
if (!guest) {
throw new Meteor.Error('invalid-token');
}

return API.v1.success({ field: { key, value, overwrite } });
} catch (e) {
return API.v1.failure(e);
if (!(await Livechat.setCustomFields({ token, key, value, overwrite }))) {
return API.v1.failure();
}

return API.v1.success({ field: { key, value, overwrite } });
},
});

Expand Down
89 changes: 41 additions & 48 deletions apps/meteor/app/livechat/server/api/v1/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,36 +234,33 @@ API.v1.addRoute(
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
put() {
try {
check(this.bodyParams, {
rid: String,
oldVisitorId: String,
newVisitorId: String,
});

const { rid, newVisitorId, oldVisitorId } = this.bodyParams;

const { visitor } = Promise.await(findVisitorInfo({ userId: this.userId, visitorId: newVisitorId }));
if (!visitor) {
throw new Meteor.Error('invalid-visitor');
}

let room = LivechatRooms.findOneById(rid, { _id: 1 }); // TODO: check _id
if (!room) {
throw new Meteor.Error('invalid-room');
}

const { v: { _id: roomVisitorId } = {} } = room; // TODO: v it will be undefined
if (roomVisitorId !== oldVisitorId) {
throw new Meteor.Error('invalid-room-visitor');
}

room = Livechat.changeRoomVisitor(this.userId, rid, visitor);

return API.v1.success({ room });
} catch (e) {
return API.v1.failure(e);
// This endpoint is deprecated and will be removed in future versions.
check(this.bodyParams, {
rid: String,
oldVisitorId: String,
newVisitorId: String,
});

const { rid, newVisitorId, oldVisitorId } = this.bodyParams;

const { visitor } = Promise.await(findVisitorInfo({ userId: this.userId, visitorId: newVisitorId }));
if (!visitor) {
throw new Meteor.Error('invalid-visitor');
}

let room = LivechatRooms.findOneById(rid, { _id: 1, v: 1 }); // TODO: check _id
if (!room) {
throw new Meteor.Error('invalid-room');
}

const { v: { _id: roomVisitorId } = {} } = room; // TODO: v it will be undefined
if (roomVisitorId !== oldVisitorId) {
throw new Meteor.Error('invalid-room-visitor');
}

room = Livechat.changeRoomVisitor(this.userId, rid, visitor);

return API.v1.success({ room });
},
},
);
Expand All @@ -273,33 +270,29 @@ API.v1.addRoute(
{ authRequired: true, permissionsRequired: ['view-l-room'] },
{
get() {
try {
check(this.queryParams, { roomId: String });
check(this.queryParams, { roomId: String });

const { roomId } = this.queryParams;
const { roomId } = this.queryParams;

const { user } = this;
const { user } = this;

if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'joinRoom' });
}
if (!user) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'joinRoom' });
}

const room = LivechatRooms.findOneById(roomId);
const room = LivechatRooms.findOneById(roomId);

if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'joinRoom' });
}
if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'joinRoom' });
}

if (!canAccessRoom(room, user)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'joinRoom' });
}
if (!canAccessRoom(room, user)) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'joinRoom' });
}

addUserToRoom(roomId, user);
addUserToRoom(roomId, user);

return API.v1.success();
} catch (e) {
return API.v1.failure(e);
}
return API.v1.success();
},
},
);
24 changes: 10 additions & 14 deletions apps/meteor/app/livechat/server/api/v1/transcript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/transcript', {
async post() {
try {
check(this.bodyParams, {
token: String,
rid: String,
email: String,
});
check(this.bodyParams, {
token: String,
rid: String,
email: String,
});

const { token, rid, email } = this.bodyParams;
if (!(await Livechat.sendTranscript({ token, rid, email }))) {
return API.v1.failure({ message: TAPi18n.__('Error_sending_livechat_transcript') });
}

return API.v1.success({ message: TAPi18n.__('Livechat_transcript_sent') });
} catch (e) {
return API.v1.failure(e);
const { token, rid, email } = this.bodyParams;
if (!(await Livechat.sendTranscript({ token, rid, email }))) {
return API.v1.failure({ message: TAPi18n.__('Error_sending_livechat_transcript') });
}

return API.v1.success({ message: TAPi18n.__('Livechat_transcript_sent') });
},
});
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/api/v1/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ API.v1.addRoute('livechat/visitor', {
await Promise.all(rooms.map((room: IRoom) => Livechat.saveRoomInfo(room, visitor)));
}

if (customFields && customFields instanceof Array) {
if (customFields && Array.isArray(customFields)) {
customFields.forEach((field) => {
const customField = Promise.await(LivechatCustomField.findOneById(field.key));
if (!customField) {
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,11 @@ export const Livechat = {
const visitor = await LivechatVisitors.getVisitorByToken(token, {
projection: { _id: 1, token: 1, language: 1, username: 1, name: 1 },
});

if (!visitor) {
throw new Meteor.Error('error-invalid-token', 'Invalid token');
}

const userLanguage = (visitor && visitor.language) || settings.get('Language') || 'en';
const timezone = getTimezone(user);
Livechat.logger.debug(`Transcript will be sent using ${timezone} as timezone`);
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/tests/data/livechat/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const createVisitor = (department?: string): Promise<ILivechatVisitor> =>
});
});

export const takeInquiry = (roomId: string, _agentId: string): Promise<IOmnichannelRoom> => {
export const takeInquiry = (roomId: string, _agentId?: string): Promise<IOmnichannelRoom> => {
return new Promise((resolve, reject) => {
request
.post(methodCall(`livechat:takeInquiry`))
Expand Down
Loading