Skip to content

[NEW] Add GUI for customFields in Omnichannel conversations#15840

Merged
renatobecker merged 7 commits intoRocketChat:developfrom
antkaz:gui-custom-fields-livechat
Jan 10, 2020
Merged

[NEW] Add GUI for customFields in Omnichannel conversations#15840
renatobecker merged 7 commits intoRocketChat:developfrom
antkaz:gui-custom-fields-livechat

Conversation

@antkaz
Copy link
Contributor

@antkaz antkaz commented Nov 21, 2019

@renatobecker-zz
Copy link

Hi @antkaz!
Thanks for your effort in implementing it.
It's, for sure, a very nice feature, I'll review this PR ASAP!

Cheers!

@antkaz
Copy link
Contributor Author

antkaz commented Nov 21, 2019

@renatobecker Thank you! 😊

@renatobecker-zz renatobecker-zz added this to the 3.0.0 milestone Dec 2, 2019
<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}}">
Copy link

@renatobecker-zz renatobecker-zz Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to add these permissions for the REST API?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know... Thank you. Ok, then I won't add new permissions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!
If you're gonna add the validation option, I'll wait for your new commit to get back to the review process, okay?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will add and let you know

const fields = [];
let livechatData = {};
const user = Template.instance().user.get();
if (user) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (user) {
const { livechatData = {} } = user || {};

livechatData = _.extend(livechatData, user.livechatData);
}

if (!_.isEmpty(livechatData)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 || {};
		...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course! Thank you)

}

if (!_.isEmpty(livechatData)) {
for (const _id in livechatData) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 });
			...
		});

},

saveGuest({ _id, name, email, phone }) {
saveGuest({ _id, name, email, phone, livechatData }) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
saveGuest({ _id, name, email, phone, livechatData }) {
saveGuest({ _id, name, email, phone, livechatData = {} }) {

if (phone) {
updateData.phone = phone;
}
if (livechatData) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to check this here..

updateData.phone = phone;
}
if (livechatData) {
updateData.livechatData = {};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

@renatobecker-zz renatobecker-zz self-assigned this Dec 19, 2019
@lgtm-com
Copy link

lgtm-com bot commented Dec 20, 2019

This pull request introduces 2 alerts when merging 6590db5 into 18ba691 - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class

@renatobecker-zz renatobecker-zz modified the milestones: 2.4.0, 3.0.0 Dec 23, 2019
@renatobecker renatobecker requested review from renatobecker and removed request for renatobecker-zz January 7, 2020 19:25
@renatobecker
Copy link
Contributor

renatobecker commented Jan 7, 2020

Hi @antkaz!

Did you push the last requested changes here?
I mean, is the PR ready for a final review?

Thanks.

@antkaz
Copy link
Contributor Author

antkaz commented Jan 9, 2020

Hi @antkaz!

Did you push the last requested changes here?
I mean, is the PR ready for a final review?

Thanks.

Hi @renatobecker! I rebased branch to last develop and pushed changes. You can review PR. Thanks!

Copy link
Contributor

@renatobecker renatobecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned above, we need to replace the implementation based on collection by the REST API approach.

return;
}
const value = s.trim(livechatData[field._id]);
if (value === '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't apply this validation, otherwise, the Agent won't be able to clear/remove a field value, right?

return;
}
const value = s.trim(livechatData[field._id]);
if (value === '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't apply this validation, otherwise, the Agent won't be able to clear/remove a field value, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Hello. I fixed your remarks =)

@renatobecker renatobecker changed the title [NEW] Add GUI for customFields in Livechat [NEW] Add GUI for customFields in Omnichannel conversations Jan 10, 2020
@renatobecker renatobecker removed the request for review from renatobecker-zz January 10, 2020 16:42
@renatobecker renatobecker dismissed renatobecker-zz’s stale review January 10, 2020 16:55

User can't finish their review.

@renatobecker renatobecker merged commit bfdb8b9 into RocketChat:develop Jan 10, 2020
@renatobecker
Copy link
Contributor

Thanks, @antkaz for your effort on this improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Editing custom fields in livechat

4 participants