Is there any way to filter the “Chat” component by a user type?
For example, my chat component is linked to the user data source but I do not want to display any of the admins. There is a column in the user DS to distinguish whether a user is an admin or not.
Filtering the list of contacts on the Chat screen will require custom code added to the screen JS of the Chat screen.
Let’s say we have a data source ‘Users’ with column ‘Admin’. For admin users the value of the ‘Admin’ column can be either ‘Yes’ or [Yes]. For regular users there will be no value in the ‘Admin’ column.
Fliplet.Hooks.on('beforeChatContactsRendering', function onBeforeChatContactsRendering(data) {
let emailColumnName = 'Email';
let loginDataSourceName = 'Users';
//find all regular users
return Fliplet.DataSources.connectByName(loginDataSourceName).then(function(connection){
return connection.find({
where: {
[emailColumnName]: { $in: data.contacts.map(contact => contact.data[emailColumnName]) },
$or: [
{ 'Admin': { $ne: 'Yes' }},
{ 'Admin': { $nin: ['Yes'] }}
]
},
attributes: [emailColumnName]
}).then(function(notAdminUsers){
let usersEmailsToShow = notAdminUsers.map(user => user.data[emailColumnName]);
//if regular users are found => update the list of contacts available on the chat screen
if (usersEmailsToShow.length){
data.contacts = data.contacts.filter(contact => usersEmailsToShow.includes(contact.data[emailColumnName]));
} else {
//show no users
data.contacts = [];
}
return Promise.resolve(data);
});
});
});
This code takes all non-admin users from ‘Users’ data source and updates the list of contacts for the Chat component. Don’t forget to update emailColumnName and loginDataSourceName variables for the email column name and login data source respectively.
Hope this was helpfull. Please let me know if you have any other questions.