[IMPROVE] Replace livechat:visitorInfo publication by REST (#15639)
* Replace livechat:visitorInfo publication by REST * Rename endpointpull/15771/head
parent
4817896573
commit
a8ad42b34f
@ -1,3 +0,0 @@ |
||||
import { Mongo } from 'meteor/mongo'; |
||||
|
||||
export const LivechatVisitor = new Mongo.Collection('rocketchat_livechat_visitor'); |
||||
@ -0,0 +1,16 @@ |
||||
import { check } from 'meteor/check'; |
||||
|
||||
import { API } from '../../../../api'; |
||||
import { findVisitorInfo } from '../../../server/api/lib/visitors'; |
||||
|
||||
API.v1.addRoute('livechat/visitors.info', { authRequired: true }, { |
||||
get() { |
||||
check(this.queryParams, { |
||||
visitorId: String, |
||||
}); |
||||
|
||||
const visitor = Promise.await(findVisitorInfo({ userId: this.userId, visitorId: this.queryParams.visitorId })); |
||||
|
||||
return API.v1.success(visitor); |
||||
}, |
||||
}); |
||||
@ -0,0 +1,17 @@ |
||||
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission'; |
||||
import { LivechatVisitors } from '../../../../models/server/raw'; |
||||
|
||||
export async function findVisitorInfo({ userId, visitorId }) { |
||||
if (!await hasPermissionAsync(userId, 'view-l-room')) { |
||||
throw new Error('error-not-authorized'); |
||||
} |
||||
|
||||
const visitor = await LivechatVisitors.findOneById(visitorId); |
||||
if (!visitor) { |
||||
throw new Error('visitor-not-found'); |
||||
} |
||||
|
||||
return { |
||||
visitor, |
||||
}; |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
import { BaseRaw } from './BaseRaw'; |
||||
|
||||
export class LivechatVisitorsRaw extends BaseRaw { |
||||
|
||||
} |
||||
@ -0,0 +1,59 @@ |
||||
import { getCredentials, api, request, credentials } from '../../../data/api-data.js'; |
||||
import { updatePermission, updateSetting } from '../../../data/permissions.helper'; |
||||
import { createVisitor } from '../../../data/livechat/rooms.js'; |
||||
|
||||
describe('LIVECHAT - visitors', function() { |
||||
this.retries(0); |
||||
let visitor; |
||||
|
||||
before((done) => getCredentials(done)); |
||||
|
||||
before((done) => { |
||||
updateSetting('Livechat_enabled', true) |
||||
.then(() => createVisitor()) |
||||
.then((createdVisitor) => { |
||||
visitor = createdVisitor; |
||||
done(); |
||||
}); |
||||
}); |
||||
|
||||
describe('livechat/visitors.info', () => { |
||||
it('should return an "unauthorized error" when the user does not have the necessary permission', (done) => { |
||||
updatePermission('view-l-room', []).then(() => { |
||||
request.get(api('livechat/visitors.info?visitorId=invalid')) |
||||
.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 return an "visitor not found error" when the visitor doe snot exists', (done) => { |
||||
updatePermission('view-l-room', ['admin']).then(() => { |
||||
request.get(api('livechat/visitors.info?visitorId=invalid')) |
||||
.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('visitor-not-found'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
it('should return the visitor info', (done) => { |
||||
request.get(api(`livechat/visitors.info?visitorId=${ visitor._id }`)) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body.visitor._id).to.be.equal(visitor._id); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue