text: --------------------------------------------------------------------------------
  -- REST AGENT ------------------------------------------------------------------
  --------------------------------------------------------------------------------
  /**
   * Запрос данных в формате REST JSON API (Агент).
   * @param {text} pPath - Путь
   * @param {jsonb} pPayload - JSON
   * @return {SETOF json} - Записи в JSON
   */
  CREATE OR REPLACE FUNCTION rest.agent (
    pPath       text,
    pPayload    jsonb default null
  ) RETURNS     SETOF json
  AS $$
  DECLARE
    r           record;
    e           record;
  
    arKeys      text[];
  BEGIN
    IF pPath IS NULL THEN
      PERFORM RouteIsEmpty();
    END IF;
  
    IF current_session() IS NULL THEN
  	PERFORM LoginFailed();
    END IF;
  
    CASE pPath
    WHEN '/agent/type' THEN
  
      FOR r IN SELECT * FROM jsonb_to_record(pPayload) AS x(fields jsonb)
      LOOP
        FOR e IN EXECUTE format('SELECT %s FROM api.type($1)', JsonbToFields(r.fields, GetColumns('type', 'api'))) USING GetEntity('agent')
        LOOP
          RETURN NEXT row_to_json(e);
        END LOOP;
      END LOOP;
  
    WHEN '/agent/method' THEN
  
      IF pPayload IS NULL THEN
        PERFORM JsonIsEmpty();
      END IF;
  
      arKeys := array_cat(arKeys, ARRAY['id']);
      PERFORM CheckJsonbKeys(pPath, arKeys, pPayload);
  
      IF jsonb_typeof(pPayload) = 'array' THEN
  
        FOR r IN SELECT * FROM jsonb_to_recordset(pPayload) AS x(id uuid)
        LOOP
          FOR e IN SELECT r.id, api.get_methods(GetObjectClass(r.id), GetObjectState(r.id)) as method FROM api.get_agent(r.id) ORDER BY id
          LOOP
            RETURN NEXT row_to_json(e);
          END LOOP;
        END LOOP;
  
      ELSE
  
        FOR r IN SELECT * FROM jsonb_to_record(pPayload) AS x(id uuid)
        LOOP
          FOR e IN SELECT r.id, api.get_methods(GetObjectClass(r.id), GetObjectState(r.id)) as method FROM api.get_agent(r.id) ORDER BY id
          LOOP
            RETURN NEXT row_to_json(e);
          END LOOP;
        END LOOP;
  
      END IF;
  
    WHEN '/agent/count' THEN
  
      IF pPayload IS NOT NULL THEN
        arKeys := array_cat(arKeys, ARRAY['search', 'filter', 'reclimit', 'recoffset', 'orderby']);
        PERFORM CheckJsonbKeys(pPath, arKeys, pPayload);
      ELSE
        pPayload := '{}';
      END IF;
  
      IF jsonb_typeof(pPayload) = 'array' THEN
  
        FOR r IN SELECT * FROM jsonb_to_recordset(pPayload) AS x(search jsonb, filter jsonb, reclimit integer, recoffset integer, orderby jsonb)
        LOOP
          FOR e IN SELECT count(*) FROM api.list_agent(r.search, r.filter, r.reclimit, r.recoffset, r.orderby)
          LOOP
            RETURN NEXT row_to_json(e);
          END LOOP;
        END LOOP;
  
      ELSE
  
        FOR r IN SELECT * FROM jsonb_to_record(pPayload) AS x(search jsonb, filter jsonb, reclimit integer, recoffset integer, orderby jsonb)
        LOOP
          FOR e IN SELECT count(*) FROM api.list_agent(r.search, r.filter, r.reclimit, r.recoffset, r.orderby)
          LOOP
            RETURN NEXT row_to_json(e);
          END LOOP;
        END LOOP;
  
      END IF;
  
    WHEN '/agent/set' THEN
  
      IF pPayload IS NULL THEN
        PERFORM JsonIsEmpty();
      END IF;
  
      arKeys := array_cat(arKeys, GetRoutines('set_agent', 'api', false));
      PERFORM CheckJsonbKeys(pPath, arKeys, pPayload);
  
      IF jsonb_typeof(pPayload) = 'array' THEN
  
        FOR r IN EXECUTE format('SELECT row_to_json(api.set_agent(%s)) FROM jsonb_to_recordset($1) AS x(%s)', array_to_string(GetRoutines('set_agent', 'api', false, 'x'), ', '), array_to_string(GetRoutines('set_agent', 'api', true), ', ')) USING pPayload
        LOOP
          RETURN NEXT r;
        END LOOP;
  
      ELSE
  
        FOR r IN EXECUTE format('SELECT row_to_json(api.set_agent(%s)) FROM jsonb_to_record($1) AS x(%s)', array_to_string(GetRoutines('set_agent', 'api', false, 'x'), ', '), array_to_string(GetRoutines('set_agent', 'api', true), ', ')) USING pPayload
        LOOP
          RETURN NEXT r;
        END LOOP;
  
      END IF;
  
    WHEN '/agent/get' THEN
  
      IF pPayload IS NULL THEN
        PERFORM JsonIsEmpty();
      END IF;
  
      arKeys := array_cat(arKeys, ARRAY['id', 'fields']);
      PERFORM CheckJsonbKeys(pPath, arKeys, pPayload);
  
      IF jsonb_typeof(pPayload) = 'array' THEN
  
        FOR r IN SELECT * FROM jsonb_to_recordset(pPayload) AS x(id uuid, fields jsonb)
        LOOP
          FOR e IN EXECUTE format('SELECT %s FROM api.get_agent($1)', JsonbToFields(r.fields, GetColumns('agent', 'api'))) USING r.id
          LOOP
            RETURN NEXT row_to_json(e);
          END LOOP;
        END LOOP;
  
      ELSE
  
        FOR r IN SELECT * FROM jsonb_to_record(pPayload) AS x(id uuid, fields jsonb)
        LOOP
          FOR e IN EXECUTE format('SELECT %s FROM api.get_agent($1)', JsonbToFields(r.fields, GetColumns('agent', 'api'))) USING r.id
          LOOP
            RETURN NEXT row_to_json(e);
          END LOOP;
        END LOOP;
  
      END IF;
  
    WHEN '/agent/list' THEN
  
      IF pPayload IS NOT NULL THEN
        arKeys := array_cat(arKeys, ARRAY['fields', 'search', 'filter', 'reclimit', 'recoffset', 'orderby']);
        PERFORM CheckJsonbKeys(pPath, arKeys, pPayload);
      ELSE
        pPayload := '{}';
      END IF;
  
      FOR r IN SELECT * FROM jsonb_to_record(pPayload) AS x(fields jsonb, search jsonb, filter jsonb, reclimit integer, recoffset integer, orderby jsonb)
      LOOP
        FOR e IN EXECUTE format('SELECT %s FROM api.list_agent($1, $2, $3, $4, $5)', JsonbToFields(r.fields, GetColumns('agent', 'api'))) USING r.search, r.filter, r.reclimit, r.recoffset, r.orderby
        LOOP
          RETURN NEXT row_to_json(e);
        END LOOP;
      END LOOP;
  
    ELSE
      RETURN NEXT ExecuteDynamicMethod(pPath, pPayload);
    END CASE;
  
    RETURN;
  END;
  $$ LANGUAGE plpgsql
    SECURITY DEFINER
    SET search_path = kernel, pg_temp;
  