info:
  name: Elenco contraenti formattati
  type: http
  seq: 12

http:
  method: POST
  url: "{{elixFormsApiUrl}}/eftl/process/v1"
  headers:
    - name: x-requested-with
      value: XMLHttpRequest
    - name: x-api-key
      value: "{{elixFormsWsAuthenticationToken}}"
    - name: x-ef-request-id
      value: "8760"
  body:
    type: json
    data: |-
      {
        "userDocument": "{{elixBase64EftlDocument}}", // see the pre-request script
        "userContext": {
          "lang": "IT"
        }
      }
  auth: inherit

runtime:
  scripts:
    - type: before-request
      code: |-
        const { prepareEftlDocument } = require("./elixFormsJs.js")
        // Copy-pasta your EFTL document below inside the template literals (`)
        prepareEftlDocument(String.raw`
        [EFTL][HEADER name="trimDocument" value="true" type="boolean" /]
        [VAR name="idxNome" type="number"][% idxNome = 0; %][/VAR]
        [VAR name="idxCfPIva" type="number"][% idxCfPIva = 1; %][/VAR]
        [VAR name="idxSede" type="number"][% idxSede = 2; %][/VAR]

        [VAR name="elencoContraentiCompleto" type="string"][% elencoContraentiCompleto = ""; %][/VAR]

        [VAR name="contraenti" type="iterable"][SPLIT regex="\n"][TAG]SCHEMAID,341,COL0004,IUQOID, , [/TAG][/SPLIT][/VAR]
        [VAR name="countContraenti" type="number"][SIZE_OF varname="contraenti" /][/VAR]
        [VAR name="idxContraente" type="number"][% idxContraente = 0; %][/VAR]

        [!-- Estraggo i dati del primo contraente (ce ne sarà sempre almeno uno) --]
        [VAR name="currContraente" type="string"][VALUE_OF varname="contraenti" index="idxContraente" /][/VAR]
        [VAR name="currContraenteSplit" type="iterable"][SPLIT regex=", (.*?):"][TRIM][VALUE_OF varname="currContraente" /][/TRIM][/SPLIT][/VAR]
        [VAR name="currContraenteNome" type="string"][TRIM][VALUE_OF varname="currContraenteSplit" index="idxNome" /][/TRIM][/VAR]
        [VAR name="currContraenteCfPIva" type="string"][TRIM][VALUE_OF varname="currContraenteSplit" index="idxCfPIva" /][/TRIM][/VAR]
        [VAR name="currContraenteSede" type="string"][TRIM][VALUE_OF varname="currContraenteSplit" index="idxSede" /][/TRIM][/VAR]
        [% elencoContraentiCompleto = currContraenteNome + " (CF/PIVA: " + currContraenteCfPIva + ") con sede legale in " + currContraenteSede; %]

        [!-- Se c'è più di un contraente, ciclo su tutti i rimanenti (tutto questo per non avere una ", " in fondo alla stringa finale...) --]
        [WHILE]
            [CONDITION][% idxContraente < countContraenti - 1 %][/CONDITION]
            [DO]
                [% idxContraente = idxContraente + 1; %]

                [VAR name="currContraente" type="string"][VALUE_OF varname="contraenti" index="idxContraente" /][/VAR]
                [VAR name="currContraenteSplit" type="iterable"][SPLIT regex=", (.*?):"][TRIM][VALUE_OF varname="currContraente" /][/TRIM][/SPLIT][/VAR]
                [VAR name="currContraenteNome" type="string"][VALUE_OF varname="currContraenteSplit" index="idxNome" /][/VAR]
                [VAR name="currContraenteCfPIva" type="string"][VALUE_OF varname="currContraenteSplit" index="idxCfPIva" /][/VAR]
                [VAR name="currContraenteSede" type="string"][VALUE_OF varname="currContraenteSplit" index="idxSede" /][/VAR]
                [% elencoContraentiCompleto = elencoContraentiCompleto + "; " + currContraenteNome + " (CF/PIVA: " + currContraenteCfPIva + ") con sede legale in " + currContraenteSede; %]
            [/DO]
        [/WHILE]

        [%= elencoContraentiCompleto %]
        [/EFTL]
        `);

settings:
  encodeUrl: true
  timeout: 0
  followRedirects: true
  maxRedirects: 5
  forwardAuthorizationHeader: true
