info:
  name: Elenco Contraenti in formato CSV DSAN
  type: http
  seq: 30

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: "34398"
  body:
    type: json
    data: |-
      {
        "userDocument": "{{elixBase64EftlDocument}}", // see the pre-request script
        "userContext": {
          "lang": "IT",
          "crlf": "\r\n"
        }
      }
  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="firstItem" type="number"][% firstItem = 0; %][/VAR]
        [VAR name="secondItem" type="number"][% secondItem = 1; %][/VAR]
        [VAR name="elencoContraentiCsv" type="string"][% elencoContraentiCsv = ""; %][/VAR]

        [VAR name="elencoContraenti" type="string"][TRIM][TAG]GETVALUEBYTAG,CONTRATTO_CONTRAENTI,REQUEST,ID_OBJECT[/TAG][/TRIM][/VAR]
        [VAR name="elencoContraentiIterable" type="iterable"][SPLIT regex="\n" emptyIfBlank="true"][VALUE_OF varname="elencoContraenti" /][/SPLIT][/VAR]
        [VAR name="elencoContraentiCount" type="number"][SIZE_OF varname="elencoContraentiIterable" /][/VAR]

        [VAR name="contraenteIdx" type="number"][% contraenteIdx = 0; %][/VAR]
        [WHILE threshold="99"]
        [CONDITION][% contraenteIdx < elencoContraentiCount %][/CONDITION]
        [DO]
          [VAR name="contraenteIterable" type="iterable"][SPLIT regex=", CF/PIVA: " emptyIfBlank="true"][TRIM][VALUE_OF varname="elencoContraentiIterable" index="contraenteIdx" /][/TRIM][/SPLIT][/VAR]
          [VAR name="contraenteDenominazione" type="string"][VALUE_OF varname="contraenteIterable" index="firstItem" /][/VAR]

          [% elencoContraentiCsv = elencoContraentiCsv + contraenteDenominazione; %]
          [% contraenteIdx = contraenteIdx + 1; %]

          [IF]
              [CONDITION][% contraenteIdx < elencoContraentiCount %][/CONDITION]
              [THEN]
                  [% elencoContraentiCsv = elencoContraentiCsv + ", "; %]
              [/THEN]
          [/IF]
        [/DO][/WHILE]

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

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