1+ import asyncio
12import os
23import uuid
34from typing import Any , Dict , List , Optional , Tuple
1011 HEADER_TENANT_ID ,
1112)
1213from ...tracing import traced
13- from ..common import BaseService , FolderContext , UiPathApiConfig , UiPathExecutionContext
14+ from ..common import (
15+ BaseService ,
16+ FolderContext ,
17+ UiPathApiConfig ,
18+ UiPathConfig ,
19+ UiPathExecutionContext ,
20+ )
1421from .task_schema import TaskSchema
15- from .tasks import Task
22+ from .tasks import Task , TaskRecipient , TaskRecipientType
1623
1724
1825def _create_spec (
@@ -113,13 +120,94 @@ def _retrieve_action_spec(
113120 )
114121
115122
116- def _assign_task_spec (task_key : str , assignee : str ) -> RequestSpec :
117- return RequestSpec (
123+ async def _assign_task_spec (
124+ self , task_key : str , assignee : str | None , task_recipient : TaskRecipient | None
125+ ) -> RequestSpec :
126+ request_spec = RequestSpec (
118127 method = "POST" ,
119128 endpoint = Endpoint (
120129 "/orchestrator_/odata/Tasks/UiPath.Server.Configuration.OData.AssignTasks"
121130 ),
122- json = {"taskAssignments" : [{"taskId" : task_key , "UserNameOrEmail" : assignee }]},
131+ )
132+ if task_recipient :
133+ recipient_value = await _resolve_recipient (self , task_recipient )
134+ if (
135+ task_recipient .type == TaskRecipientType .USER_ID
136+ or task_recipient .type == TaskRecipientType .EMAIL
137+ ):
138+ request_spec .json = {
139+ "taskAssignments" : [
140+ {
141+ "taskId" : task_key ,
142+ "assignmentCriteria" : "SingleUser" ,
143+ "userNameOrEmail" : recipient_value ,
144+ }
145+ ]
146+ }
147+ else :
148+ request_spec .json = {
149+ "taskAssignments" : [
150+ {
151+ "taskId" : task_key ,
152+ "assignmentCriteria" : "AllUsers" ,
153+ "assigneeNamesOrEmails" : [recipient_value ],
154+ }
155+ ]
156+ }
157+ elif assignee :
158+ request_spec .json = {
159+ "taskAssignments" : [{"taskId" : task_key , "UserNameOrEmail" : assignee }]
160+ }
161+ return request_spec
162+
163+
164+ async def _resolve_recipient (self , task_recipient : TaskRecipient ) -> str :
165+ recipient_value = task_recipient .value
166+ if task_recipient .type == TaskRecipientType .USER_ID :
167+ user_spec = _resolve_user (task_recipient .value )
168+ user_response = await self .request_async (
169+ user_spec .method ,
170+ user_spec .endpoint ,
171+ json = user_spec .json ,
172+ content = user_spec .content ,
173+ headers = user_spec .headers ,
174+ scoped = "org" ,
175+ )
176+ recipient_value = user_response .json ().get ("email" )
177+ if task_recipient .type == TaskRecipientType .GROUP_ID :
178+ group_spec = _resolve_group (task_recipient .value )
179+ group_response = await self .request_async (
180+ group_spec .method ,
181+ group_spec .endpoint ,
182+ json = group_spec .json ,
183+ content = group_spec .content ,
184+ headers = group_spec .headers ,
185+ scoped = "org" ,
186+ )
187+ recipient_value = group_response .json ().get ("displayName" )
188+ return recipient_value
189+
190+
191+ def _resolve_user (entity_id : str ) -> RequestSpec :
192+ org_id = UiPathConfig .organization_id
193+ return RequestSpec (
194+ method = "POST" ,
195+ endpoint = Endpoint (
196+ "/identity_/api/Directory/Resolve/{org_id}" .format (org_id = org_id )
197+ ),
198+ json = {"entityId" : entity_id , "entityType" : "User" },
199+ )
200+
201+
202+ def _resolve_group (entity_id : str ) -> RequestSpec :
203+ org_id = UiPathConfig .organization_id
204+ return RequestSpec (
205+ method = "GET" ,
206+ endpoint = Endpoint (
207+ "/identity_/api/Group/{org_id}/{entity_id}" .format (
208+ org_id = org_id , entity_id = entity_id
209+ )
210+ ),
123211 )
124212
125213
@@ -181,6 +269,7 @@ async def create_async(
181269 app_folder_path : Optional [str ] = None ,
182270 app_folder_key : Optional [str ] = None ,
183271 assignee : Optional [str ] = None ,
272+ recipient : Optional [TaskRecipient ] = None ,
184273 ) -> Task :
185274 """Creates a new action asynchronously.
186275
@@ -226,8 +315,10 @@ async def create_async(
226315 headers = spec .headers ,
227316 )
228317 json_response = response .json ()
229- if assignee :
230- spec = _assign_task_spec (json_response ["id" ], assignee )
318+ if assignee or recipient :
319+ spec = await _assign_task_spec (
320+ self , json_response ["id" ], assignee , recipient
321+ )
231322 await self .request_async (
232323 spec .method , spec .endpoint , json = spec .json , content = spec .content
233324 )
@@ -249,6 +340,7 @@ def create(
249340 app_folder_path : Optional [str ] = None ,
250341 app_folder_key : Optional [str ] = None ,
251342 assignee : Optional [str ] = None ,
343+ recipient : Optional [TaskRecipient ] = None ,
252344 ) -> Task :
253345 """Creates a new task synchronously.
254346
@@ -294,8 +386,10 @@ def create(
294386 headers = spec .headers ,
295387 )
296388 json_response = response .json ()
297- if assignee :
298- spec = _assign_task_spec (json_response ["id" ], assignee )
389+ if assignee or recipient :
390+ spec = asyncio .run (
391+ _assign_task_spec (self , json_response ["id" ], assignee , recipient )
392+ )
299393 self .request (
300394 spec .method , spec .endpoint , json = spec .json , content = spec .content
301395 )
0 commit comments