Filter references before adding them

Feature(s) impacted

Adding an existing reference
Here I try to add a product to a menu:
Capture d’écran 2022-03-23 à 18.29.49

Observed behavior

When adding an existing reference, we get a little context of the reference (only the Reference field ).

Expected behavior

My use case:
I have a lot of products, some with the same name but different configurations, some of them unactive.

  • I’d like to filter them before adding them (the unactive ones) or
  • I’d like to get a bit more context so I can differenciate them (the ones with same or close name but different configuration)

Is there a way to achieve that?

Context

Please provide in this mandatory section, the relevant information about your configuration:

  • Project name: mytraiteur
  • Team name: Operations
  • Environment name: Development
  • Agent type & version:
"liana": "forest-express-sequelize",
"liana_version": "8.4.7",
"stack": {
      "database_type": "mysql",
      "engine": "nodejs",
      "engine_version": "14.17.0",
      "orm_version": "5.15.
}
  • Recent changes made on your end if any: none

Hi @mathieuh :wave: unfortunately it is not possible today. I will push your request to our product board.
I suggest you to switch your pieces field to an enum and set the enum options with the load hook based on the record.
Please have a look on https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions/use-a-smart-action-form#change-your-forms-data-based-on-previous-field-values.
Let me know if that help :pray:

Hi,

I’m not sure what you mean. Do you mean that I create an action for that instead of using the built in add action?

Oh m’y bad I thought that you using a smart action.
Ok so, You need to override the GET route of your relation.
To do what you what you want, please have a look on request.query.context and request.header('forest-context-url')

router.get('/pieces', permissionMiddlewareCreator.list(), (request, response, next) => {
  if(request.query?.context?.relationship === 'HasMany') {
    // do what you want here
  } else {
    next();
  }
});

Le me know.

1 Like

Got it! I’ll try that and mark your reply as a solution if that works.

Cheers,

1 Like

I managed to rewrite the get function but the pieces don’t match the search param anymore.
How should I do it? I’m not sure about using LIKE %search%.

Also, is there a way to get the record I’m adding the pieces to? I’m adding pieces to a menu, I’d like to get the menu id and its other properties but I’m not sure if it’s possible.

Cheers,

Can you share with us the code you written?
I think you should use something like this Default routes - Developer guide

That’s almost exactly what I wrote.

router.get('/pieces', permissionMiddlewareCreator.list(), (request, response, next) => {
  const { query, user } = request
  // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#get-a-list-of-records
  const recordsGetter = new RecordsGetter(models.pieces, user, query)
  models.pieces.findAll({ where: { active: { [Op.gt]: 0 } } })
    .then(pieces => recordsGetter.serialize(pieces))
    .then(serializedPieces => response.send(serializedPieces))
    .catch(next)

  // next();
});

I think you should use

const filters = '{ "field":"active", "operator":"greater_than", "value":0 }'
await recordsGetter.getAll({ filters })...

:warning: you should check for equest.query?.context, if not, this behavior become the default one for list of pieces.

1 Like

Thanks, it works! Could you link me the doc to recordsGetter? I didn’t find it.

Yes, I know I need put that in a if, I was waiting to make it work first :smiley:

1 Like

Unfortunately we doesn’t have such as API reference for now. It will change soon.