[NEW] Endpoint to fetch livechat rooms with several filters (#15155)
* WIP: livechat endpoint to get all rooms with filters * Created an endpoint to get all livechat rooms with filters * Remove wrong expectation * Move endpoint to another folder * simplify operationpull/15133/head^2
parent
4b646e10f0
commit
c0f106b2f8
@ -0,0 +1,51 @@ |
||||
import { Match, check } from 'meteor/check'; |
||||
|
||||
import { hasPermission } from '../../../../authorization/server'; |
||||
import { API } from '../../../../api'; |
||||
import { findRooms } from '../../../server/api/lib/livechat'; |
||||
|
||||
API.v1.addRoute('livechat/rooms', { authRequired: true }, { |
||||
get() { |
||||
try { |
||||
if (!hasPermission(this.userId, 'view-livechat-manager')) { |
||||
return API.v1.unauthorized(); |
||||
} |
||||
const { offset, count } = this.getPaginationItems(); |
||||
const { sort, fields } = this.parseJsonQuery(); |
||||
const { agents, departmentId, open, tags } = this.requestParams(); |
||||
let { createdAt, customFields, closedAt } = this.requestParams(); |
||||
check(agents, Match.Maybe([String])); |
||||
check(departmentId, Match.Maybe(String)); |
||||
check(open, Match.Maybe(String)); |
||||
check(tags, Match.Maybe([String])); |
||||
if (createdAt) { |
||||
createdAt = JSON.parse(createdAt); |
||||
} |
||||
if (closedAt) { |
||||
closedAt = JSON.parse(closedAt); |
||||
} |
||||
if (customFields) { |
||||
customFields = JSON.parse(customFields); |
||||
} |
||||
const { rooms, total } = findRooms({ |
||||
agents, |
||||
departmentId, |
||||
open: open && open === 'true', |
||||
createdAt, |
||||
closedAt, |
||||
tags, |
||||
customFields, |
||||
options: { offset, count, sort, fields }, |
||||
}); |
||||
|
||||
return API.v1.success({ |
||||
rooms, |
||||
count: rooms.length, |
||||
offset, |
||||
total, |
||||
}); |
||||
} catch (e) { |
||||
return API.v1.failure(e); |
||||
} |
||||
}, |
||||
}); |
@ -0,0 +1,38 @@ |
||||
import { api, credentials, request } from '../api-data'; |
||||
import { adminUsername } from '../user'; |
||||
|
||||
export const createLivechatRoom = (visitorToken) => new Promise((resolve) => { |
||||
request.get(api(`/livechat/room?token=${ visitorToken }`)) |
||||
.set(credentials) |
||||
.end((err, res) => resolve(res.body.room)); |
||||
}); |
||||
|
||||
export const createVisitor = () => new Promise((resolve) => { |
||||
request.get(api('livechat/visitor/iNKE8a6k6cjbqWhWd')) |
||||
.end((err, res) => { |
||||
if (!err && res && res.body && res.body.visitor) { |
||||
return resolve(res.body.visitor); |
||||
} |
||||
request.post(api('livechat/visitor')) |
||||
.set(credentials) |
||||
.send({ |
||||
visitor: { |
||||
name: `Visitor ${ Date.now() }`, |
||||
email: 'visitor@rocket.chat', |
||||
token: 'iNKE8a6k6cjbqWhWd', |
||||
phone: '55 51 5555-5555', |
||||
customFields: [{ key: 'address', value: 'Rocket.Chat street', overwrite: true }], |
||||
}, |
||||
}) |
||||
.end((err, res) => { resolve(res.body.visitor); }); |
||||
}); |
||||
}); |
||||
|
||||
export const createAgent = () => new Promise((resolve) => { |
||||
request.post(api('livechat/users/agent')) |
||||
.set(credentials) |
||||
.send({ |
||||
username: adminUsername, |
||||
}) |
||||
.end((err, res) => resolve(res.body.user)); |
||||
}); |
@ -0,0 +1,136 @@ |
||||
import { getCredentials, api, request, credentials } from '../../../data/api-data.js'; |
||||
import { createVisitor, createLivechatRoom, createAgent } from '../../../data/livechat/rooms.js'; |
||||
import { updatePermission, updateSetting } from '../../../data/permissions.helper'; |
||||
|
||||
describe('LIVECHAT - rooms', function() { |
||||
this.retries(0); |
||||
|
||||
before((done) => getCredentials(done)); |
||||
|
||||
before((done) => { |
||||
updateSetting('Livechat_enabled', true).then(() => { |
||||
createAgent() |
||||
.then(() => createVisitor()) |
||||
.then((visitor) => createLivechatRoom(visitor.token)) |
||||
.then(done); |
||||
}); |
||||
}); |
||||
|
||||
describe('livechat/rooms', () => { |
||||
it('should return an "unauthorized error" when the user does not have the necessary permission', (done) => { |
||||
updatePermission('view-livechat-manager', []).then(() => { |
||||
request.get(api('livechat/rooms')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(403) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body.error).to.be.equal('unauthorized'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
it('should return an error when the "agents" query parameter is not valid', (done) => { |
||||
updatePermission('view-livechat-manager', ['admin']).then(() => { |
||||
request.get(api('livechat/rooms?agents=invalid')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
it('should return an error when the "departmentId" query parameter is not valid', (done) => { |
||||
request.get(api('livechat/rooms?departmentId[]=marcos')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an error when the "open" query parameter is not valid', (done) => { |
||||
request.get(api('livechat/rooms?open[]=true')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an error when the "tags" query parameter is not valid', (done) => { |
||||
request.get(api('livechat/rooms?tags=invalid')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an error when the "createdAt" query parameter is not valid', (done) => { |
||||
request.get(api('livechat/rooms?createdAt=invalid')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an error when the "closedAt" query parameter is not valid', (done) => { |
||||
request.get(api('livechat/rooms?closedAt=invalid')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an error when the "customFields" query parameter is not valid', (done) => { |
||||
request.get(api('livechat/rooms?customFields=invalid')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an array of rooms when has no parameters', (done) => { |
||||
request.get(api('livechat/rooms')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body.rooms).to.be.an('array'); |
||||
expect(res.body).to.have.property('offset'); |
||||
expect(res.body).to.have.property('total'); |
||||
expect(res.body).to.have.property('count'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
it('should return an array of rooms when the query params is all valid', (done) => { |
||||
request.get(api(`livechat/rooms?agents[]=teste&departamentId=123&open=true&createdAt={"start": "2018-01-26T00:11:22.345Z", "end": "2018-01-26T00:11:22.345Z"}
|
||||
&closedAt={"start": "2018-01-26T00:11:22.345Z", "end": "2018-01-26T00:11:22.345Z"}&tags[]=rocket |
||||
&customFields={"docId": "031041"}&count=3&offset=1&sort={"_updatedAt": 1}&fields={"msgs": 1}`))
|
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body.rooms).to.be.an('array'); |
||||
expect(res.body).to.have.property('offset'); |
||||
expect(res.body).to.have.property('total'); |
||||
expect(res.body).to.have.property('count'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
}); |
Loading…
Reference in new issue