POST route in smart collection

I created a smart collection :

const { collection } = require('forest-express-sequelize');
collection('sageProviders', {
  fields: [{
    field: 'id',
    type: 'String',
    get: record => record.matricule
  }, {
    field: 'matricule',
    type: 'String',
  }, {
    field: 'siret',
    type: 'String'
  }, {
    field: 'raison_sociale',
    type: 'String'
  }, {
    field: 'site_adresse',
    type: 'String'
  }, {
    field: 'site_ville',
    type: 'String'
  }, {
    field: 'site_codepostal',
    type: 'String'
  }, {
    field: 'telephone',
    type: 'String'
  }, {
    field: 'email',
    type: 'String'
  }, {
    field: 'bic',
    type: 'String'
  }, {
    field: 'iban',
    type: 'String'
  }, {
    field: 'street_number',
    type: 'String'
  }, {
    field: 'street',
    type: 'String'
  }, {
    field: 'domiciliation',
    type: 'String'
  }]
});

I created the POST route :

// Create a sageProviders
router.post('/sageProviders', permissionMiddlewareCreator.create(), (request, response) => {
  const recordCreator = new RecordCreator(models.buildings);
  const data = request.body.data.attributes;
  const body = {
    matricule: '',
    mail: data.email,
    city: data.site_ville,
    street: data.street,
    company_name: data.raison_sociale,
    iban: data.iban,
    street_number: data.street_number,
    phone_number: data.telephone,
    siret: data.siret,
    bic: data.bic,
    domiciliation: data.domiciliation,
    zip_code: data.site_codepostal
  };

  axios.post(`${API_URL}/forest_admin/sage_create_provider`, body, {
    headers: {
      'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
      'X-CURRENT-USER-EMAIL': request.user.email
    },
  }).then(postResponse => {
    axios.get(
      `${API_URL}/forest_admin/sage_providers`, {
      headers: {
        'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
        'X-CURRENT-USER-EMAIL': request.user.email
      },
      params: {
        provider_name: '',
        siret: '',
        matricule: postResponse.data.matricule
      }
    }).then(async getResponse => {
      response.send(await recordCreator.serialize(getResponse.data[0]));
    }).catch(getError => {
      response.status(400).send(getError.response.data.error);
    });
  }).catch(postError => {
    response.status(400).send(postError.response.data.error);
  });
});

After having created a record, I get the following message :
Screenshot 2021-04-16 164851

I would like to display my record summary view, as it would do for any regular collection (non smart).

How could I achieve that ? Thank you for your help.

Hi @JeremyV,

If you look just after the create you’ll see that there is a GET /forest/sageProviders/yourNewRecordId. If you did not implement the GET it will display the cannot found error

I did implement the GET route, here it is :

// Get a sageProvider
router.get('/sageProviders/:matricule', permissionMiddlewareCreator.details(), (request, response, next) => {
  const recordSerializer = new RecordSerializer({ name: 'sageProviders' });
  const matricule = request.params.matricule;
  const params = {
    provider_name: '',
    siret: '',
    matricule
  };

  axios.get(
    `${API_URL}/forest_admin/sage_providers`, {
    headers: {
      'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
      'X-CURRENT-USER-EMAIL': request.user.email
    },
    params
  }).then(async res => {
    response.send({
      ...(await recordSerializer.serialize(res.data[0])),
      meta: {
        count: res.data.length
      }
    });
  }).catch(err => {
    response.status(400).send({ error: err.response.data.error.message });
  });
});

:thinking:

Then could you please share your error logs from your browser and from your server after the create ?

This is What I get :

Hmm weird :thinking:
Could you please share a video reproducing the issue and also could you share a screenshot of what you see when clicking on the first e.value in the errors

I had the same problem here: Can't create new resources - #31 by nathanqueija and the reason was that I was not returning the ID of the newly created element from the POST call. It used to work without the id before, but stopped at some point. Make sure that you’re returning a unique ID when you serialize the response in the POST call.

2 Likes

Thank you @nathanqueija , that was it indeed.

This is what I did to make it work :

      response.send(
        await recordCreator.serialize(
          {
            id: getResponse.data[0].matricule,
            ...getResponse.data[0]
          }
        )
      );