Create a scope more than one level away based on a Smart field
Create a scope more than one level away based on a Smart field
Step 1: Create a smart field and the filter for the users table
users tableconst { collection } = require('forest-express-sequelize');
const { users, departments, companies } = require('../models');
const models = require('../models');
const { Op } = models.objectMapping;
collection('users', {
actions: [],
fields: [
{
field: 'company name',
isFilterable: true,
type: 'String',
get: async (user) => {
//We are looking for the company name of the user (user belongs to a department that belongs to a company)
const company = await companies.findOne({
attributes: ['name'],
include: {
required: true,
model: departments,
where: { id: user.departmentId },
},
});
return company.name;
},
filter: async ({ condition: { value, operator } }) => {
switch (operator) {
case 'equal':
//We are looking for all the users ids that have a company name equal to the condition value
const queryToFindUsers = await users.findAll({
attributes: ['id'],
include: [
{
required: true,
model: departments,
include: [
{
required: true,
model: companies,
where: { name: { [Op.eq]: value } },
},
],
},
],
});
//We map this array of objects to retrieve the user ids
const userIds = queryToFindUsers.map((user) => user.id);
return { id: { [Op.in]: userIds } };
default:
return null;
}
},
},
],
segments: [],
});Step 2: Configure the scope in the UI


Last updated
Was this helpful?