New Forest Admin agent Node.js - Adding smart action

Hello :wave:

Feature(s) impacted

collection.addAction()

Observed behavior

I’ve added a smart action in my test environment, here’s the code below

      collection.addAction('Traiter la demande de remboursement', {
        scope: 'Single',
        form: [
          {
            label: 'Traiter la demande',
            type: 'Enum',
            enumValues: ['Valider', 'Refuser', 'A modifier'],
            isRequired: true,
          },
          {
            label: 'montant',
            type: 'Number',
            value: async context => {
              const refundRequestData = await context.getRecord(['amount']);
              const refundStatus = await context.formValues['Traiter la demande'];
              if (refundStatus === 'Valider') {
                return refundRequestData.amount;
              }
              return null;
            },
            isRequired: true
          },
          {
            label: 'commentaire',
            type: 'String',
            widget: 'TextArea',
            placeholder: "Commentaire visible au client, attention en cas de suppression de l'ancien message ce dernier sera perdu. Privilégiez un saut à la ligne si vous souhaiter compléter le message.",
            defaultValue: async context => {
              const refundRequestData = await context.getRecord(['agentComment']);
              if (refundRequestData.agentComment) {
                return refundRequestData.agentComment;
              }
              return null;
            },
            isRequired: async context => {
              const refundStatus = context.formValues['Traiter la demande'];
              if (refundStatus === 'Refuser') {
                return true;
              }
              return false;
            }
          },
        ],
        execute: async (context, resultBuilder) => {
          console.log(context)
          return resultBuilder.success('Amount charged!');
        },
      });

Expected behavior

Using execute and then displaying the log and resultBuilder

Failure Logs

In web log

In my dev log

Is it better to use an app.use() to receive the call, or is there a problem with my addAction? Because I don’t see any problem using the smart action :confused:

Context

  • Project name: Nostrum Care v3
  • Team name: Op
  • Environment name: Local
  • Agent (forest package) name & version:
    @forestadmin/agent”: “^1.43.0”,
    @forestadmin/datasource-sequelize”: “^1.5.21”,
    @forestadmin/datasource-sql”: “^1.0.0”,
    “dotenv”: “^16.0.1”,
    “pg”: “^8.8.0”,
    “sequelize”: “^6.33.0”,
    “stripe”: “^14.17.0”
  • Database type: Postgresql

Thank you in advance for your help !!

Hello @jacques_liao,

Your smart action and your code look valid.

I could run it without getting any issue on my local project.

Before submitting the form, can you see http calls like this on your agent logs ? If you do what is the status ?

info: [200] POST /_actions/NP_refundRequests-Traiter/0/traiter-la-demande-de-remboursement/hooks/load

Hello @nbouliol

I have this

Warn [404] POST /_actions/NP_refundRequests/0/traiter-la-demande-de-remboursement - 18ms

@nbouliol

I think I’ve found the problem.

Because currently I use

agent.mountOnExpress(app).start();
app.listen(process.env.APPLICATION_PORT);

if I comment on the part and uncomment

agent.mountOnStandaloneServer(Number(process.env.APPLICATION_PORT));

it works

But I need

agent.mountOnExpress(app).start();

because it allows me to make calls to the forest route, and then return documents from Minio.

Hello,

By adding this, it allows the route to be found and the smart action to be executed.

app.use('/_actions/NP_refundRequests/0/traiter-la-demande-de-remboursement', async (req, res, next) => {});

It works in Dev, but we’ll have to see if it works in production. (It works in Production) !!