{
  "name": "OLO • Приёмка задачи → CRM",
  "flow": [
    {
      "id": 1,
      "module": "gateway:CustomWebHook",
      "version": 1,
      "parameters": {
        "hook": null,
        "maxResults": 1
      },
      "mapper": {},
      "metadata": {
        "designer": {
          "x": 0,
          "y": 0,
          "name": "Приёмка OLO"
        },
        "parameters": [
          {
            "name": "hook",
            "type": "hook:gateway-webhook",
            "label": "Webhook",
            "required": true
          },
          {
            "name": "maxResults",
            "type": "number",
            "label": "Maximum number of results"
          }
        ],
        "interface": [
          {
            "name": "value",
            "type": "text",
            "label": "Raw JSON"
          },
          {
            "name": "__IMTHEADERS__",
            "type": "array",
            "label": "Request headers",
            "spec": [
              {
                "name": "name",
                "type": "text"
              },
              {
                "name": "value",
                "type": "text"
              }
            ]
          }
        ]
      }
    },
    {
      "id": 2,
      "module": "util:SetVariables",
      "version": 1,
      "parameters": {},
      "mapper": {
        "scope": "roundtrip",
        "variables": [
          {
            "name": "secret",
            "value": "SET_OLO_WEBHOOK_SECRET"
          },
          {
            "name": "timestamp",
            "value": "{{first(map(1.`__IMTHEADERS__`; \"value\"; \"name\"; \"x-olo-timestamp\"))}}"
          },
          {
            "name": "signature",
            "value": "{{first(map(1.`__IMTHEADERS__`; \"value\"; \"name\"; \"x-olo-signature\"))}}"
          },
          {
            "name": "event",
            "value": "{{first(map(1.`__IMTHEADERS__`; \"value\"; \"name\"; \"x-olo-event\"))}}"
          },
          {
            "name": "delivery",
            "value": "{{first(map(1.`__IMTHEADERS__`; \"value\"; \"name\"; \"x-olo-delivery\"))}}"
          }
        ]
      },
      "metadata": {
        "designer": {
          "x": 270,
          "y": 0,
          "name": "Секрет OLO и заголовки"
        }
      }
    },
    {
      "id": 3,
      "module": "builtin:BasicRouter",
      "version": 1,
      "parameters": {},
      "mapper": null,
      "metadata": {
        "designer": {
          "x": 540,
          "y": 0,
          "name": "Проверить подпись"
        }
      },
      "routes": [
        {
          "flow": [
            {
              "id": 4,
              "module": "json:ParseJSON",
              "version": 1,
              "parameters": {
                "type": ""
              },
              "mapper": {
                "json": "{{1.value}}"
              },
              "metadata": {
                "designer": {
                  "x": 795,
                  "y": -140,
                  "name": "Прочитать подписанное событие"
                },
                "parameters": [
                  {
                    "name": "type",
                    "type": "udt",
                    "label": "Data structure"
                  }
                ],
                "interface": [
                  {
                    "name": "id",
                    "type": "text"
                  },
                  {
                    "name": "event",
                    "type": "text"
                  },
                  {
                    "name": "version",
                    "type": "number"
                  },
                  {
                    "name": "data",
                    "type": "collection",
                    "spec": [
                      {
                        "name": "external_id",
                        "type": "text"
                      },
                      {
                        "name": "review_status",
                        "type": "text"
                      }
                    ]
                  }
                ]
              },
              "filter": {
                "name": "Подпись верна и актуальна",
                "conditions": [
                  [
                    {
                      "a": "{{2.secret}}",
                      "b": "SET_OLO_WEBHOOK_SECRET",
                      "o": "text:notequal"
                    },
                    {
                      "a": "{{2.secret}}",
                      "b": "{{emptystring}}",
                      "o": "text:notequal"
                    },
                    {
                      "a": "{{1.value}}",
                      "b": "{{emptystring}}",
                      "o": "text:notequal"
                    },
                    {
                      "a": "{{2.event}}",
                      "b": "task.accepted",
                      "o": "text:equal"
                    },
                    {
                      "a": "{{2.signature}}",
                      "b": "sha256={{sha256(2.timestamp + \".\" + 1.value; \"hex\"; 2.secret; \"text\")}}",
                      "o": "text:equal"
                    },
                    {
                      "a": "{{abs(parseNumber(formatDate(now; \"X\"); \".\") - parseNumber(ifempty(replace(ifempty(2.timestamp; \"0\"); /[^0-9]/g; emptystring); \"0\"); \".\"))}}",
                      "b": "301",
                      "o": "number:less"
                    }
                  ]
                ]
              }
            },
            {
              "id": 5,
              "module": "builtin:BasicRouter",
              "version": 1,
              "parameters": {},
              "mapper": null,
              "metadata": {
                "designer": {
                  "x": 1060,
                  "y": -140,
                  "name": "Проверить содержимое"
                }
              },
              "routes": [
                {
                  "flow": [
                    {
                      "id": 6,
                      "module": "http:ActionSendData",
                      "version": 3,
                      "parameters": {
                        "handleErrors": false,
                        "useNewZLibDeCompress": true
                      },
                      "mapper": {
                        "url": "https://crm.example.invalid/hooks/olo",
                        "method": "post",
                        "headers": [
                          {
                            "name": "Authorization",
                            "value": "Bearer SET_CRM_API_TOKEN"
                          },
                          {
                            "name": "Idempotency-Key",
                            "value": "{{4.id}}"
                          }
                        ],
                        "qs": [],
                        "bodyType": "raw",
                        "contentType": "application/json",
                        "data": "{{1.value}}",
                        "parseResponse": false,
                        "followRedirect": false,
                        "followAllRedirects": false,
                        "timeout": 3,
                        "shareCookies": false,
                        "useQuerystring": false,
                        "serializeUrl": false,
                        "gzip": true,
                        "rejectUnauthorized": true
                      },
                      "metadata": {
                        "designer": {
                          "x": 1330,
                          "y": -230,
                          "name": "Передать результат в CRM"
                        },
                        "interface": [
                          {
                            "name": "statusCode",
                            "type": "number",
                            "label": "Status code"
                          },
                          {
                            "name": "data",
                            "type": "any",
                            "label": "Data"
                          }
                        ]
                      },
                      "filter": {
                        "name": "Задача принята",
                        "conditions": [
                          [
                            {
                              "a": "{{4.id}}",
                              "b": "{{emptystring}}",
                              "o": "text:notequal"
                            },
                            {
                              "a": "{{4.id}}",
                              "b": "{{2.delivery}}",
                              "o": "text:equal"
                            },
                            {
                              "a": "{{4.event}}",
                              "b": "task.accepted",
                              "o": "text:equal"
                            },
                            {
                              "a": "{{toString(4.version)}}",
                              "b": "1",
                              "o": "text:equal"
                            },
                            {
                              "a": "{{4.data.external_id}}",
                              "b": "{{emptystring}}",
                              "o": "text:notequal"
                            },
                            {
                              "a": "{{4.data.review_status}}",
                              "b": "accepted",
                              "o": "text:equal"
                            }
                          ]
                        ]
                      }
                    },
                    {
                      "id": 7,
                      "module": "gateway:WebhookRespond",
                      "version": 1,
                      "parameters": {},
                      "mapper": {
                        "status": "{{if(floor(6.statusCode / 100) = 2; 204; 502)}}",
                        "body": "{{if(floor(6.statusCode / 100) = 2; emptystring; \"crm_unavailable\")}}",
                        "headers": [
                          {
                            "key": "Content-Type",
                            "value": "text/plain; charset=utf-8"
                          }
                        ]
                      },
                      "metadata": {
                        "designer": {
                          "x": 1600,
                          "y": -230,
                          "name": "Ответ OLO после CRM"
                        }
                      }
                    }
                  ]
                },
                {
                  "flow": [
                    {
                      "id": 8,
                      "module": "gateway:WebhookRespond",
                      "version": 1,
                      "parameters": {},
                      "mapper": {
                        "status": "422",
                        "body": "{\"error\":\"invalid_olo_event\"}",
                        "headers": [
                          {
                            "key": "Content-Type",
                            "value": "application/json; charset=utf-8"
                          }
                        ]
                      },
                      "metadata": {
                        "designer": {
                          "x": 1330,
                          "y": 0,
                          "name": "Отклонить событие"
                        }
                      },
                      "filter": {
                        "name": "Событие не подходит",
                        "conditions": [
                          [
                            {
                              "a": "{{4.id}}",
                              "b": "{{emptystring}}",
                              "o": "text:equal"
                            }
                          ],
                          [
                            {
                              "a": "{{4.id}}",
                              "b": "{{2.delivery}}",
                              "o": "text:notequal"
                            }
                          ],
                          [
                            {
                              "a": "{{4.event}}",
                              "b": "task.accepted",
                              "o": "text:notequal"
                            }
                          ],
                          [
                            {
                              "a": "{{toString(4.version)}}",
                              "b": "1",
                              "o": "text:notequal"
                            }
                          ],
                          [
                            {
                              "a": "{{4.data.external_id}}",
                              "b": "{{emptystring}}",
                              "o": "text:equal"
                            }
                          ],
                          [
                            {
                              "a": "{{4.data.review_status}}",
                              "b": "accepted",
                              "o": "text:notequal"
                            }
                          ]
                        ]
                      }
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "flow": [
            {
              "id": 9,
              "module": "gateway:WebhookRespond",
              "version": 1,
              "parameters": {},
              "mapper": {
                "status": "401",
                "body": "{\"error\":\"invalid_olo_signature\"}",
                "headers": [
                  {
                    "key": "Content-Type",
                    "value": "application/json; charset=utf-8"
                  }
                ]
              },
              "metadata": {
                "designer": {
                  "x": 795,
                  "y": 240,
                  "name": "Отклонить подпись"
                }
              },
              "filter": {
                "name": "Подпись не прошла проверку",
                "conditions": [
                  [
                    {
                      "a": "{{2.secret}}",
                      "b": "SET_OLO_WEBHOOK_SECRET",
                      "o": "text:equal"
                    }
                  ],
                  [
                    {
                      "a": "{{2.secret}}",
                      "b": "{{emptystring}}",
                      "o": "text:equal"
                    }
                  ],
                  [
                    {
                      "a": "{{1.value}}",
                      "b": "{{emptystring}}",
                      "o": "text:equal"
                    }
                  ],
                  [
                    {
                      "a": "{{2.event}}",
                      "b": "task.accepted",
                      "o": "text:notequal"
                    }
                  ],
                  [
                    {
                      "a": "{{2.signature}}",
                      "b": "sha256={{sha256(2.timestamp + \".\" + 1.value; \"hex\"; 2.secret; \"text\")}}",
                      "o": "text:notequal"
                    }
                  ],
                  [
                    {
                      "a": "{{abs(parseNumber(formatDate(now; \"X\"); \".\") - parseNumber(ifempty(replace(ifempty(2.timestamp; \"0\"); /[^0-9]/g; emptystring); \"0\"); \".\"))}}",
                      "b": "301",
                      "o": "number:greaterorequal"
                    }
                  ]
                ]
              }
            }
          ]
        }
      ]
    }
  ],
  "metadata": {
    "instant": true,
    "version": 1,
    "scenario": {
      "roundtrips": 1,
      "maxErrors": 3,
      "autoCommit": true,
      "autoCommitTriggerLast": true,
      "sequential": false,
      "slots": null,
      "confidential": false,
      "dataloss": false,
      "dlq": false,
      "freshVariables": false
    },
    "designer": {
      "orphans": []
    },
    "notes": []
  }
}
