Skip to content

Instantly share code, notes, and snippets.

@arnaudbesnier
Last active August 12, 2020 15:13
Show Gist options
  • Save arnaudbesnier/d63fa3c188127a35d13668b67caba0fe to your computer and use it in GitHub Desktop.
Save arnaudbesnier/d63fa3c188127a35d13668b67caba0fe to your computer and use it in GitHub Desktop.
Forest Express - Admin API - Override on the records list for a Smart Action context
// forest/order.js (Smart Action declaration)
const Liana = require('forest-express-sequelize');
Liana.collection('order', {
actions: [{
name: 'Test product',
fields: [{
field: 'product',
type: 'Number',
reference: 'product.id',
widget: 'belongsto select',
}],
}],
});
// app.js (Product search override)
app.get('/forest/product', async (request, response, next) => {
if (request.query && request.query.searchExtended > -1) {
// NOTICE: Seach for collection list, keep the default behaviour.
return next();
}
// NOTICE: Get the context (objectId) from the referer header.
let orderId = request.headers.referer.match(/record\/[^/]*\/([^/]*)\//);
if (orderId && orderId[1]) {
orderId = orderId[1];
}
const condition = { where: { orderId } };
const products = await models.product.findAll(condition);
return response.send(await new Liana.ResourceSerializer(
Liana,
models.product,
products,
null,
{},
).perform());
});
Liana.init({
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment