Skip to content

Commit 10b205d

Browse files
committed
Add new "Automation unblocker" page
1 parent 5b7bc70 commit 10b205d

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: 'Automation: Automation unblocker'
3+
sidebar_label: 'Automation unblocker'
4+
sidebar_position: 1
5+
---
6+
7+
# Automation unblocker
8+
9+
Unblocks specified datapoints when conditions are met.
10+
11+
## Source code
12+
13+
```py
14+
from txscript import TxScript
15+
from typing import Callable, Dict, Iterator, List
16+
from pydantic import BaseModel
17+
18+
19+
class FieldConfig(BaseModel):
20+
check_field: str
21+
fields_to_automate: List[str]
22+
23+
24+
class Settings(BaseModel):
25+
value_existence: List[FieldConfig] = []
26+
single_option: List[FieldConfig] = []
27+
28+
29+
def rossum_hook_request_handler(payload: dict) -> Dict[str, list]:
30+
settings = Settings.parse_obj(payload["settings"])
31+
content = payload["annotation"]["content"]
32+
33+
operations = []
34+
35+
for config in settings.value_existence:
36+
automation_ops = automate(
37+
config,
38+
content,
39+
lambda dp: dp["content"]["value"] != "",
40+
)
41+
operations.extend(automation_ops)
42+
43+
for config in settings.single_option:
44+
automation_ops = automate(
45+
config,
46+
content,
47+
lambda dp: len(dp.get("options",[])) == 1 and dp["content"]["value"] != "",
48+
)
49+
operations.extend(automation_ops)
50+
51+
return {"operations": operations}
52+
53+
54+
def automate(
55+
config: FieldConfig,
56+
content: List[dict],
57+
condition: Callable[[dict], bool],
58+
) -> List[dict]:
59+
operations = []
60+
for datapoint in find_all_by_schema_id(content, config.check_field):
61+
if condition(datapoint):
62+
for schema_id in config.fields_to_automate:
63+
for automated_dp in find_all_by_schema_id(content, schema_id):
64+
operations.append(create_automation_operation(automated_dp))
65+
return operations
66+
67+
68+
69+
def create_automation_operation(
70+
datapoint: dict,
71+
automation_type: str = "connector",
72+
) -> dict:
73+
"""
74+
Enable automation of a specific field by updating its validation sources.
75+
:param datapoint: content of the datapoint
76+
:param automation_type: type of the automation validation source to be used
77+
:return: dict with replace operation definition (see https://api.elis.rossum.ai/docs/#annotation-content-event-response-format)
78+
"""
79+
validation_sources = datapoint["validation_sources"]
80+
81+
if automation_type not in validation_sources:
82+
validation_sources.append(automation_type)
83+
84+
return {
85+
"op": "replace",
86+
"id": datapoint["id"],
87+
"value": {"validation_sources": validation_sources},
88+
}
89+
90+
91+
def find_all_by_schema_id(content: List[dict], schema_id: str) -> Iterator[dict]:
92+
for node in content:
93+
if node["schema_id"] == schema_id:
94+
yield node
95+
elif children := node.get("children"):
96+
yield from find_all_by_schema_id(children, schema_id)
97+
```
98+
99+
## Example configuration
100+
101+
```json
102+
{
103+
"single_option": [
104+
{
105+
"check_field": "order_header_match",
106+
"fields_to_automate": ["order_id"]
107+
}
108+
]
109+
}
110+
```

docs/learn/automation/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: 'Automation'
3+
sidebar_position: 1
4+
---
5+
6+
import DocCardList from '@theme/DocCardList';
7+
8+
<DocCardList />

0 commit comments

Comments
 (0)