restricted action for a collection customers solely for customers that have registered orders. In our data models a customer hasMany orders.
The behavior observed above corresponds to this implementation in the file customers.js
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
restricted action for a collection customers solely for customers that have registered orders. In our data models a customer hasMany orders.
The behavior observed above corresponds to this implementation in the file customers.js
const { collection } = require('forest-express-sequelize');
const models = require('../models');
const { Op } = models.Sequelize;
collection('customers', {
actions: [
{
name: 'Restricted action',
},
],
fields: [
{
field: 'ordersNumber',
type: 'Number',
get(customer) {
return models.orders
.count({ where: { customer_id: customer.id } })
.then((nb) => nb);
},
},
],
segments: [
{
name: 'Customers with orders',
where: (query) => {
const recordId = JSON.parse(query.filters).value;
return models.orders
.count({ where: { customer_id: recordId } })
.then((ordersNumber) => {
if (ordersNumber > 0) {
return { id: { [Op.in]: [recordId] } };
}
return { id: { [Op.in]: [null] } };
});
},
},
],
});
{
segment: 'Customers with orders',
filters: '{"field":"id","operator":"equal","value":"67573"}',
timezone: 'Europe/Paris'
}
Was this page helpful?