Hello !
I want to do something similar to what the scope feature can do, only a little more complexe.
Basically I have a table Order which has an foreign key to the Distributor table, which again has a foreign key to the SalesRepresentative table. I want the user connected to the Forest Admin client (which is a SalesRepresentative) to only see the Order of the Distributor related to itself.
The scope functionality gives me access to the fields from the Order and Distributor collections but not from the SalesRepresentative collection.
I tried to override the GET
route of the Order collection, and just execute my custom query there so I can get the data I want. It works quite fine, but the issue is that I have then to manage the pagination, filtering and sorting on my own. It seems quite complicated to completely override the GET
route behavior when I only want to add a simple filter on my collection.
Do you have an easier/better way to get this done ?
I share the code I have now
router.get("/order", permissionMiddlewareCreator.list(), async (request, response, next) => {
const { user } = request;
const querySQL = `SELECT
ord.id,
...
FROM \`order\` ord
INNER JOIN distributor
ON ord.distributorId = distributor.Id
INNER JOIN salesRepresentative
ON distributor.salesRepresentativeId = salesRepresentative.id
WHERE salesRepresentative.email = "${user.email}"`;
const queryType = connections.default.QueryTypes.SELECT;
const result = await connections.default.query(querySQL, { type: queryType });
const recordSerializer = new RecordSerializer({name: 'order'});
const recordSerialized = await recordSerializer.serialize(result);
response.send(recordSerialized);
});