I have a smart action in a collection :
{
name: 'Générer un ODS',
type: 'single',
fields: [{
field: 'Copropriétaire',
type: 'Enum',
enums: []
}, {
field: 'Type ODS',
type: 'Enum',
enums: [
'Avec devis',
'Sans devis',
'Demande de devis'
]
}, {
field: 'Descriptif intervention',
type: 'String'
}, {
field: 'Date intervention souhaitée',
type: 'Date'
}, {
field: 'Informations détaillées pour le fournisseur',
type: 'String'
}, {
field: 'Fournisseur',
type: 'Enum',
enums: []
}],
hooks: {
load: ({ fields, record }) => models.contracts.findAll({
where: { place_id: record.id },
include: [{
model: models.identities,
as: 'identity'
}]
}).then(contracts => {
fields['Copropriétaire'].enums = contracts.map(contract => `${contract.customerReferenceNumber} - ${contract.identity ? contract.identity.lastName.toUpperCase() : ''} ${contract.identity ? contract.identity.firstName : ''}`);
return axios.get(
`${API_URL}/forest_admin/sage_providers`, {
headers: {
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
'X-CURRENT-USER-EMAIL': record.userEmail
},
params: {
place_id: record.id,
provider_name: '',
siret: '',
matricule: ''
}
}).then(async res => {
if (res.data) {
fields['Fournisseur'].enums = res.data.map(fournisseur => `${fournisseur.matricule} - ${fournisseur.raison_sociale}`);
}
return fields;
});
})
}
}
For any API I need to call in a smart field, I need to enrich the record to access to the user email, I’m doing it like this :
// Get a Place
router.get('/places/:recordId', permissionMiddlewareCreator.details(), (request, response, next) => {
// Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#get-a-record
// forward count calls to avoid error
if (request.params.recordId === 'count') {
return next();
}
const recordGetter = new RecordGetter(models.places);
recordGetter.get(request.params.recordId)
.then(record => {
record.userEmail = request.user.email;
return recordGetter.serialize(record);
})
.then(recordSerialized => response.send(recordSerialized))
.catch(next);
});
for any get method in a smart field, i can recover the userEmail in the record, for instance :
{
field: 'attestation',
type: 'File',
get: place => axios.put(`${API_URL} /forest_admin/places / ${place.id} /download_coownership_register`, null, {
headers: {
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}`,
'X-CURRENT-USER-EMAIL': place.userEmail
},
}).then(res => res.data.attestation ? res.data.attestation : '')
}
but in the hooks: load method, i do not get the enriched record. I need to retrieve the userEmail in order to call my API.