[IMPROVE] Replace livechat:agents pub by REST calls (#15490)
parent
4ac63d84ea
commit
ac9159da5d
@ -0,0 +1,55 @@ |
||||
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission'; |
||||
import { Users } from '../../../../models/server/raw'; |
||||
|
||||
async function findUsers({ userId, role, pagination: { offset, count, sort } }) { |
||||
if (!await hasPermissionAsync(userId, 'view-livechat-manager') || !await hasPermissionAsync(userId, 'manage-livechat-agents')) { |
||||
throw new Error('error-not-authorized'); |
||||
} |
||||
|
||||
const cursor = await Users.findUsersInRoles(role, undefined, { |
||||
sort: sort || { name: 1 }, |
||||
skip: offset, |
||||
limit: count, |
||||
fields: { |
||||
username: 1, |
||||
name: 1, |
||||
status: 1, |
||||
statusLivechat: 1, |
||||
emails: 1, |
||||
}, |
||||
}); |
||||
|
||||
const total = await cursor.count(); |
||||
|
||||
const users = await cursor.toArray(); |
||||
|
||||
return { |
||||
users, |
||||
count: users.length, |
||||
offset, |
||||
total, |
||||
}; |
||||
} |
||||
export async function findAgents({ userId, pagination: { offset, count, sort } }) { |
||||
return findUsers({ |
||||
role: 'livechat-agent', |
||||
userId, |
||||
pagination: { |
||||
offset, |
||||
count, |
||||
sort, |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
export async function findManagers({ userId, pagination: { offset, count, sort } }) { |
||||
return findUsers({ |
||||
role: 'livechat-manager', |
||||
userId, |
||||
pagination: { |
||||
offset, |
||||
count, |
||||
sort, |
||||
}, |
||||
}); |
||||
} |
||||
@ -0,0 +1,97 @@ |
||||
import { getCredentials, api, request, credentials } from '../../../data/api-data.js'; |
||||
import { createAgent, createManager } from '../../../data/livechat/rooms.js'; |
||||
import { updatePermission, updateSetting } from '../../../data/permissions.helper'; |
||||
|
||||
describe('LIVECHAT - Agents', function() { |
||||
this.retries(0); |
||||
let agent; |
||||
let manager; |
||||
|
||||
before((done) => getCredentials(done)); |
||||
|
||||
before((done) => { |
||||
updateSetting('Livechat_enabled', true) |
||||
.then(createAgent) |
||||
.then((createdAgent) => { |
||||
agent = createdAgent; |
||||
}) |
||||
.then(createManager) |
||||
.then((createdManager) => { |
||||
manager = createdManager; |
||||
done(); |
||||
}); |
||||
}); |
||||
|
||||
describe('livechat/users/:type', () => { |
||||
it('should return an "unauthorized error" when the user does not have the necessary permission', (done) => { |
||||
updatePermission('view-livechat-manager', []) |
||||
.then(() => updatePermission('manage-livechat-agents', [])) |
||||
.then(() => { |
||||
request.get(api('livechat/users/agent')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body.error).to.be.equal('error-not-authorized'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
it('should throw an error when the type is invalid', (done) => { |
||||
updatePermission('view-livechat-manager', ['admin']) |
||||
.then(() => updatePermission('manage-livechat-agents', ['admin'])) |
||||
.then(() => { |
||||
request.get(api('livechat/users/invalid-type')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body.error).to.be.equal('Invalid type'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
it('should return an array of agents', (done) => { |
||||
updatePermission('view-livechat-manager', ['admin']) |
||||
.then(() => updatePermission('manage-livechat-agents', ['admin'])) |
||||
.then(() => { |
||||
request.get(api('livechat/users/agent')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body.users).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'); |
||||
const agentRecentlyCreated = res.body.users.find((user) => agent._id === user._id); |
||||
expect(agentRecentlyCreated._id).to.be.equal(agent._id); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
it('should return an array of managers', (done) => { |
||||
updatePermission('view-livechat-manager', ['admin']) |
||||
.then(() => updatePermission('manage-livechat-agents', ['admin'])) |
||||
.then(() => { |
||||
request.get(api('livechat/users/manager')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body.users).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'); |
||||
const managerRecentlyCreated = res.body.users.find((user) => manager._id === user._id); |
||||
expect(managerRecentlyCreated._id).to.be.equal(manager._id); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue