[NEW] Add GUI for customFields in Omnichannel conversations#15840
[NEW] Add GUI for customFields in Omnichannel conversations#15840renatobecker merged 7 commits intoRocketChat:developfrom antkaz:gui-custom-fields-livechat
Conversation
|
Hi @antkaz! Cheers! |
|
@renatobecker Thank you! 😊 |
| <label class="rc-input__label"> | ||
| <div class="rc-input__title">{{label}}</div> | ||
| <div class="rc-input__wrapper"> | ||
| <input class="rc-input__element" type="text" name="{{name}}" autocomplete="off" data-visitorLivechatData="true" value="{{value}}"> |
There was a problem hiding this comment.
Let's create a new permission to allow/deny editing custom field values, what do you think?
By default, The new permission would be assigned to the livechat-agent, livechat-managers and admin.
There was a problem hiding this comment.
Do I need to add these permissions for the REST API?
There was a problem hiding this comment.
Do I need to add these permissions for the REST API?
No, It's not necessary.
Since we only allow fetching data through the REST API, we don't need to change the REST API permissions.
There was a problem hiding this comment.
I meant the endpoint for updating values POST livechat/custom.field (and livechat/custom.fields). If we restrict access to editing values of custom fields for GUI, the user can still change the values through these API methods.
I think that if we restrict the permissions to edit values of custom fields in the interface, then these restrictions should be for the API. What do you think?
There was a problem hiding this comment.
I meant the endpoint for updating values POST
livechat/custom.field(andlivechat/custom.fields). If we restrict access to editing values ofcustom fieldsfor GUI, the user can still change the values through these API methods.
I think that if we restrict the permissions to edit values ofcustom fieldsin the interface, then these restrictions should be for the API. What do you think?
I see.. The fact is that the Livechat Widget calls those endpoits too, so we can't add permission restrictions there because there is no authenticated user on the Widget-side, you know?
So, let's just skip the idea of implementing new permissions to deal with custom fields, okay?
There was a problem hiding this comment.
I did not know... Thank you. Ok, then I won't add new permissions
There was a problem hiding this comment.
Perfect!
If you're gonna add the validation option, I'll wait for your new commit to get back to the review process, okay?
There was a problem hiding this comment.
Yes, I will add and let you know
| const fields = []; | ||
| let livechatData = {}; | ||
| const user = Template.instance().user.get(); | ||
| if (user) { |
There was a problem hiding this comment.
| if (user) { | |
| const { livechatData = {} } = user || {}; |
| livechatData = _.extend(livechatData, user.livechatData); | ||
| } | ||
|
|
||
| if (!_.isEmpty(livechatData)) { |
There was a problem hiding this comment.
Looking at the complexity of your algorithm, I'd say you can write it better.
For instance:
...
customRoomFields() {
const customFields = Template.instance().customFields.get();
if (!customFields || customFields.length === 0) {
return [];
}
const fields = [];
const room = Template.instance().room.get();
const { livechatData = {} } = room || {};
...
There was a problem hiding this comment.
of course! Thank you)
| } | ||
|
|
||
| if (!_.isEmpty(livechatData)) { | ||
| for (const _id in livechatData) { |
There was a problem hiding this comment.
And Here, why not use Object.keys instead of these if's ?
You can do something like this:
Object.keys(livechatData).forEach((key) => {
const field = _.findWhere(customFields, { key });
...
});
app/livechat/server/lib/Livechat.js
Outdated
| }, | ||
|
|
||
| saveGuest({ _id, name, email, phone }) { | ||
| saveGuest({ _id, name, email, phone, livechatData }) { |
There was a problem hiding this comment.
| saveGuest({ _id, name, email, phone, livechatData }) { | |
| saveGuest({ _id, name, email, phone, livechatData = {} }) { |
app/livechat/server/lib/Livechat.js
Outdated
| if (phone) { | ||
| updateData.phone = phone; | ||
| } | ||
| if (livechatData) { |
There was a problem hiding this comment.
You don't need to check this here..
app/livechat/server/lib/Livechat.js
Outdated
| updateData.phone = phone; | ||
| } | ||
| if (livechatData) { | ||
| updateData.livechatData = {}; |
There was a problem hiding this comment.
| updateData.livechatData = {}; | |
| const customFields = {}; | |
| fields.forEach((field) => { | |
| if (!livechatData.hasOwnProperty(field._id)) { | |
| return; | |
| } | |
| const value = s.trim(livechatData[field._id]); | |
| if (value === '') { | |
| return; | |
| } | |
| if (field.regexp !== undefined && field.regexp !== '') { | |
| const regexp = new RegExp(field.regexp); | |
| if (!regexp.test(value)) { | |
| throw new Meteor.Error(TAPi18n.__('error-invalid-custom-field-value', { field: field.label })); | |
| } | |
| } | |
| customFields[field._id] = value; | |
| }); | |
| Object.assign(updateData, customFields); |
|
This pull request introduces 2 alerts when merging 6590db5 into 18ba691 - view on LGTM.com new alerts:
|
|
Hi @antkaz! Did you push the last requested changes here? Thanks. |
Hi @renatobecker! I rebased branch to last |
renatobecker
left a comment
There was a problem hiding this comment.
Hi @antkaz,
Your PR looks good, I just found a few things to be fixed.
After that, I'll merge it, looking forward to your next commit. =D
Thanks.
| import { hasRole } from '../../../../../authorization'; | ||
| import './visitorEdit.html'; | ||
| import { APIClient } from '../../../../../utils/client'; | ||
| import { LivechatCustomField } from '../../../collections/LivechatCustomField'; |
There was a problem hiding this comment.
We have been deprecating all of the Meteor pub/sub, we're replacing it by the REST API approach, so It would be better to apply the same approach here, okay?
Here you can see an example of the REST API approach working:
https://github.com/RocketChat/Rocket.Chat/blob/develop/app/livechat/client/views/app/livechatCustomFields.js#L75
| this.subscribe('livechat:customFields'); | ||
| this.autorun(async () => { | ||
| const { room } = await APIClient.v1.get(`rooms.info?roomId=${ rid }`); | ||
| const customFields = LivechatCustomField.find().fetch(); |
There was a problem hiding this comment.
As I mentioned above, we need to replace the implementation based on collection by the REST API approach.
app/livechat/server/lib/Livechat.js
Outdated
| return; | ||
| } | ||
| const value = s.trim(livechatData[field._id]); | ||
| if (value === '') { |
There was a problem hiding this comment.
You can't apply this validation, otherwise, the Agent won't be able to clear/remove a field value, right?
app/livechat/server/lib/Livechat.js
Outdated
| return; | ||
| } | ||
| const value = s.trim(livechatData[field._id]); | ||
| if (value === '') { |
There was a problem hiding this comment.
You can't apply this validation, otherwise, the Agent won't be able to clear/remove a field value, right?
There was a problem hiding this comment.
Hi @antkaz,
Your PR looks good, I just found a few things to be fixed.
After that, I'll merge it, looking forward to your next commit. =DThanks.
Hello. I fixed your remarks =)
User can't finish their review.
|
Thanks, @antkaz for your effort on this improvement. |
Closes RocketChat/feature-requests#151