Woodshop
Search…
Display a customized response
This example shows you how to display a different response following a smart action based on the number of records selected:
    if the action is done on one company, the response is "Company is now live!"
    if the action is done on several company, the response is "Companies are now live!"

Requirements

    An admin backend running on forest-express-sequelize

How it works

Directory: /models

This directory contains the companies.js file where the model is declared.
/models/companies.js
1
module.exports = (sequelize, DataTypes) => {
2
const { Sequelize } = sequelize;
3
const Companies = sequelize.define('companies', {
4
name: {
5
type: DataTypes.STRING,
6
},
7
status: {
8
type: DataTypes.STRING,
9
}
10
}, {
11
tableName: 'companies',
12
timestamps: false,
13
schema: process.env.DATABASE_SCHEMA,
14
});
15
16
17
return Companies;
18
};
Copied!

Directory: /forest

This directory contains the companies.js file where the Smart Action Mark as Liveis declared.
1
const { collection } = require('forest-express-sequelize');
2
3
collection('companies', {
4
actions: [{
5
name: 'Mark as Live',
6
type: 'bulk',
7
}],
8
});
Copied!

Directory: /routes

This directory contains the companies.js file where the implementation of the route is handled. The POST /forest/actions/mark-as-live API call is triggered when you click on the Smart Action in the Forest UI.
1
//...
2
const { RecordsGetter } = require('forest-express-sequelize');
3
4
router.post('/actions/mark-as-live', (req, res) => {
5
return new RecordsGetter(companies, req.user, req.query).getIdsFromRequest(req)
6
.then((companyIds) => {
7
const companiesCount = companyIds.length;
8
return companies
9
.update({ status: 'live' }, { where: { id: companyIds }})
10
.then(() => {
11
const message = companiesCount > 1 ? 'Companies are now live!' : 'Company is now live!';
12
res.send({ success: message });
13
});
14
});
15
});
16
17
//...
Copied!
Last modified 2mo ago