Triggering an external endpoint on collection summary load

Feature(s) impacted

We are hoping to trigger a call to one of our own apis upon loading the summary view of a certain collection. Is this possible?

Observed behavior

N/A

Expected behavior

Upon loading the collection, an external endpoint is called and, ideally, a page refresh is triggered upon receiving a response.

Context

  • Project name: scratch-payment-service
  • Team name: All
  • Environment name: all
  • Database type: postgres
  • Recent changes made on your end if any: N/A

And, if you are self-hosting your agent:

  • Agent technology: (nodejs, php, ruby, python) nodejs
  • Agent (forest package) name & version (from your .lock file): “forest-express-sequelize”: “9.6.0”

Hi @Brett_Belka,

There is multiple ways to do that.

But what would greatly help me here is to know why you need to do that :slight_smile: ?

And why do you need the page to be refreshed afterward ? Does the data change or do you need to displaying a view totally different ?

PS: I see you are still on our legacy agent, I strongly suggest you to migrate to our new agent-nodejs which is more performant, easier to use an have much more feature :wink:

Hey @vince !

We have a corner case where we need one of our database records updated and we need to make sure that, when we load, we are checking the case and updating as necessary. We’re building an endpoint on one our other services to handle all of the logic. The endpoint will check to see if the record needs to be updated, if so update it, and return appropriate status. We’ll need a page (or individual collection, preferably) reload once updated. It’s a collection that is loaded as connected data in a number of our collections so it would be ideal if the endpoint trigger/reload were connected to the collection updated.

We’ve looked into migrating in the past but we a bit apprehensive about the amount of work required as we’re using a number of internal services and I was given the impression that much of our backend would need to be rewritten.

Okey so there is a feature in beta that could help you. You can send notification to the frontend and let the frontend refresh data automatically or display a notification

export type NotificationFromAgent = {
  notification:
    | { refresh: { collectionName: string; recordIds?: string[] } }
    | { message: { type: 'success' | 'info' | 'warning' | 'error'; text: string } };
  target?: { users?: string[]; team?: string; roles?: string[] };
};

const sendNotification = async (notif: NotificationFromAgent) => {
  const resp = await fetch(
    `https://api.forestadmin.com/liana/notifications-from-agent`,
    {
      method: 'POST',
      body: JSON.stringify(notif),
      headers: {
        'forest-secret-key': process.env.FOREST_ENV_SECRET,
        'Content-Type': 'application/json',
      },
    },
  );

  return resp;
};

Ok. Great! Thanks for your help!

Looks easy enough. We can just execute the external fetch first in sendNotification, correct?

Does this require the new agent? It seems like it could be independent of agent version.

Where does this need to live for it to execute automatically when the collection loads initially? The only automatic behavior I’m aware of on load of a collection is the smart field get hook. Is that the suggested approach?

Can refresh accept an array of collections to refresh? We may be updating two collections (I haven’t seen final logic on that yet).

How does target work? If we leave it undefined, will it automatically target everyone?

Can you also refresh my memory on recordIds? Is that assigned by FA or is that the id from our db record?

It’s just a webhook, so you can call it whatever the agent you are using.

About the options:

  • target: If left empty, it will send the notification to everyone :wink:.
  • refresh: It accepts a single collection. If you need to refresh multiple collection, you will need to call the webhook multiple times
  • recordIds: If you omit it, it will refresh all the records of that collection displayed. The ids are the one you have in your Database :wink:

Where does this need to live for it to execute automatically when the collection loads initially? The only automatic behavior I’m aware of on load of a collection is the smart field get hook. Is that the suggested approach?

Isn’t the update done by another App ? If so, just call the webhook when the update is done.
If your issue is that you need to update the record in db when fetching the collection, then just override/extend the route of the collection.

1 Like

Ok. Thanks! I guess I’m a little confused about what the notification is, exactly. I had imagined that it was just a modal or something that appeared when triggered but if its a webhook, how is the notification delivered? Email? Or will all active users see it? Or?

Extension of the route is a great idea. Again, what I’m trying to do is, when a specific collection loads, trigger an external endpoint, then, if we get a 200, refresh the updated records. If 204, do nothing (success but update wasn’t necessary). If error, alert error.

Based upon what I’m understanding, I’m thinking that the most beneficial would be to extend the get route to hit the external endpoint, process the response and trigger a “notification” which causes the changed records to refresh but doesn’t alert anyone. Does that make sense?

Yes you are right this is what should be done I guess :smiley:
It won’t alert anyone if you don’t specify the message part (it will just refresh the collection). If you need to warn your users you can just fill the message part to let them know for example that their data has been refreshed :wink:

Hey, @vince! Just working on implementing this and I’m getting an error when trying to post the to the notification endpoint.

{"level":"info","message":"Sending notification to Forest Admin at https://api.development.forestadmin.com/liana/notifications-from-agent"}
{"level":"error","message":"Error sending notification to Forest Admin: getaddrinfo ENOTFOUND api.development.forestadmin.com"}

@vince any thoughts? The url provided doesn’t seem to exist.

Hey @Brett_Belka ,

Sorry I didn’t saw you replied earlier :sweat_smile:.

Oh sorry I send you a wrong endpoint :sweat_smile:, you are trying to reach out our development environment which you can’t. I updated my snippet above

It’s https://api.forestadmin.com not https://api.development.forestadmin.com

2 Likes