[IMPROVE] Replace livechat:customFields to REST (#15496)
* Create livechat custom fields endpoints to replace pub * Replace livechat:customFields subscribes * Apply suggestions from review * Add support to pagination * Remove unecessary asyncpull/15937/head
parent
003e135a5c
commit
931e793a87
@ -0,0 +1,35 @@ |
||||
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission'; |
||||
import { LivechatCustomField } from '../../../../models/server/raw'; |
||||
|
||||
export async function findLivechatCustomFields({ userId, pagination: { offset, count, sort } }) { |
||||
if (!await hasPermissionAsync(userId, 'view-l-room')) { |
||||
throw new Error('error-not-authorized'); |
||||
} |
||||
|
||||
const cursor = await LivechatCustomField.find({}, { |
||||
sort: sort || { label: 1 }, |
||||
skip: offset, |
||||
limit: count, |
||||
}); |
||||
|
||||
const total = await cursor.count(); |
||||
|
||||
const customFields = await cursor.toArray(); |
||||
|
||||
return { |
||||
customFields, |
||||
count: customFields.length, |
||||
offset, |
||||
total, |
||||
}; |
||||
} |
||||
|
||||
export async function findCustomFieldById({ userId, customFieldId }) { |
||||
if (!await hasPermissionAsync(userId, 'view-l-room')) { |
||||
throw new Error('error-not-authorized'); |
||||
} |
||||
|
||||
return { |
||||
customField: await LivechatCustomField.findOneById(customFieldId), |
||||
}; |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
import { BaseRaw } from './BaseRaw'; |
||||
|
||||
export class LivechatCustomFieldRaw extends BaseRaw { |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@ |
||||
import { getCredentials, api, request, credentials } from '../../../data/api-data.js'; |
||||
import { updatePermission, updateSetting } from '../../../data/permissions.helper'; |
||||
|
||||
describe('LIVECHAT - custom fields', function() { |
||||
this.retries(0); |
||||
|
||||
before((done) => getCredentials(done)); |
||||
|
||||
before((done) => { |
||||
updateSetting('Livechat_enabled', true).then(done); |
||||
}); |
||||
|
||||
describe('livechat/custom-fields', () => { |
||||
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/custom-fields')) |
||||
.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 array of custom fields', (done) => { |
||||
updatePermission('view-l-room', ['admin']) |
||||
.then(() => { |
||||
request.get(api('livechat/custom-fields')) |
||||
.set(credentials) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body.customFields).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); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('livechat/custom-fields/id', () => { |
||||
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/custom-fields/invalid-id')) |
||||
.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(() => updatePermission('view-l-room', ['admin']).then(done)); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue