Gather several smart fields at once

Here’s a bunch of smart fields.

  fields: [{
    field: 'titre',
    type: 'String',
    get: maintenanceContract => models.providers.findOne({
      where: { id: maintenanceContract.providerIdKey }
    }).then(provider => axios.get(`${API_URL}/forest_admin/sage_providers`, {
      headers: {
        'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
        'X-CURRENT-USER-EMAIL': process.env.SERVICE_EMAIL
      },
      params: {
        provider_name: '',
        siret: '',
        matricule: provider ? provider.matricule : ''
      }
    }).then(res => {
      return `${res.data[0].raison_sociale} - ${maintenanceContract.reference}`;
    }))
  }, {
    field: 'Fournisseur',
    type: 'String',
    get: maintenanceContract => models.providers.findOne({
      where: { id: maintenanceContract.providerIdKey }
    }).then(provider => axios.get(`${API_URL}/forest_admin/sage_providers`, {
      headers: {
        'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
        'X-CURRENT-USER-EMAIL': process.env.SERVICE_EMAIL
      },
      params: {
        provider_name: '',
        siret: '',
        matricule: provider ? provider.matricule : ''
      }
    }).then(res => {
      return `${res.data[0].raison_sociale} - ${res.data[0].site_ville}`;
    }))
  }, {
    field: 'raison sociale',
    type: 'String',
    get: maintenanceContract => models.providers.findOne({
      where: { id: maintenanceContract.providerIdKey }
    }).then(provider => axios.get(`${API_URL}/forest_admin/sage_providers`, {
      headers: {
        'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
        'X-CURRENT-USER-EMAIL': process.env.SERVICE_EMAIL
      },
      params: {
        provider_name: '',
        siret: '',
        matricule: provider ? provider.matricule : ''
      }
    }).then(res => res.data[0].raison_sociale))
  }, {
    field: 'SIRET',
    type: 'String',
    get: maintenanceContract => models.providers.findOne({
      where: { id: maintenanceContract.providerIdKey }
    }).then(provider => axios.get(`${API_URL}/forest_admin/sage_providers`, {
      headers: {
        'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
        'X-CURRENT-USER-EMAIL': process.env.SERVICE_EMAIL
      },
      params: {
        provider_name: '',
        siret: '',
        matricule: provider ? provider.matricule : ''
      }
    }).then(res => res.data[0].siret))
  }, {
    field: 'adresse',
    type: 'String',
    get: maintenanceContract => models.providers.findOne({
      where: { id: maintenanceContract.providerIdKey }
    }).then(provider => axios.get(`${API_URL}/forest_admin/sage_providers`, {
      headers: {
        'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
        'X-CURRENT-USER-EMAIL': process.env.SERVICE_EMAIL
      },
      params: {
        provider_name: '',
        siret: '',
        matricule: provider ? provider.matricule : ''
      }
    }).then(res => res.data[0].site_adresse))
  }, 

As you can see, there’s an API call for each field. I would like instead call my API once and distribute the gathered information to my smart fields.
At the same time. I want to keep it simple and avoid files dependencies and tedious imbricated asynchronous calls. Is there any Forest Admin standard, compliant, way to do it ?

2 Likes

Hey @JeremyV,

As far as I know, there is not way to directly achieve the goal you want.
This is a known feature request, so I’m adding your message to our product board.

The “best” workaround we currently have would be to locally cache these values in your backend, and implement the get base on these infos. Far from an ideal solution I must admit, but the best we currently have :confused:
Thinking about it, and depending on dependencies of your smart field & your collection, maybe a BelongsTo smart collection could also do the trick here.

As a side note, this is a topic we definitely will work on in a near future, since performance AND code readability on this specific point have already be made multiple times. I cannot provide any ETA nor info though, since the topic was not started yet.