info:
  name: Notifiche email - Elenco carriere e certificati
  type: http
  seq: 2

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: "33380"
  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: |-
        // Copy-pasta your EFTL document below inside the template literals (`)
        var eftlDocumentToBeProcessed = bru.setVar("elixBase64EftlDocument", btoa(String.raw`
        [EFTL][HEADER name="trimDocument" value="true" type="boolean" /]
        [% elencoCC = "##"; item1 = 0; item2 = 1; indexInsert = 0; %]

        [VAR name="carriereInsert" type="iterable"][SPLIT regex="##"][TAG]SCHEMAID,867,COL0017,IUQOID, ,##[/TAG][/SPLIT][/VAR]
        [VAR name="certificatiInsert" type="iterable"][SPLIT regex="##"][TAG]SCHEMAID,867,COL0014,IUQOID, ,##[/TAG][/SPLIT][/VAR]
        [!--
        [VAR name="carriereInsert" type="iterable"][SPLIT regex="##"][% "11 - L1##11 - L1##22 - L2##11 - L1##33 - L3##11 - L1##33 - L3" %][/SPLIT][/VAR]
        [VAR name="certificatiInsert" type="iterable"][SPLIT regex="##"][% "C1##C1##C2##C3##C4##C5##C6" %][/SPLIT][/VAR]
        --]
        [VAR name="certificatiCount" type="number"][SIZE_OF varname="certificatiInsert" /][/VAR]

        [!-- Questo ciclo produce una stringa nel seguente formato: --]
        [!-- ##L1::C1;;C1;;C3;;C5##L2::C2##L3::C4;;C6 --]
        [!-- dove: "##" separa i gruppi di carriera + certificati, "::" separa il nome della carriera dai suoi certificati, ";;" separa i singoli certificati della carriera --]
        [WHILE threshold="99"]
          [CONDITION][% indexInsert < certificatiCount %][/CONDITION]
          [DO]
            [VAR name="carrieraIterable" type="iterable"][SPLIT regex=" - "][VALUE_OF varname="carriereInsert" index="indexInsert" /][/SPLIT][/VAR]
            [VAR name="nomeCarriera" type="string"][VALUE_OF varname="carrieraIterable" index="item2" /][/VAR]
            [VAR name="nomeCertificato" type="string"][VALUE_OF varname="certificatiInsert" index="indexInsert" /][/VAR]
            [IF]
              [CONDITION][CONTAINS varname="elencoCC"][VALUE_OF varname="nomeCarriera" /][/CONTAINS][/CONDITION]
              [THEN]
                [!-- La carriera è già stata aggiunta con uno o più certificati, devo accodare quello attuale agli altri --]
                [% elencoCCInner = "##"; indexInner = 0; %]
                [VAR name="elencoCCInnerIterable" type="iterable"][SPLIT regex="##" emptyIfBlank="true"][VALUE_OF varname="elencoCC" /][/SPLIT][/VAR]
                [VAR name="sizeOfElencoCCInner" type="number"][SIZE_OF varname="elencoCCInnerIterable" /][/VAR]
                [WHILE threshold="99"]
                  [CONDITION][% indexInner < sizeOfElencoCCInner %][/CONDITION]
                  [DO]
                    [VAR name="nomeCarrieraInner" type="string"][VALUE_OF varname="elencoCCInnerIterable" index="indexInner" /][/VAR]
                    [% elencoCCInner = elencoCCInner + nomeCarrieraInner; %]
                    [IF]
                      [CONDITION][CONTAINS varname="nomeCarrieraInner"][VALUE_OF varname="nomeCarriera" /][/CONTAINS][/CONDITION]
                      [THEN][% elencoCCInner = elencoCCInner + ";;" + nomeCertificato; %][/THEN]
                    [/IF]
                    [% elencoCCInner = elencoCCInner + "##"; %]
                    [% indexInner = indexInner + 1; %]
                  [/DO]
                [/WHILE]
                [% elencoCC = elencoCCInner; %]
              [/THEN]
              [ELSE]
                [!-- La carriera non è ancora stata aggiunta, non devo fare altro --]
                [% elencoCC = elencoCC + nomeCarriera + "::" + nomeCertificato + "##"; %]
              [/ELSE]
            [/IF]
            [% indexInsert = indexInsert + 1; %]
          [/DO]
        [/WHILE]

        [!-- ORA costruisco l'elenco gerarchico HTML... Avrei potuto farlo sopra, ma il blocco di logica lo posso riutilizzare per altro --]
        [VAR name="elencoCertificatiPerCarriera" type="iterable"][SPLIT regex="##" emptyIfBlank="true"][VALUE_OF varname="elencoCC" /][/SPLIT][/VAR]
        <ol>[FOR varname="gruppo" iterable="elencoCertificatiPerCarriera"]
          [VAR name="gruppoIterable" type="iterable"][SPLIT regex="::"][VALUE_OF varname="gruppo" /][/SPLIT][/VAR]
          [VAR name="gruppoCarriera" type="string"][VALUE_OF varname="gruppoIterable" index="item1" /][/VAR]
          [VAR name="gruppoCertificatiIterable" type="iterable" unique="true"][SPLIT regex=";;"][VALUE_OF varname="gruppoIterable" index="item2" /][/SPLIT][/VAR]
          <li>[%= gruppoCarriera %]</li>
          <ol>[FOR varname="certificato" iterable="gruppoCertificatiIterable"]<li>[%= certificato %]</li>[/FOR]</ol>
        [/FOR]</ol>

        [/EFTL]
        `));

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